Sub topic 14
When the Parser Is Wrong
Prerequisites and the right mental model
Assume you know basic Python and grammar terms such as noun, verb, subject, and object. You should also know how to load a spaCy pipeline and inspect a Doc.
spaCy's dependency parser is a statistical model. It predicts a tree from the text. The tree is useful, but it is not a guaranteed grammatical analysis. Every token may receive a dependency label even when the prediction is wrong. Treat parser output as evidence to check, not as ground truth.
A parser can fail because of unfamiliar vocabulary, unusual word order, spelling errors, long sentences, ambiguous grammar, or a mismatch between the training data and your domain. News text, product reviews, legal documents, chat messages, and biomedical text have different patterns.
Common failure modes
A wrong tokenization can make every later result unreliable. For example, a product code, URL, or hyphenated phrase may be split differently from what your application expects.
Part-of-speech errors often cause dependency errors. In “We can book flights,” book is a verb. In “We read the book,” book is a noun. The surrounding words help the model decide, but ambiguous contexts remain difficult.
Attachment errors occur when a phrase could modify more than one token. In “I saw the researcher with a telescope,” the prepositional phrase might describe how the speaker saw the researcher or identify which researcher was seen.
Coordination also causes errors. In “The editors reviewed the paper and approved the release,” the second verb shares the subject. In longer sentences, a parser may attach the conjunction or shared subject incorrectly.
Sentence boundaries can fail when punctuation is missing or when abbreviations look like sentence endings. If sentence segmentation is wrong, the dependency tree may connect words across sentences or split one sentence into two.
Check output before using it
Check whether the pipeline actually contains the annotations your code expects. A tokenizer alone cannot provide reliable POS tags or dependencies. Then inspect a few representative examples from your real data.
import spacy
nlp = spacy.load("en_core_web_sm")
texts = [
"The small team wrote a detailed report.",
"The model flagged the request as unusual.",
]
for text in texts:
doc = nlp(text)
if not doc.has_annotation("DEP"):
raise RuntimeError("This pipeline has no dependency annotation")
print(f"\n{text}")
for sent in doc.sents:
print("sentence:", sent.text)
print([
(token.text, token.pos_, token.dep_, token.head.text)
for token in sent
])
roots = [token for token in sent if token.head == token]
if len(roots) != 1:
print("warning: unexpected number of sentence roots:", len(roots))The root check is a useful structural test, but it cannot prove that the tree is linguistically correct. A tree can have one root and still attach the wrong subject or object.
For important extraction tasks, validate relations with rules such as:
- Require the expected part of speech for a subject, verb, or object.
- Reject relations that cross sentence boundaries.
- Compare extracted spans with known examples.
- Log the original sentence beside every extracted relation.
- Review errors by domain and update the model or rules when needed.
Do not assume that a label named obj or nsubj has identical behavior across languages or models. Inspect examples from the specific pipeline you use. Also remember that spaCy does not provide a universal, directly comparable confidence score for every dependency decision. Structural checks and labeled evaluation data are more useful than invented certainty values.
A practical decision
Use dependency parsing when grammatical structure provides information that simpler matching cannot. For stable phrases, a regular expression or phrase matcher may be easier to test. For high-risk decisions, use parser output as one feature among several and keep a human review path.
Exercise
Your extractor finds model — approve — request, but the sentence is “The request was approved by the model.” What should you check before accepting the triple?
Show answer
Check the passive dependencies and the labels of request and model. The grammatical subject may be request, while model may be an agent. Do not assume that the first noun is the actor.