2016-04-21 3 views
0
from nltk.chunk.util import tagstr2tree 
from nltk import word_tokenize, pos_tag 
text = "John Rose Center is very beautiful place and i want to go there with Barbara Palvin. Also there are stores like Adidas ,Nike ,Reebok Center." 
tagged_text = pos_tag(text.split()) 

grammar = "NP:{<NNP>+}" 

cp = nltk.RegexpParser(grammar) 
result = cp.parse(tagged_text) 

print(result) 

Ausgang:NLTK - Chunk Grammatik nicht lesen Kommata

(S 
    (NP John/NNP Rose/NNP Center/NNP) 
    is/VBZ 
    very/RB 
    beautiful/JJ 
    place/NN 
    and/CC 
    i/NN 
    want/VBP 
    to/TO 
    go/VB 
    there/RB 
    with/IN 
    (NP Barbara/NNP Palvin./NNP) 
    Also/RB 
    there/EX 
    are/VBP 
    stores/NNS 
    like/IN 
    (NP Adidas/NNP ,Nike/NNP ,Reebok/NNP Center./NNP)) 

Die Grammatik i nur für Chunking verwenden funktioniert auf NNP-Tags, aber wenn Wörter mit Komma sequentiell sind sie immer noch auf der gleichen Linie. ich möchte, dass meine Brocken wie folgt aus:

(S 
    (NP John/NNP Rose/NNP Center/NNP) 
    is/VBZ 
    very/RB 
    beautiful/JJ 
    place/NN 
    and/CC 
    i/NN 
    want/VBP 
    to/TO 
    go/VB 
    there/RB 
    with/IN 
    (NP Barbara/NNP Palvin./NNP) 
    Also/RB 
    there/EX 
    are/VBP 
    stores/NNS 
    like/IN 
    (NP Adidas,/NNP) 
    (NP Nike,/NNP) 
    (NP Reebok/NNP Center./NNP)) 

Was soll ich in dem schreiben „Grammatik =“ oder kann ich die Ausgabe bearbeiten, wie ich oben geschrieben habe wie man sehen kann ich nur Eigennamen für meine benannte Entität Projekt analysieren pls? helfen Sie mir.

+0

Verwenden 'tagged_text = pos_tag (word_tokenize (Text))'. Versuchen Sie nicht, den Ausgabe-Tracker von github repo zu verwenden, um auf die SO-Frage aufmerksam zu machen. Der Issue Tracker sollte verwendet werden, um einen Fehler zu melden, eine Verbesserung vorschlagen. – alvas

+0

Außerdem hängt es nicht mit Java/StanfordNLP zusammen, also habe ich diese Tags entfernt. – alvas

+0

Bitte vermeiden Sie auch, mehrere Fragen zum selben Thema zu stellen und die Fragen minimal inkrementell zu variieren. – alvas

Antwort

2

Verwenden word_tokenize(string) statt string.split():

>>> import nltk 
>>> from nltk.chunk.util import tagstr2tree 
>>> from nltk import word_tokenize, pos_tag 
>>> text = "John Rose Center is very beautiful place and i want to go there with Barbara Palvin. Also there are stores like Adidas ,Nike ,Reebok Center." 
>>> tagged_text = pos_tag(word_tokenize(text)) 
>>> 
>>> grammar = "NP:{<NNP>+}" 
>>> 
>>> cp = nltk.RegexpParser(grammar) 
>>> result = cp.parse(tagged_text) 
>>> 
>>> print(result) 
(S 
    (NP John/NNP Rose/NNP Center/NNP) 
    is/VBZ 
    very/RB 
    beautiful/JJ 
    place/NN 
    and/CC 
    i/NN 
    want/VBP 
    to/TO 
    go/VB 
    there/RB 
    with/IN 
    (NP Barbara/NNP Palvin/NNP) 
    ./. 
    Also/RB 
    there/EX 
    are/VBP 
    stores/NNS 
    like/IN 
    (NP Adidas/NNP) 
    ,/, 
    (NP Nike/NNP) 
    ,/, 
    (NP Reebok/NNP Center/NNP) 
    ./.) 
+0

Danke! aber wie kann ich das so umschreiben? Gibt es eine Funktion zum Umschreiben des Chunked-Textes? http://stackoverflow.com/questions/36702150/python-re-write-the-text-with-its-proper-nouns-chunked?stw=2 –