我正试图在Windows中运行NLTK中的Stanford Parser。我在python中这样做。我的代码是
import os
from nltk.parse import stanford
os.environ['JAVAHOME'] = 'C:/Program Files/Java/jdk1.8.0_25/bin'
os.environ['STANFORD_PARSER'] = 'C:/jars'
os.environ['STANFORD_MODELS'] = 'C:/jars'
parser =stanford.StanfordParser(model_path="C:/Users/pc/Desktop/Project/englishPCFG.ser.gz")
sentences = parser.raw_parse_sents(("Hello, My name is Melroy.", "What is your name?"))
for i in sentences:
print i
这是它给出的输出
listiterator object at 0x03FB6150
listiterator object at 0x03FB61B0
我正在寻找以下输出:
Tree('ROOT', [Tree('S', [Tree('INTJ', [Tree('UH', ['Hello'])]), Tree(',', [',']), Tree('NP', [Tree('PRP$', ['My']), Tree('NN', ['name'])]), Tree('VP', [Tree('VBZ', ['is']), Tree('ADJP', [Tree('JJ', ['Melroy'])])]), Tree('.', ['.'])])]), Tree('ROOT', [Tree('SBARQ', [Tree('WHNP', [Tree('WP', ['What'])]), Tree('SQ', [Tree('VBZ', ['is']), Tree('NP', [Tree('PRP$', ['your']), Tree('NN', ['name'])])]), Tree('.', ['?'])])])]
答案 0 :(得分:2)
raw_parse_sents
会返回列表人员列表。您可以像这样迭代它们:
for myListiterator in sentences:
for t in myListiterator:
print t
> (ROOT
> (S
> (INTJ (UH Hello))
> (, ,)
> (NP (PRP$ My) (NN name))
> (VP (VBZ is) (ADJP (JJ Melroy)))
> (. .)))
> (ROOT
> (SBARQ
> (WHNP (WP What))
> (SQ (VBZ is) (NP (PRP$ your) (NN name)))
> (. ?)))
如果您想要引用的确切输出格式,可以这样做:
print [list(i)[0] for i in sentences]
> [Tree('ROOT', [Tree('S', [Tree('INTJ', [Tree('UH', ['Hello'])]), Tree(',', [',']), Tree('NP', [Tree('PRP$', ['My']), Tree('NN', ['name'])]), Tree('VP', [Tree('VBZ', ['is']), Tree('ADJP', [Tree('JJ', ['Melroy'])])]), Tree('.', ['.'])])]), Tree('ROOT', [Tree('SBARQ', [Tree('WHNP', [Tree('WP', ['What'])]), Tree('SQ', [Tree('VBZ', ['is']), Tree('NP', [Tree('PRP$', ['your']), Tree('NN', ['name'])])]), Tree('.', ['?'])])])]