1
votes

I am trying to use the Stanford parser in a small application written in Python with the NLTK interface. I tried the code given below.

Everything seems to work right, no errors, Java is launched but I systematically get an empty iterator () and the program does not display the parsing tree.

I use Windows 7, Python 3.4.3, JRE jre1.8.0_51. I did the same with the POS tagger but got the same empty result.


import os
from nltk.parse import stanford
os.environ['STANFORD_PARSER'] = 'path\\jars'
os.environ['STANFORD_MODELS'] = 'path\\jars'
os.environ['JAVAHOME']= "path\\Java\jre1.8.0_51\\bin"


parser = stanford.StanfordParser(model_path="path\\englishPCFG.ser.gz")
sentences = parser.raw_parse_sents(("Hello the world.", "Thank you for helping me with this problem."))
print(sentences)


for line in sentences:
    for sentence in line:
        sentence.draw() 
1
You can't iterate twice over the same iterator object. By doing print(sentences) you already use up the iterator, so it is empty by the time you want to draw the trees. Save sentences in a list to use it more than once. - lenz

1 Answers

0
votes

Try:

sentences = list(parser.raw_parse_sents(("Hello the world.", "Thank you for helping me with this problem.")))

for line in sentences:
    for sentence in line:
        sentence.draw()