Skip to main content

dependency parsing using spaCy : spacy exploration part 2


Introduction: 

In our previous post, we discussed about the basic nlp works using spacy. If you have not read that post, read that post now for better understanding. Today we are going to discuss dependency parsing using spaCy. This is the second post of our spacy exploration series.

What is dependency parsing?

dependency parsing is the analyzing of a sentence in grammatical way, to establish the grammatical dependency between "head" words and other words which modify those heads.

The end result for dependency parsing can be thought to be creating a correct dependency tree as well as tagging the correct dependency tag on each words. In this case, a dependency tree is a directed graph, with one word connected to another with a arrow towards the dependent words from the head word. Look at this following dependency tree for example, from the stanford nlp's page:


 

Now there has been a lot of research on dependency parsing but I would not mention all of it. As our title states, we are interested with the spacy dependency parser. 

According to this post written by matthew honnibal ( author of spacy package), spacy uses a greedy transition based dependency parsing. Details of that algorithm is out of the scope of current discussion.

Now let's discuss about how to parse and analyze dependency tree using spaCy.

Token level informations available in spacy about dependency:

Spacy abstractly considers the dependency to be head to child using a arc of dependency. As spacy internally uses the transition based dependency parsing; which uses the terms like left arc, right arc; even spacy software also considers the edges from a head word to its dependent words as arcs.

In spacy, the nlp pipeline by default contains dependency parsing. So if you have not explicitly pointed out disabling it; then spacy completes the dependency parsing when the doc object is created after processing it. 

Therefore in your processed doc object; each token contains all the dependency related information as well as it contains the children of the token. Here children refer to the syntactic direct dependents in the text of that specific token. 

For reference check this following code:



import spacy
text = "Google announced their 3rd office in India today."
nlp = spacy.load("en_core_web_sm")
doc = nlp(text)
for token in doc:
    print(token.text, token.dep_,"Head of this token is", token.head.text,
     [child for child in token.children]

The above code on running, produces the dependency level, head token's text, and all the children tokens for the specific token.You can change the text to your desired one and then run it in your console. So this is how we can do dependency parsing using spaCy and see the token level basic values.

Now consider the fact that the dependency tree is basically a tree data structure and therefore parsing is possible. Also, each subtree of this dependency tree is also a tree; therefore one can run local tree based parsing and therefore analyze smaller structure within the tree.

Ways to parse the dependency tree:

There are number of attributes in the token object, which helps you to parse the dependency tree in the best ways possible. Other than analyzing the tree, I have not seen any practical use of these yet, hence I will list these down without further code discussion. For respective code snippets, one can visit the official site. So, the respective important attributes are:

(a) token.left and token.right: 

these attributes tell you which the token's children appear to the left of the token in the sentence and to the right of the children. You can directly get the numbers of these using token.n_lefts and token.n_rights attribute.

(b) token.subtree:

this directly gives you the subtree created from considering the current token as the relative head. Basically this gives you the subtree from the current token to its children and so on. But as a returned value this attribute provides you a ordered sequence of these tokens in the subtree.

(c) There are also few other attributes such as token.ancestors ( to go up the dependency tree from the current token) as well as token.right_edge and token.left_edge; in sense of going into the edges of the token.subtree.

Visualize the dependency tree:

Finally, to manually analyze and understand dependency from the spacy dependency parsing; one can create visualizations of the tree too. For this you will use the displacy from spacy. The sample code is as below:


import spacy
text = "displaCy uses JavaScript, SVG and CSS."
nlp = spacy.load("en_core_web_sm")
doc = nlp(text)
from spacy import displacy
displacy.render(doc,style='dep')

This creates following types of pictures of the dependency tree! 


 Pic credit: wikipedia

In conclusion, we went over a brief definition and description of what is dependency parsing, what algo spacy uses under the hood and finally explored the useful codes as well visualization code snippet for seeing and using dependency tree and dependency labels created. Thanks for reading and follow the blog for upcoming spacy exploration posts!

Further readings:

(1) How to manipulate and create spacy's pipeline and custom pipelines

(2) How to train neural network models using spacy

Comments

Popular posts from this blog

Mastering SQL for Data Science: Top SQL Interview Questions by Experience Level

Introduction: SQL (Structured Query Language) is a cornerstone of data manipulation and querying in data science. SQL technical rounds are designed to assess a candidate’s ability to work with databases, retrieve, and manipulate data efficiently. This guide provides a comprehensive list of SQL interview questions segmented by experience level—beginner, intermediate, and experienced. For each level, you'll find key questions designed to evaluate the candidate’s proficiency in SQL and their ability to solve data-related problems. The difficulty increases as the experience level rises, and the final section will guide you on how to prepare effectively for these rounds. Beginner (0-2 Years of Experience) At this stage, candidates are expected to know the basics of SQL, common commands, and elementary data manipulation. What is SQL? Explain its importance in data science. Hint: Think about querying, relational databases, and data manipulation. What is the difference between WHERE ...

What is Bort?

 Introduction: Bort, is the new and more optimized version of BERT; which came out this october from amazon science. I came to know about it today while parsing amazon science's news on facebook about bort. So Bort is the newest addition to the long list of great LM models with extra-ordinary achievements.  Why is Bort important? Bort, is a model of 5.5% effective and 16% total size of the original BERT model; and is 20x faster than BERT, while being able to surpass the BERT model in 20 out of 23 tasks; to quote the abstract of the paper,  ' it obtains performance improvements of between 0 . 3% and 31%, absolute, with respect to BERT-large, on multiple public natural language understanding (NLU) benchmarks. ' So what made this achievement possible? The main idea behind creation of Bort is to go beyond the shallow depth of weight pruning, connection deletion or merely factoring the NN into different matrix factorizations and thus distilling it. While methods like know...

Spacy errors and their solutions

 Introduction: There are a bunch of errors in spacy, which never makes sense until you get to the depth of it. In this post, we will analyze the attribute error E046 and why it occurs. (1) AttributeError: [E046] Can't retrieve unregistered extension attribute 'tag_name'. Did you forget to call the set_extension method? Let's first understand what the error means on superficial level. There is a tag_name extension in your code. i.e. from a doc object, probably you are calling doc._.tag_name. But spacy suggests to you that probably you forgot to call the set_extension method. So what to do from here? The problem in hand is that your extension is not created where it should have been created. Now in general this means that your pipeline is incorrect at some level.  So how should you solve it? Look into the pipeline of your spacy language object. Chances are that the pipeline component which creates the extension is not included in the pipeline. To check the pipe eleme...