Sub topic 9
Walking the Dependency Tree: Children, Subtree, and Ancestors
Assume you know basic Python, can load a spaCy model, and understand nouns, verbs, subjects, and objects. This chapter uses the dependency parse created by en_core_web_sm.
A dependency tree connects each token to the token it depends on. The head attribute points from a token to its governor. The governor is the word that controls or explains the token's grammatical role. A verb often governs its subject and object.
Immediate children
token.children returns the tokens that attach directly to a token. These are one step below it in the dependency tree. The order follows the sentence.
token.lefts and token.rights return only the immediate children that appear to the left or right of the token. These properties are useful when word order matters.
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.lemma_ == "approve")
print("verb:", verb.text)
print("head:", verb.head.text)
print("children:", [(child.text, child.dep_) for child in verb.children])
print("left children:", [token.text for token in verb.lefts])
print("right children:", [token.text for token in verb.rights])For this sentence, committee is a nominal subject of approved, and budget is its direct object. The article and adjective belong below those noun tokens, not directly below the verb.
Subtrees and ancestors
token.subtree walks from a token through all of its descendants. A descendant is a token that depends on the starting token, directly or indirectly. This lets you collect a complete phrase rooted at a particular word.
token.ancestors walks in the other direction. It yields the token's parent, then that parent's parent, until it reaches the sentence root. The result is useful when you need to understand the larger grammatical structure around a token.
noun = next(token for token in doc if token.text == "budget")
print("noun:", noun.text)
print("subtree:", " ".join(token.text for token in noun.subtree))
print("ancestors:", [token.text for token in noun.ancestors])The subtree rooted at budget includes the, new, and budget. Its ancestors include approved and then the sentence root, depending on the model's analysis.
Choosing the right traversal
Use the narrowest traversal that matches the task:
- Use
headto find the governor of one token. - Use
childrento inspect direct grammatical dependents. - Use
leftsorrightswhen the dependent's position matters. - Use
subtreeto collect a phrase and its modifiers. - Use
ancestorsto trace a token back through the sentence structure.
A subtree is not guaranteed to represent a perfect human-defined phrase. Statistical parsers can attach modifiers incorrectly, and some languages allow non-contiguous structures. Inspect dependency labels and token indices before using the result in an extraction system.
A practical inspection pattern
When debugging an extraction rule, print each token's text, dependency label, and head before writing traversal code. This reveals whether the model attached the words as your rule expects.
Exercise
Which property should you use to collect every token that depends directly or indirectly on a token such as "report"?
Show answer
Use token.subtree. Use token.children instead when you only want the immediate dependents.