Introduction to Dependency Parsing in Python

FeaImg Dependency Parsing

Hey folks! Today in this tutorial, we will be understanding what Dependency Parsing is and how to implement the same using the Python programming language.


What is Dependency Parsing?

The technique of assessing the grammatical structure of a sentence based on the dependencies between the words in a sentence is known as Dependency Parsing.

Various tags in dependency parsing describe the relationship between two words in a sentence. These are the dependencies tags.

Dependency Parsing Illustration
Dependency Parsing Illustration

Why is Dependency Parsing needed?

Dependency parsing allows us to construct a parsing tree using tags to determine the relationship between words in a sentence rather than using any Grammar rule as in syntactic parsing, which provides a lot of flexibility even when the order of words changes.


Implementing Dependency Parsing in Python

For implementing Dependency Parsing, we would make use of the spaCy module in Python. So, first of all, we import the same into our program.

import spacy
from spacy import displacy

Now let’s take some random sentences on which we want to perform Dependency Parsing.

text1 = 'It took me more than two hours to translate a few pages of English.'
text2 = 'The old rusted farm equipment surrounded the house predicting its demise.'
text3 = 'I am a great listener, really good with empathy vs sympathy and all that, but I hate people.'
text4 = 'The ants enjoyed the barbecue more than the family.'

Next, we use spacy.load function which will help to construct a language object and load in the model data and weights, and return it.

nlp=spacy.load('en_core_web_sm')

Finally, we will display the dependency parsing output for the texts mentioned in the previous code snippet.

displacy.render(nlp(text1),jupyter=True)
Dependency Parsing Output 1
Dependency Parsing Output 1

Dependency Parsing Output 2
Dependency Parsing Output 2
Dependency Parsing Output 3
Dependency Parsing Output 3
Dependency Parsing Output 4
Dependency Parsing Output 4

Conclusion

Congratulations! You just learned how to build a Dependency Parsing chart for sentences using NLP. Hope you enjoyed it! 😇

Liked the tutorial? In any case, I would recommend you to have a look at the tutorials mentioned below:

  1. Stemming and Lemmatization in Python
  2. Creating Bag of Words Model from Scratch in python
  3. Word Cloud using Python
  4. Sentiment Analysis using Python

Thank you for taking your time out! Hope you learned something new!! 😄