php - 使用词性标注否定句子

标签 php regex nlp

我正在尝试找到一种方法来否定基于词性标记的句子。请考虑:

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

顺便说一句:在这个例子中,我使用了 POS-tagging implementationlexicon伊恩巴伯。此代码运行的示例是:

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,这显然应该发生。由于我的正则表达式技能还不够,有没有办法从使用的正则表达式中排除这些词?

[edit] 另外,我非常欢迎您在这个否定的实现中提出其他评论/批评,因为我确信它(仍然)有很大的缺陷:-)

最佳答案

试一试:

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

关于php - 使用词性标注否定句子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10398294/

相关文章:

php - 如何组织服务器端ajax脚本

regex - R:缩写字符串中的状态名称

php - 使用php从网站中提取数据

azure - 用于对话数据提取的 Luis 或文本分析

python - 如何使用无监督方法将句子分类到预定义的主题桶之一

php - 查询数据库后打印出部分唯一的数据

php - MYSQL 自然排序不符合预期

javascript - 使用表单数据和 jQuery(1.10.2) Ajax 在文件上传进度中未调用处理函数

javascript - 正则表达式只返回第一组

nlp - Smalltalk和tf-idf算法