javascript - 在 JavaScript 中自动释义句子

标签 javascript regex

在 JavaScript 中,是否可以使用随机生成的正则表达式匹配自动替换句子中的正则表达式?我正在尝试使用这种方法使用正则表达式列表自动解释一个句子,如下所示:

replaceWithRandomFromRegexes("你不是疯子!你是神童!", ["(genius|prodigy)", "(freak|loony|crackpot|crank|crazy)", "(You 're |You are |Thou art )", "(arent|ain't|are not)"])

此处,输入字符串中每个正则表达式的每个匹配项都应替换为随机生成的正则表达式匹配项。

function replaceWithRandomFromRegexes(theString, theRegexes){
    //For each regex in theRegexes, replace the first match of the regex in the string with a randomly generated match of that regex.
}

最佳答案

这似乎比你想象的要简单得多。怎么样:

function randomReplace(subject, groups, wordsOnly) {
    var meta = /([.?*+^$[\]\\(){}|-])/g, all = {};
    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)]
    });
}

例子:

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

如评论中讨论的那样,扩展功能可能是这样的:

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);
}

例子:

z = "(He|She|It|(B|R)ob(by|)) (real|tru|sure)ly is"
console.log(expand(z)) 

结果:

[
 "He really is",
 "He truly is",
 "He surely is",
 "She really is",
 "She truly is",
 "She surely is",
 "It really is",
 "It truly is",
 "It surely is",
 "Bobby really is",
 "Bobby truly is",
 "Bobby surely is",
 "Bob really is",
 "Bob truly is",
 "Bob surely is",
 "Robby really is",
 "Robby truly is",
 "Robby surely is",
 "Rob really is",
 "Rob truly is",
 "Rob surely is"
]

关于javascript - 在 JavaScript 中自动释义句子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16627247/

相关文章:

javascript - 我要制作一个解析器,我想学习正确的解析

python - 如何让正则表达式分别选择单词

javascript - 如何获得2个字符串之间的所有匹配项?

regex - 使用path_reg匹配haproxy路径

python - 注释和变量之间的 line.strip

javascript - 替换括号外仅包含字母的任何内容

javascript - 有效括号故障排除

javascript - 如果我在之前的选择中选择了一个选项,则删除输入字段

javascript - 具有动态 templateUrl 和 Transclude 的 Angular 指令

javascript - 改变 html slider 按钮滚动时的颜色