How to get the dependency parse tree in spaCy

What is a dependency parse tree? While there is no simple explanation on the spaCy website, I found a good explanation on the Stanford Stanza website.

Dependency parsing .. builds a tree structure of words from the input sentence, which represents the syntactic dependency relations between words.


It is easier to explain with an example.

Add a new file called to the project and add the following code

Note: you need to download the en_core_web_sm model first to be able to run the script below
import spacy
from spacy import displacy

nlp = spacy.load("en_core_web_sm")
doc = nlp("Dialogflow, previously known as, is a chatbot framework provided by Google. Google acquired API.AI in 2016.")
displacy.serve(doc, style="dep")

Notice that we have also imported a new module called displacy. It allows us to visualize the results of the dependency parsing

When you run the code, the Run window in PyCharm provides a link to localhost

Click on the link, and you will see something like this:

What you see is the “tree” structure of the sentences, where the tree branches represent relationships.