使用词性标注否定句子

6
我正在尝试通过词性标注来否定句子。请参考以下内容:
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,这显然是不应该发生的。由于我的正则表达式技能并不那么好,是否有一种方法可以从使用的正则表达式中排除这些单词?

[编辑]此外,我非常欢迎您对这种否定实现提出其他意见/批评,因为我相信它仍然存在缺陷:-)


https://dev59.com/AXE85IYBdhLWcg3wvGOm - Aditya Mukherji
1个回答

3

试一试这个:

$sentence = preg_replace("/(\s)(?:(?!never|neither|not)(\w*))\/(JJ|MD|RB|VB|VBD|VBN)\b/", "$1not$2", $sentence);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接