regex - Automatically paraphrasing sentences in JavaScript -


in javascript, possible automatically replace regular expression in sentence randomly generated match of regular expression? i'm trying use approach automatically paraphrase sentence using list of regular expressions, so:

replacewithrandomfromregexes("you aren't crackpot! you're prodigy!", ["(genius|prodigy)", "(freak|loony|crackpot|crank|crazy)", "(you're |you |thou art )", "(aren't|ain't|are not)"])

here, each match of each regular expression in input string should replaced randomly generated match of regular expression.

function replacewithrandomfromregexes(thestring, theregexes){     //for each regex in theregexes, replace first match of regex in string randomly generated match of regex. } 

this seems simpler think. how about:

function randomreplace(subject, groups, wordsonly) {     var meta = /([.?*+^$[\]\\(){}|-])/g, = {};     groups.foreach(function(group) {         group.foreach(function(word) { all[word] = group })     });     var r = object.keys(all).         sort(function(x, y) { return y.length - x.length }).         map(function(x) { return x.replace(meta, "\\$&") }).         join("|");     if(wordsonly)         r = "\\b(" + r + ")\\b";     return subject.replace(new regexp(r, "g"), function($0) {         return all[$0][math.floor(math.random() * all[$0].length)]     }); } 

example:

s = randomreplace(     "you aren't crackpot! you're prodigy!",     [         ["genius", "prodigy"],          ["freak", "loony", "crackpot", "crank", "crazy"],          ["you're ", "you ", "thou art "],          ["aren't", "ain't", "are not"]     ] ); console.log(s) // ain't crank! thou art genius! 

the expansion function, discussed in comments, this:

function expand(s) {     var d = [];      function product(a, b) {         var p = [];         a.map(function(x) { b.map(function(y) { p.push(x + y) })});         return p;     }      function reduce(s) {         var m;         if(s.indexof("|") >= 0)             return [].concat.apply([], s.split("|").map(reduce));         if(m = s.match(/~(\d+)(.*)/))             return product(reduce(d[m[1]]), reduce(m[2]));         return [s];     }      function add($0, $1) { d.push($1); return '~' + (d.length - 1) }      s = s.replace(/([^()|]+)/g, add);     for(var r = /\(([^()]*)\)/g; s.match(r);)         s = s.replace(r, add);      return reduce(s); } 

example:

z = "(he|she|it|(b|r)ob(by|)) (real|tru|sure)ly is" console.log(expand(z))  

result:

[  "he is",  "he is",  "he surely is",  "she is",  "she is",  "she surely is",  "it is",  "it is",  "it surely is",  "bobby is",  "bobby is",  "bobby surely is",  "bob is",  "bob is",  "bob surely is",  "robby is",  "robby is",  "robby surely is",  "rob is",  "rob is",  "rob surely is" ] 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -