2012-05-01 44 views
5

我试图找到一种基于POS标记来否定句子的方法。请考虑:使用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"; 
} 

BTW:在这个例子中,我使用了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 

正如你所看到的,(这个问题也评论中的代码),否定词本身被否定为WEL:never成为notnever,这显然不该”不会发生。由于我的正则表达式技能不是全部,有没有办法从使用的正则表达式中排除这些单词?

[编辑]另外,我也非常欢迎其他的意见/批评你可能在这个否定的实现,因为我敢肯定,这(仍)相当有缺陷的:-)

+0

http://stackoverflow.com/questions/2633353/algorithm-for-negating-sentences –

回答

3

试试这个:

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