-2

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() 
Mike
  • 1,158
  • 5
  • 22
  • 32
J. Mandy
  • 11
  • 1
  • 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 Aug 17 '15 at 20:25

1 Answers1

0

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() 
alvas
  • 115,346
  • 109
  • 446
  • 738
  • Thank you for your help. It tried your solution and it worked. I think my problem was with the firewall that was blocking my Java/Python interface. As you know, printing an iterator does not affect the place of the reading cursor. Thanks again for your prompt help. – J. Mandy Aug 18 '15 at 14:08
  • As @lenz said, you have finished iterating the iterator after printing the parsed sentence. – alvas Aug 18 '15 at 14:16