What is dependency parsing?

Share

Introduction

Natural Language Processing (NLP) is an interdisciplinary concept that uses synthetic intelligence and computational linguistics fundamentals to see how human languages interact with technology. There are many techniques used with NLP in which syntax and semantics are used for parsing. Parsing is the grammatical analysis of a sentence. For example, if a sentence is fed to NLP, parsing involves breaking the sentence into parts of speech, for example, noun, verb, etc.

Dependency parsing

Dependency parsing refers to examining the dependencies between the phrases of the sentence to determine the grammatical structure of a sentence. Dependency grammar provides a representation of a language as graphs. Nodes are words, and edges are dependencies. In this process, it is assumed that there is a direct relationship between each linguistic unit in a sentence. The relationships between each linguistic unit, or phrase, in the sentence, are expressed by directed arcs called dependency structures.

Dependency structure

Dependency structures are directed graphs that satisfy the following constraints:

  • They have a single designated root node that has no incoming arcs.
  • Each node has one incoming edge except the root node.
  • There is a unique path to each node from the root node.

Example

Example of dependency parsing

In this example, we see that we can traverse from the root word score to any other word in the structure. If we need to move from "score" to "good", we will move from "score" to "marks" and then from "marks" to "good".

Dependency tags

An essential part of dependency parsing is the dependency tag. A dependency tag indicates the relationship between two phrases. It is the word that modifies the meaning of the other word. If we look at the above example, the word "Intelligent" is an adjective for the word "students". The dependency tag used here is amod, which stands for the adjectival modifier. The start of the arrow represents the parent, and the end represents the child or dependent.

Some of the common tags are used for the syntactical relationships between the words. They are described below:

Commonly Used Dependency Tags

Dependency Tag

Description

Dependency Tag

Description

acl

clausal modifier of a noun (adnominal clause)

expl:pass

reflexive pronoun used in reflexive passive

acl:relcl

relative clause modifier

expl:pv

reflexive clitic with an inherently reflexive verb

advcl

adverbial clause modifier

fixed

fixed multiword expression

amod

adjectival modifier

goeswith

goes with

appos

appositional modifier

iobj

indirect object

aux 

auxiliary  

list

list

aux:pass

passive auxiliary

mark  

 marker

case

case-marking

nmod

nominal modifier

cc

coordinating conjunction

nmod:poss

possessive nominal modifier

cc:preconj 

preconjunct

nmod:tmod

temporal modifier

ccomp

clausal complement

nsubj

nominal subject

clf

classifier

nsubj:pass

passive nominal subject

compound

compound

nummod

numeric modifier

compound:prt

phrasal verb particle

obj  

 object

conj

conjunct

obl:arg

oblique argument

cop

copula

obl:lmod

locative modifier

csubj

clausal subject

obl:tmod

temporal modifier

dep

unspecified dependency

parataxis

parataxis

det

determiner

punct 

punctuation

 det:nummod

pronominal quantifier agreeing in the case with the noun

root 

root 

det:poss

possessive determiner

vocative 

vocative

discourse

discourse

xcomp  

 open clausal complement

expl

expletive



Implementation

Now, let's create a dependency parser in Python. Dependency parsing in Python is very easy and straightforward. We need to install some libraries. The implementation of the code for the above example is given below:

import spacy
nlp=spacy.load('en_core_web_sm')
text='Intelligent students score good marks easily.'
for token in nlp(text):
print(token.text,'->',token.dep_,'->',token.head.text)

Code explanation

  • Lines 1–2: We import the Python library spacy and then load the spacy pipeline for supporting the English language.
  • Line 4: A sample text in the English language.
  • Line 6: We have a Loop for each token in the text.
  • Line 7: The token.text returns the token of a sentence, token.dep_ returns the dependency tag for a word, and the token.head.text returns the respective head word (to which the arrow is pointing).

Copyright ©2024 Educative, Inc. All rights reserved