1

I am trying to run Stanford Parser in NLTK in Windows. I am doing it in python. My code for the same is

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

This is the output it gave

listiterator object at 0x03FB6150  
listiterator object at 0x03FB61B0

I am looking for the following output:

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('.', ['?'])])])]
Interrobang
  • 16,984
  • 3
  • 55
  • 63
rombi
  • 199
  • 3
  • 22

1 Answers1

2

raw_parse_sents returns a list of listiterators. You can iterate through them like this:

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

If you want the exact output format you quoted, you can do it like this:

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('.', ['?'])])])]
leekaiinthesky
  • 5,413
  • 4
  • 28
  • 39
  • Actually i am trying to find syntactic category pairs out of it i.e. parent and child node of every word.. Eg. in the first sentence these pairs are.. (INTJ, UH) , (NP, PRP$), (NP, NN) , (VP, VBZ) , (VP , VPZ) , (ADJP, JJ). Can you tell me how to do it.? @leekaiinthesky – rombi Mar 23 '15 at 15:29
  • @rombi I suggest you ask a new question on the site so that it gets top-level visibility from fresh eyes. The content of the new question is substantively different, and I may very well not have the best answer. :) – leekaiinthesky Mar 23 '15 at 16:06
  • 1
    Done.http://stackoverflow.com/questions/29215022/matrix-creation-from-stanford-parser-output – rombi Mar 23 '15 at 16:29