Python Dependency Parsing

Dependency Parsing:

Dependency parsing is the process of assessing the grammatical structure of a sentence based on the dependencies between the words in the sentence.

In Dependency parsing, various tags represent the relationship between two words in a sentence.These are the dependencies tags.

In the phrase ‘rainy weather,’ for example, the word rainy alters the meaning of the noun weather. As a result, a dependency exists between the weather and the rainy, with the weather acting as the head and the rainy acting as the dependent or child. The amod tag, which stands for adjectival modifier, represents this dependency.

What is the purpose of Dependency Parsing?
Dependency parsing allows us to build a parsing tree using tags to detect the link between words in a phrase/sentence rather to using any Grammar rule as in syntactic parsing, giving us a lot of flexibility even when the order of words changes.

Dependency Parsing in Python

Approach:

  • Import spacy module using the import keyword
  • Import displacy function from spacy module using the import keyword
  • Give some random string as static input and store it in a variable
  • Similarly, give the second and third strings as static input and store it in separate variables
  • Use the spacy.load() function to create a language object, load in the model data and weights, and return it.
  • Get the dependency parsing result using the displacy.render() function by passing the above
    strings and jupyter=True as the arguments.
  • The Exit of the Program.

Below is the implementation:

# Import spacy module using the import keyword
import spacy
# Import displacy function from spacy module using the import keyword
from spacy import displacy
# Give some random string as static input and store it in a variable
str_1 = 'Hello this is btechgeeks.good morning all'
# Similarly give the second and third strings as static input and store it in separate variables
str_2 = 'welcome to the python coding platform'
str_3 = 'good morning this is btechgeeks'
# Use the spacy.load() function to create a language object, load in the model data and weights, and return it.
nlp=spacy.load('en_core_web_sm')
# Get the dependency parsing result using the displacy.render() function by passing the above
# strings and jupyter=True as the arguments
displacy.render(nlp(str_1),jupyter=True)
displacy.render(nlp(str_2),jupyter=True)
displacy.render(nlp(str_3),jupyter=True)

Output:

Dependency Parsing output for the first string

Dependency parsing Output for the second String

Dependency parsing output for the third String