Learn Dependency Parser and Dependency Tree Visualizer in Spacy

Introduction

In this article, we will take you through the tutorial of Dependency Parser and Dependency Tree in Spacy. We will discuss what is dependency parsing and then understand Spacy dependency parser with examples. Finally, we will show you how to visualize with the help of the dependency tree in Spacy.

Dependency Parsing

Dependency parsing is the process of generating the relationship among different words of a sentence and describing their syntactic roles.

E.g. in the phrase ‘running fast’, the dependence relation between the words is the ‘admov’ tag which stands for an adverbial modifier. Here “fast” is the child and “running” is the head text.

Similar to this, there exist many dependencies between words in an English sentence.

Dependency Parser and Dependency Tree Visualizer in Spacy

Spacy Dependency Parser

The Spacy library has a fast and accurate syntactic dependency parser which return various dependence parse tag describing the relationship between two words in a sentence. It has around 41 dependency parse tags.

Spacy also has a feature to visualize it by using the dependence tree and also has a bunch of options to access the dependence tree.

The dependence parse also provides input to sentence boundary detection in spacy and helps in iterating over noun phrases or chunks.

Example of Spacy Dependency Parser

In the below example of Spacy dependency parser, the attribute dep_  returns the dependency tag of a word and head.text returns the respective headword.

In [1]:
import spacy
nlp=spacy.load('en_core_web_sm')

text='I was heading towrds North'

print(f"{'Token':{8}} {'dependence':{6}} {'head text':{9}}  {'Dependency explained'} ")
for token in nlp(text):
     print(f"{token.text:{8}} {token.dep_+' =>':{10}}   {token.head.text:{9}}  {spacy.explain(token.dep_)} ")
[Out] :
Token dependence head text Dependency explained
I nsubj => heading nominal subject
was aux => heading auxiliary
heading ROOT => heading None
towrds dobj => heading direct object
North npadvmod => heading noun phrase as adverbial modifier

Dependency Parser Lists in Spacy

We can get the list of dependency parser in Spacy by using nlp.pipe_labels[‘parser’]

In [2]

import spacy

nlp = spacy.load("en_core_web_sm")
parser_lst = nlp.pipe_labels['parser']

print(len(parser_lst))
print(parser_lst)

[Out] :

41
['ROOT', 'acl', 'acomp', 'advcl', 'advmod', 'agent', 'amod', 'appos', 'attr', 'aux', 'auxpass', 'case', 'cc', 'ccomp', 'compound', 'conj', 'csubj', 'csubjpass', 'dative', 'dep', 'det', 'dobj', 'expl', 'intj', 'mark', 'meta', 'neg', 'nmod', 'npadvmod', 'nsubj', 'nsubjpass', 'nummod', 'oprd', 'parataxis', 'pcomp', 'pobj', 'poss', 'preconj', 'predet', 'prep', 'prt', 'punct', 'quantmod', 'relcl', 'xcomp']

Spacy Dependency Tree Example

In the code below we are generating the dependency tree using Spacy’s displacy function.

The arc is used to represent the dependency between two words in which the word at the arrowhead is the child, and the word at the end of the arrow is the head. The term dep is used for the arc label, which describes the type of syntactic relation that connects the child to the head.

The root word can act as the head of multiple words in a sentence but is not a child of any other word. The word ‘heading’ here has multiple numbers of the outgoing arrows and no incoming arrows hence it is the root word.

Except for the root word, every word has exactly one head. One interesting thing about the root word is that if you start tracing the dependencies in a sentence starting from any word you finally reach the root word.

In [2]:
import spacy
from spacy import displacy

nlp=spacy.load('en_core_web_sm')
displacy.render(doc,jupyter=True)
[Out] :

Dependency Parser and Dependency Tree Visualizer in Spacy

Given below is a list of Dependency and their explanations.

Dep explain
0 det determiner
1 compound compound
2 nsubj nominal subject
3 amod adjectival modifier
4 cc coordinating conjunction
5 conj conjunct
6 dobj direct object
7 relcl relative clause modifier
8 punct punctuation
9 ccomp clausal complement
10 prep prepositional modifier
11 pobj object of preposition
12 pcomp complement of preposition
13 advmod adverbial modifier
14 aux auxiliary
15 mark marker
16 nsubjpass nominal subject (passive)
17 auxpass auxiliary (passive)
18 advcl adverbial clause modifier
19 appos appositional modifier
20 dative dative
21 acomp adjectival complement
22 nummod numeric modifier
23 attr attribute
24 dep unclassified dependent
25 agent agent
26 expl expletive
27 acl clausal modifier of noun (adjectival clause)
28 npadvmod noun phrase as an adverbial modifier
29 neg negation modifier
30 xcomp open clausal complement
31 intj interjection

Iterating over Children

The spacy function has token.children attribute using which we can get the list of children of head text.

In [3]:
import spacy

nlp = spacy.load("en_core_web_sm")
doc = nlp("I was heading towards North.")
for token in doc:
    print(token.text, token.dep_, token.head.text, [child for child in token.children])
[Out] :
I nsubj heading []
was aux heading []
heading ROOT heading [I, was, towards]
towards prep heading [North]
North pobj towards []

Reference – Spacy Documentation

 

  • Afham Fardeen

    This is Afham Fardeen, who loves the field of Machine Learning and enjoys reading and writing on it. The idea of enabling a machine to learn strikes me.

Follow Us

Leave a Reply

Your email address will not be published. Required fields are marked *