2012-05-01 10 views
5

Sto cercando di trovare un modo per annullare le frasi in base al tagging POS. Si prega di considerare:Negare frasi usando la codifica 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: In questo esempio, sto usando il POS-tagging implementation e lexicon di Ian Barber. Un esempio di questo codice in esecuzione potrebbe essere:

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 

Come si può vedere, (e questo problema è anche commentato nel codice), negando parole stesse vengono negati come wel: never diventa notnever, che ovviamente shouldn' t succedere Dal momento che le mie abilità regex non sono tutto questo, c'è un modo per escludere queste parole dalla regex usata?

[modifica] Inoltre, mi piacerebbe molto accogliere altri commenti/critiche che potreste avere in questa implementazione nega, dato che io sono sicuro che è (ancora) molto imperfetta :-)

+0

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

risposta

3

dare una prova:

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