0

I use following code to print result of a parse tree in 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

I expected the result as follows:

(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)))))))

But i got the result as

<listiterator object at 0x7f6ed5c30890>

Why does that happen?

Gadheyan .t.s
  • 377
  • 2
  • 9
  • 23

1 Answers1

1

The return type of raw_parse_sents is iter(iter(Tree)), not a string. http://www.nltk.org/_modules/nltk/parse/stanford.html

Try:

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

Stanford Parser and NLTK

Community
  • 1
  • 1
jgloves
  • 719
  • 4
  • 14