Draw dependency tree in Python with stanfordcorenlp format

Viewed 1091

I am using StanfordCoreNLP for dependency parser sentence. I set up and use StanfordCoreNLP follow this tutorial: https://www.khalidalnajjar.com/setup-use-stanford-corenlp-server-python/ .

Input a sentence "He derives great joy and happiness from cycling", I got an output:

[('ROOT', 0, 2), ('nsubj', 2, 1), ('amod', 4, 3), ('dobj', 2, 4), ('cc', 4, 5), ('conj', 4, 6), ('prep', 2, 7), ('pobj', 7, 8)]

How i draw a dependency tree using nltk package or other package using this output.

Thank for help.

1 Answers

@ambientlight answered this question to draw a tree using stanford dependency tree.

The dependency tree looks as follows:

enter image description here

Using NLTK python, you can try as follows.

import nltk
from nltk.tokenize import word_tokenize
from nltk.tag import pos_tag
doc = "He derives great joy and happiness from cycling"
doc = nltk.word_tokenize(doc)
doc = nltk.pos_tag(doc)
grammar = "NP: {<DT>?<JJ>*<NN>}"
cp = nltk.RegexpParser(grammar)
result = cp.parse(doc)
result.draw()

Output: enter image description here

Related