自然语言文本突变的模板语言

Template language for natural language text mutations

坚持一项看似会导致更广泛问题的相当琐碎的任务。

需要能够生成相同短文本的轻微变化。有些词形取决于说话者的性别,有些可以用同义词代替。

伪代码:

I {random:decided|made up my mind} to {random:try|test|give a try to}
this {new|fresh} {cool|awesome} {service|web service|online tool}.

我正在寻找一种 "industry standard" 模板语言来描述此类文本和可能的变体。进一步思考,我可能需要 global 变量(如性别变量),cross-links 用于句子前面选择的依赖项。

这看起来很接近正则表达式语法。理想情况下,非程序员会更多 readable/writable。

也许这个问题是众所周知的,有固态解决方案,比如专门针对该任务的编程语言?

假设您不允许在文本中使用括号或分隔符(或以某种方式转义它们),您可以毫不费力地完成此操作,例如 JavaScript:

function randreplace (txt) {
    var matches = txt.match(/\{([^}]+)\}/g);
    for (var m in matches) {
        m = matches[m];
        var opts = m.substring(1, m.length-1); // rm '{' and '}'
        opts = opts.split('|');
        var rand = opts[Math.floor(Math.random() * opts.length)];
        txt = txt.replace(m, rand);
    }
    return txt;
}
var example = "I {decided|made up my mind} to {try|test|give a try to} this {new|fresh} {cool|awesome} {service|web service|online tool}.";

console.log(randreplace(example));

我找不到这样的东西,所以我着手创建它。结果称为 Nalgene - the natural language generation language。语法相当简单,但也足够强大,可以支持递归短语、同义词、捕获值和依赖项。

%
    $person.name went to the $place to %action

%action
    ~buy a new $item
    ~sell @posessive($person.gender) $item

~buy
    buy
    purchase

$place
    store
    market

...

它输出生成的句子和树表示(主要目的是为 ML 系统生成训练数据)。

> jill went to the store to return her toothbrush

( %
    ( $person.name
        jill )
    ( $place
        store )
    ( %action
        ( $item
            toothbrush ) ) )

如果一年后你还在寻找,停下来打开一个问题,让我知道你在梦想的 NLG 语言中寻找什么。