Converting output of dependency parsing to tree
<p>I am using <code>Stanford dependency parser</code> and the I get the following output of the sentence</p>

<blockquote>
  <p>I shot an elephant in my sleep</p>
</blockquote>

<pre><code>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$'))]
</code></pre>

<p><strong>I want to convert this into a graph with nodes being each token and edges being the relation between them.</strong></p>

<p>I need the graph structure for further processing hence it would help if modification to it are easy and also must be easily representable.</p>

<p>Here is my code till now.</p>

<pre><code>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
</code></pre>

<p>How can I make such a graph structure?</p>

<p>You can traverse over <code>dep.triples()</code> and get your desired output.</p>

<p><strong>Code:</strong></p>

<pre><code>for triple in dep.triples():
    print triple[1],"(",triple[0][0],", ",triple[2][0],")"
</code></pre>

<p><strong>Output:</strong></p>

<pre><code>nsubj ( shot ,  I )
dobj ( shot ,  elephant )
det ( elephant ,  an )
nmod ( shot ,  sleep )
case ( sleep ,  in )
nmod:poss ( sleep ,  my )
</code></pre>

<p>For more information you can check : <a href="http://www.nltk.org/_modules/nltk/parse/dependencygraph.html" rel="nofollow">NLTK Dependencygraph</a> methods <code>triples()</code>, <code>to_dot()</code> and <code>dep.tree().draw()</code></p>

<p>Edit - </p>

<p>The output of <code>dep.to_dot()</code> is</p>

<pre><code>digraph G{
edge [dir=forward]
node [shape=plaintext]

0 [label="0 (None)"]
0 -&gt; 2 [label="root"]
1 [label="1 (I)"]
2 [label="2 (shot)"]
2 -&gt; 4 [label="dobj"]
2 -&gt; 7 [label="nmod"]
2 -&gt; 1 [label="nsubj"]
3 [label="3 (an)"]
4 [label="4 (elephant)"]
4 -&gt; 3 [label="det"]
5 [label="5 (in)"]
6 [label="6 (my)"]
7 [label="7 (sleep)"]
7 -&gt; 5 [label="case"]
7 -&gt; 6 [label="nmod:poss"]
}
</code></pre>

python data-structures nlp dependency-parsing