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!
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.
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.
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.
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.
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:
I used this when reading Text Analytics With Python: CH3, good read, please reference if you need more info about dependency-based parsing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With