Dependency Parsing
The process of analyzing the grammatical structure of a sentence to determine how words relate to each other, establishing a tree of dependencies.
Why Does This Exist?
Part-of-Speech Tagging tells you that "dog" is a noun and "barked" is a verb. But it doesn't tell you which dog barked, or why it barked.
Consider the sentence: "The angry dog barked loudly at the mailman." To extract meaning from this sentence, a computer needs to know:
- Who performed the action? (The dog)
- What was the action? (Barked)
- Who was the target? (The mailman)
Dependency parsing extracts this exact relational structure. It connects words via directed links (dependencies) that explicitly state grammatical relationships, like subject, object, or modifier.
Think of It Like This
Think of It Like This
Imagine a corporate org chart.
The CEO (the main verb) is at the top. The VP of Sales and VP of Engineering report directly to the CEO (the subject and object). The sales team reports to the VP of Sales (the adjectives modifying the subject).
Dependency parsing takes a flat list of employees (a sentence) and automatically draws the org chart, showing exactly who reports to whom grammatically.
How It Actually Works
Dependency parsing models represent a sentence as a directed tree graph.
1. The Root
Every sentence has a root. In almost all cases, the root is the main verb of the sentence. All other words eventually point back to this root.
2. Heads and Dependents
Every word (except the root) has exactly one head (the word it modifies or depends on). A word can have multiple dependents (words that modify it).
- In "angry dog", "dog" is the head and "angry" is the dependent. The relationship is an adjectival modifier (
amod). - In "dog barked", "barked" is the head (the root) and "dog" is the dependent. The relationship is the nominal subject (
nsubj).
3. Transition-Based Parsing
Modern fast parsers (like the one used in spaCy) use a transition-based approach. The model reads the sentence left-to-right, maintaining a "stack" of words it is currently processing and a "buffer" of words it hasn't seen yet. A neural network decides whether to shift a word from the buffer to the stack, or to draw a dependency arrow between the top two words on the stack.
Show Me the Code
spaCy makes dependency parsing incredibly simple. It automatically computes the dependency tree when you pass a string through the pipeline.
import spacy
# Load the small English pipelinenlp = spacy.load("en_core_web_sm")
text = "The angry dog barked loudly at the mailman."doc = nlp(text)
print(f"{'Word':<10} | {'Dependency':<10} | {'Head Word'}")print("-" * 35)
for token in doc: # token.dep_ is the relationship label # token.head.text is the word it connects to print(f"{token.text:<10} | {token.dep_:<10} | {token.head.text}")
# -> The | det | dog# -> angry | amod | dog# -> dog | nsubj | barked# -> barked | ROOT | barked# -> loudly | advmod | barked# -> at | prep | barked# -> the | det | mailman# -> mailman | pobj | atWatch Out For
Watch Out For
It is strictly syntactic, not semantic. Dependency parsing only understands grammar, not meaning. If you parse the sentence "The rock ate the sandwich", the parser will happily label "rock" as the subject of "ate". It doesn't know that rocks cannot eat; it only knows that grammatically, the rock is performing the action. You must combine parsing with Named Entity Recognition or semantic models to extract true meaning.
The Quick Version
- Dependency parsing extracts the grammatical structure of a sentence.
- It outputs a directed tree graph where the main verb is the root.
- Every word points to the word it modifies (its head) with a specific grammatical label (subject, object, modifier).
- It is essential for information extraction, answering "who did what to whom".