gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 13:26:51 24 4
gpt4 key购买 nike

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

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com