0

I am using Stanford dependency parser and the I get the following output of the sentence

I shot an elephant in my sleep

>>>python dep_parsing.py 
[((u'shot', u'VBD'), u'nsubj', (u'I', u'PRP')), ((u'shot', u'VBD'), u'dobj', (u'elephant', u'NN')), ((u'elephant', u'NN'), u'det', (u'an', u'DT')), ((u'shot', u'VBD'), u'nmod', (u'sleep', u'NN')), ((u'sleep', u'NN'), u'case', (u'in', u'IN')), ((u'sleep', u'NN'), u'nmod:poss', (u'my', u'PRP$'))]

However, I want the numbered tokens as output just as it is here

nsubj(shot-2, I-1)
  root(ROOT-0, shot-2)
  det(elephant-4, an-3)
  dobj(shot-2, elephant-4)
  case(sleep-7, in-5)
  nmod:poss(sleep-7, my-6)
  nmod(shot-2, sleep-7)

Here is my code till now.

  from nltk.parse.stanford import StanfordDependencyParser
  stanford_parser_dir = 'stanford-parser/'
  eng_model_path = stanford_parser_dir + "stanford-parser-models/edu/stanford/nlp/models/lexparser/englishRNN.ser.gz"
  my_path_to_models_jar = stanford_parser_dir + "stanford-parser-3.5.2-models.jar"
  my_path_to_jar = stanford_parser_dir + "stanford-parser.jar"

  dependency_parser = StanfordDependencyParser(path_to_jar=my_path_to_jar, path_to_models_jar=my_path_to_models_jar)

  result = dependency_parser.raw_parse('I shot an elephant in my sleep')
  dep = result.next()
  a = list(dep.triples())
  print a

How can I have such an output?

Riken Shah
  • 3,022
  • 5
  • 29
  • 56
  • This is a different question, in this I have asked how can I get numbered output? and in the other one I have asked how can I display a graph. Should I still remove it? – Riken Shah Sep 06 '16 at 08:54

1 Answers1

0

Write a recursive function that traverses your tree. As a first pass, just try assigning the numbers to the words.

Rex D
  • 497
  • 3
  • 9
  • I don't wanna assign numbers manually, since numbers represent index of a particular word in a sentence, so in case of repeated words, that might create redundancy. – Riken Shah Sep 10 '16 at 06:05
  • You don't understand what I'm driving at. I'll write the code you need for $50. Pay only if satisfied. – Rex D Sep 11 '16 at 00:40