2016-01-23 86 views
5

我在Python中使用nltk的斯坦福分析器,並從Stanford Parser and NLTK獲得幫助來設置斯坦福nlp庫。爲什麼使用nltk的斯坦福分析器不能正確解析一個句子?

from nltk.parse.stanford import StanfordParser 
from nltk.parse.stanford import StanfordDependencyParser 
parser  = StanfordParser(model_path="edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz") 
dep_parser = StanfordDependencyParser(model_path="edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz") 
one = ("John sees Bill") 
parsed_Sentence = parser.raw_parse(one) 
# GUI 
for line in parsed_Sentence: 
     print line 
     line.draw() 

parsed_Sentence = [parse.tree() for parse in dep_parser.raw_parse(one)] 
print parsed_Sentence 

# GUI 
for line in parsed_Sentence: 
     print line 
     line.draw() 

我得到錯誤的分析和依賴樹,如下面的例子所示,它將'sees'視爲名詞而不是動詞。

Example parse tree Example dependency tree

我應該怎麼辦? 當我改變句子時,它的工作完全正確,例如(一個='John see Bill')。 這句話正確的輸出中可以從這裏看到correct ouput of parse tree

正確的輸出的例子還顯示如下:

correctly parsed

correct dependency parsed tree

+0

請發佈完整的代碼片段,以便其他人理解'dep_parser'來自哪裏=) – alvas

回答

6

再次,沒有模型是完美的(見Python NLTK pos_tag not returning the correct part-of-speech tag ); P

您可以使用NeuralDependencyParser嘗試「更準確」的解析器。

首先正確安裝使用正確的環境變量(見Stanford Parser and NLTKhttps://gist.github.com/alvations/e1df0ba227e542955a8a),然後解析器:

>>> from nltk.internals import find_jars_within_path 
>>> from nltk.parse.stanford import StanfordNeuralDependencyParser 
>>> parser = StanfordNeuralDependencyParser(model_path="edu/stanford/nlp/models/parser/nndep/english_UD.gz") 
>>> stanford_dir = parser._classpath[0].rpartition('/')[0] 
>>> slf4j_jar = stanford_dir + '/slf4j-api.jar' 
>>> parser._classpath = list(parser._classpath) + [slf4j_jar] 
>>> parser.java_options = '-mx5000m' 
>>> sent = "John sees Bill" 
>>> [parse.tree() for parse in parser.raw_parse(sent)] 
[Tree('sees', ['John', 'Bill'])] 

請注意,NeuralDependencyParser只產生依賴性樹:

enter image description here

+0

我正在使用模型「englishPCFG.ser.gz」,並且您正在使用model「e nglish_UD.gz「。但是,我們如何選擇這些模型,以便我們能夠選擇正確的模型? –

+0

沒有完美的模型,也沒有正確/錯誤的模型,只是最適合您的數據的模型。所以我會說,嘗試所有這些,然後根據分析的最終目的是什麼來評估它們。 – alvas

+0

真的很感謝,....我有一個錯誤,請幫助我: –