我使用以下代码在python NLTK中打印解析树的结果。
import os
import nltk
from nltk.parse import stanford
from nltk.tokenize import sent_tokenize
os.environ['STANFORD_PARSER'] = '/home/gadheyan/Project/stanford-parser-full-2014-08-27'
os.environ['STANFORD_MODELS'] = '/home/gadheyan/Project/stanford-parser-full-2014-08-27'
parser = stanford.StanfordParser(model_path="/home/gadheyan/Project/stanford-parser-full-2014-08-27/stanford-parser-3.4.1-models/edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz")
sentences = parser.raw_parse_sents(["A rare black squirrel has become a regular visitor to a suburban garden"])
print sentences
我期望结果如下:
(ROOT
(S
(NP (DT A) (JJ rare) (JJ black) (NN squirrel))
(VP
(VBZ has)
(VP
(VBN become)
(NP (DT a) (JJ regular) (NN visitor))
(PP (TO to) (NP (DT a) (JJ suburban) (NN garden)))))))
但我得到了结果
<listiterator object at 0x7f6ed5c30890>
为什么会这样?
答案 0 :(得分:1)
raw_parse_sents的返回类型是iter(iter(Tree)),而不是字符串。 http://www.nltk.org/_modules/nltk/parse/stanford.html
尝试:
for line in sentences:
for sentence in line:
sentence.draw()