在Java中自动解释句子

在Java中自动解释句子,java,regex,Java,Regex,在Java中,我尝试使用正则表达式自动解释文本 因此,我需要找到一种方法,用随机生成的正则表达式匹配项替换正则表达式的第一个匹配项,如下所示: public static String paraphraseUsingRegularExpression(String textToParaphrase, String regexToUse){ //In textToParaphrase, replace the first match of regexToUse with a randoml

在Java中,我尝试使用正则表达式自动解释文本

因此,我需要找到一种方法,用随机生成的正则表达式匹配项替换正则表达式的第一个匹配项,如下所示:

public static String paraphraseUsingRegularExpression(String textToParaphrase, String regexToUse){
    //In textToParaphrase, replace the first match of regexToUse with a randomly generated match of regexToUse, and return the modified string.
}
那么,如何用随机生成的正则表达式匹配项替换字符串中正则表达式的第一个匹配项呢?(也许一个名为


例如,
释义正则表达式(“我今天很高兴”,“非常非常”(高兴)(高兴)(今天)此时(时刻(瞬间))
将用随机生成的正则表达式匹配项替换正则表达式的第一个匹配项,这可能会产生输出
“我现在非常高兴”
,或者
“我现在非常高兴”

您可以通过以下步骤完成:

首先,将
textToParaphrase
字符串与
regexToUse
拆分,您将得到一个数组,其中
textToParaphrase
的部分与提供的表达式不匹配。例如:如果,

 textToParaphrase = "I am very happy today for you";
 regexToUse = "(very|extremely) (happy|joyful) (today|at this (moment|time|instant in time))";
输出将是:
{“我是”,“为你”}
。 然后用这些生成的字符串创建一个正则表达式(如
“(我是你的)”
)。现在再次用这个生成的表达式拆分
texttoparaphase
,您将得到给定正则表达式的匹配部分的数组。最后,用随机生成的字符串替换每个匹配部分

代码如下所示:

public static String paraphraseUsingRegularExpression(String textToParaphrase, String regexToUse){
    String[] unMatchedPortionArray = textToParaphrase.split(regexToUse);
    String regExToFilter = "(";
    for(int i = 0; i< unMatchedPortionArray.length; i++){
        if(i == unMatchedPortionArray.length -1){
            regExToFilter+=unMatchedPortionArray[i];
        } else {
            regExToFilter+=unMatchedPortionArray[i]+"|";
        }
    }
    regExToFilter+=")";

    String[] matchedPortionArray = textToParaphrase.split(regExToFilter);
    Xeger generator = new Xeger(regexToUse);
    for (String matchedSegment : matchedPortionArray){
    String result = generator.generate(); //generates randomly (according to you!)
        textToParaphrase = textToParaphrase.replace(matchedSegment, result);
    }
    return textToParaphrase;
}
公共静态字符串释义正则表达式(字符串textToParaphrase,字符串regexpression){
String[]unMatchedPortionArray=textToParaphrase.split(reguse);
字符串regextrofilter=“(”;
for(int i=0;i

干杯

在这种情况下,一个名为的库可能会很有用。与其在你自己的问题下发表评论,不如将这些附加信息放在上面。如果你觉得这个库有用,你尝试过了吗?IMO除非你的输入非常有限,否则通过正则表达式这样做是一个非常糟糕的主意,基本上永远不会起作用。这可能是你想做的,在这种情况下,好吧,但是。。。“祝你好运!”安德森·松格林解释是NLP的一部分,这很难。你在这里所做的不是解释,而是马德利布斯。