斯坦福解析器和 NLTK windows

Stanford Parser and NLTK windows

我正在尝试 运行 NLTK 中的 Stanford Parser Windows。我正在 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('.', ['?'])])])]

raw_parse_sents returns 列表迭代器列表。您可以像这样遍历它们:

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('.', ['?'])])])]