【发布时间】:2012-05-01 13:24:07
【问题描述】:
我正在尝试找到一种基于 POS 标记来否定句子的方法。请考虑:
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 的 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] 另外,我非常欢迎您在这个否定实现中可能遇到的其他 cmets / 批评,因为我确信它(仍然)存在很大缺陷 :-)
【问题讨论】: