Sub topic 8
Seeing the tree with displaCy
This chapter assumes you have basic Python, know terms such as subject, verb, and object, and have already loaded a trained spaCy pipeline. You also need the en_core_web_sm model installed. A dependency parse is useful in code, but a visualization often makes its structure easier to inspect.
What displaCy shows
spaCy's displaCy visualizer draws a sentence as a dependency tree. Each word appears as a token. Curved arrows connect dependent tokens to their head tokens. The arrow label shows the dependency relation, such as nsubj for nominal subject or dobj and obj for a direct object.
For example, in “The quick brown fox jumps,” the verb jumps is the head of the sentence. The noun fox attaches to it with the nsubj relation. The words The, quick, and brown attach below the noun phrase.
The direction can feel counterintuitive at first. A dependency arrow points from a head to its dependent. In the sentence above, jumps governs fox, so the relation is represented as jumps -> fox.
Rendering a parse as SVG
The displacy.render function creates an SVG document for a parsed Doc. SVG is a text-based image format that browsers can display. The following script writes the visualization to parse.svg.
from pathlib import Path
import spacy
from spacy import displacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("The quick brown fox jumps over the lazy dog.")
svg = displacy.render(doc, style="dep", jupyter=False)
Path("parse.svg").write_text(svg, encoding="utf-8")
print("Wrote parse.svg")Open parse.svg in a browser. The words appear along the bottom, and the arcs connect each dependent to its head. The labels describe the grammatical function of each connection.
In a Jupyter notebook, use jupyter=True instead:
displacy.render(doc, style="dep", jupyter=True)The style="dep" argument selects dependency visualization. displaCy also supports other styles, including named entities, but entity highlighting does not show the dependency tree.
Reading the visualization
Use the visualization as a debugging aid, not as proof that the parse is correct. Start by finding the root verb. Then inspect:
- Which token has the
nsubjrelation? - Which token is the object?
- Does a prepositional phrase attach to the verb or to a noun?
- Do modifiers attach to the word they describe?
- Does punctuation have a sensible attachment?
The visual layout is based on token order and tree structure. Long sentences can produce crossing or crowded arcs. This does not necessarily mean that the parser failed. It may simply mean that the sentence contains several nested phrases.
Inspecting a suspicious arc
When an arc looks wrong, compare it with the underlying token data. The head attribute identifies the token that governs the current token, while dep_ gives the relation label.
for token in doc:
print(
f"{token.text:>8} "
f"head={token.head.text:<8} "
f"dep={token.dep_}"
)This text output is easier to test than an image. A practical workflow is to render representative examples during development, then write checks for the relations your application depends on.
Exercise
What does an arrow from approved to budget labeled obj mean in a displaCy dependency diagram?
Show answer
approved is the head of budget, and budget functions as the direct object of the verb. The diagram shows a predicted grammatical relation, which should still be checked against the text.