我正在尝试通过词性标注来否定句子。请参考以下内容:
include_once 'class.postagger.php';
function negate($sentence) {
$tagger = new PosTagger('includes/lexicon.txt');
$tags = $tagger->tag($sentence);
foreach ($tags as $t) {
$input[] = trim($t['token']) . "/" . trim($t['tag']) . " ";
}
$sentence = implode(" ", $input);
$postagged = $sentence;
// Concatenate "not" to every JJ, RB or VB
// Todo: ignore negative words (not, never, neither)
$sentence = preg_replace("/(\w+)\/(JJ|MD|RB|VB|VBD|VBN)\b/", "not$1/$2", $sentence);
// Remove all POS tags
$sentence = preg_replace("/\/[A-Z$]+/", "", $sentence);
return "$postagged<br>$sentence";
}
顺便提一下,在这个例子中,我使用了 Ian Barber 的 词性标注实现 和 词汇表。代码运行的一个示例如下:
echo negate("I will never go to their place again");
I/NN will/MD never/RB go/VB to/TO their/PRP$ place/NN again/RB
I notwill notnever notgo to their place notagain
正如您所看到的(这个问题也在代码中被评论了),否定词本身也被否定了:例如never
变成了notnever
,这显然是不应该发生的。由于我的正则表达式技能并不那么好,是否有一种方法可以从使用的正则表达式中排除这些单词?
[编辑]此外,我非常欢迎您对这种否定实现提出其他意见/批评,因为我相信它仍然存在缺陷:-)