Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get a dependency tree with Stanford NLP parser

Tags:

stanford-nlp

How can I get the dependency tree as Figure below. I can get the dependency relation as pure text, and also the dependency graph with the help of dependencysee tool. But how about the dependency tree which has words as nodes and dependency as edges. Thanks very much!

Standard Stanford dependencies (collapsed and propagated)

like image 801
happy77 Avatar asked Nov 20 '12 08:11

happy77


People also ask

What is dependency tree in NLP?

A dependency parse tree is the directed graph mentioned above which has the below features: Root has no Incoming arcs (can only be Head in Head-Dependent pair) Vertices(except Root) should have only one incoming arc (Only one Parent/Head) A Unique path should exist between Root & each vertex in the tree.

What is Stanford dependency parser?

A dependency parser analyzes the grammatical structure of a sentence, establishing relationships between "head" words and words which modify those heads. The figure below shows a dependency parse of a short sentence.

What is dependency parse tree?

More formally, a dependency parse tree is a graph where the set of vertices contains the words in the sentence, and each edge in. connects two words. The graph must satisfy three conditions: There has to be a single root node with no incoming edges.


2 Answers

These graphs are produced using GraphViz, an open source graph drawing package, originally from AT&T Research. You can find a method toDotFormat() in edu.stanford.nlp.trees.semgraph.SemanticGraph that will convert a SemanticGraph into dot input language format which can be rendered by dot/GraphViz. At present, there isn't a command-line tool that provides this functionality, but it's pretty straightforward using that method.

like image 99
Christopher Manning Avatar answered Sep 28 '22 01:09

Christopher Manning


Here is how you would do exactly that (in python)

Installing all needed dependencies (OS X):

# assuming you have java installed and available in PATH
# and homebrew installed

brew install stanford-parser
brew install graphviz
pip install nltk
pip install graphviz

code:

import os
from nltk.parse.stanford import StanfordDependencyParser
from graphviz import Source

# make sure nltk can find stanford-parser
# please check your stanford-parser version from brew output (in my case 3.6.0) 
os.environ['CLASSPATH'] = r'/usr/local/Cellar/stanford-parser/3.6.0/libexec'

sentence = 'The brown fox is quick and he is jumping over the lazy dog'

sdp = StanfordDependencyParser()
result = list(sdp.raw_parse(sentence))

dep_tree_dot_repr = [parse for parse in result][0].to_dot()
source = Source(dep_tree_dot_repr, filename="dep_tree", format="png")
source.view()

which results in:

enter image description here

I used this when reading Text Analytics With Python: CH3, good read, please reference if you need more info about dependency-based parsing.

like image 30
ambientlight Avatar answered Sep 28 '22 00:09

ambientlight