我正在尝试找到一种方法来否定基于词性标记的句子。请考虑:
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 implementation和 lexicon伊恩巴伯。此代码运行的示例是:
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/