Sub topic 6
Dependency Relations: `dep_` and `head`
This chapter assumes that you know basic Python, can run a spaCy pipeline, and understand terms such as noun, verb, subject, and object. You should also know how to access attributes in a Python object.
A dependency parse describes how tokens in a sentence relate to one another grammatically. Each token usually depends on another token. The dependent token stores the relation in token.dep_, and it points to the governing token through token.head.
Consider this sentence:
The committee approved the new budget.The verb approved is the central action. The noun committee is its nominal subject, and budget is its direct object. The words the and new depend on the nouns they describe.
Reading `dep_` and `head`
Load a trained English pipeline and inspect each token:
import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("The committee approved the new budget.")
for token in doc:
print(
token.text,
"dep_=",
token.dep_,
"head=",
token.head.text,
)The output will include relationships similar to these:
| Token | `dep_` | `head` |
|---|---|---|
| committee | `nsubj` | approved |
| approved | `ROOT` | approved |
| budget | `obj` | approved |
The dep_ value is a human-readable dependency label. For example, nsubj means nominal subject, obj means object, and ROOT marks the main token of the sentence. The head is another Token object, so you can inspect its text, lemma, part of speech, or other attributes.
The root token points to itself. In a simple sentence, the main verb is often the root, but this is not guaranteed. In a sentence with no ordinary verb, another token may be selected.
To look up labels, use spacy.explain:
import spacy
print(spacy.explain("nsubj"))
print(spacy.explain("obj"))
print(spacy.explain("ROOT"))A label can vary between languages, model versions, and annotation schemes. Check the labels produced by your model instead of assuming that every model uses exactly the same names.
Understanding the tree
Dependencies form a directed tree for each parsed sentence. An arrow goes conceptually from a dependent token to its head:
A token's immediate dependents are available through token.children:
import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("The committee approved the new budget.")
verb = next(token for token in doc if token.dep_ == "ROOT")
print("root:", verb.text)
print("children:", [(child.text, child.dep_) for child in verb.children])The result includes the subject and object, although the order can depend on the model and sentence. To find a token's path toward the root, use token.ancestors:
budget = next(token for token in doc if token.text == "budget")
print([ancestor.text for ancestor in budget.ancestors])This prints the heads above budget, usually beginning with approved.
Checking parser results
The parser is a statistical model. It predicts relationships from patterns learned during training, so unusual word order, spelling errors, domain-specific language, and long sentences can produce incorrect heads or labels.
Before using a parse for extraction, inspect representative examples. Compare the predicted structure with your intended grammar, and keep the original text available for review. A high-confidence-looking label is still a prediction, not a guarantee.
Exercise
In "The analyst reviewed the report.", which token is likely the root, and what dependency label should "analyst" usually have?
Show answer
"reviewed" is likely the root, and "analyst" should usually have the nsubj label because it is the nominal subject of the verb.