regex - Automatically paraphrasing sentences in Java -
in java, i'm trying automatically paraphrase text using regular expressions.
so i'll need find way replace first match of regular expression randomly generated match of regular expression, this:
public static string paraphraseusingregularexpression(string texttoparaphrase, string regextouse){ //in texttoparaphrase, replace first match of regextouse randomly generated match of regextouse, , return modified string. } so how can replace first match of regular expression in string randomly generated match of regular expression? (perhaps library called xeger useful purpose.)
for example, paraphraseusingregularexpression("i happy today", "(very|extremely) (happy|joyful) (today|at (moment|time|instant in time))"); replace first match of regular expression randomly generated match of regular expression, produce output "i extremely joyful @ moment in time", or "i happy @ time".
you can steps bellow:
first, split texttoparaphrase string regextouse , array portions of texttoparaphrase doesn't matched provided expression. example: if,
texttoparaphrase = "i happy today you"; regextouse = "(very|extremely) (happy|joyful) (today|at (moment|time|instant in time))"; the output : {"i ", "for you"}. create regular expression these generated strings (like "(i |for you)"). again split texttoparaphrase generated expression , array of matched portions of given regular expression. , replace each of matched portion randomly generated string.
the code bellow:
public static string paraphraseusingregularexpression(string texttoparaphrase, string regextouse){ string[] unmatchedportionarray = texttoparaphrase.split(regextouse); string regextofilter = "("; for(int = 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); (string matchedsegment : matchedportionarray){ string result = generator.generate(); //generates randomly (according you!) texttoparaphrase = texttoparaphrase.replace(matchedsegment, result); } return texttoparaphrase; } cheers!
Comments
Post a Comment