MetaCyberGuru Academy
Grammar annotations help answer who did what to whom, but their labels are model predictions and must be checked on your domain.
Part-of-speech tags describe a token role such as noun or verb. Dependency arcs describe relationships such as subject, object and modifier. Together they support extraction rules that are more flexible than fixed word positions.
Treat a parse as structured evidence, not truth. Product names, fragments and informal messages can differ sharply from the model training data.
Read Tokens as Grammatical Roles and Dependency Edges
- Read token tags, heads and dependency labels from a parsed sentence.
- Trace a predicate to its subject and object while preserving source spans.
- Validate the labels your rule actually uses on a hand-reviewed sample.
- Trace one predicate through its subject and object arcs, then inspect a sentence where the parse breaks.
From a sentence to a graph
Each token points to a syntactic head, except the root. Labels describe the relationship, so word order can change while the underlying relation remains similar.
Universal POS tags are easier to compare across models; fine-grained tags provide language-specific detail.
The root anchors the analysis
Start extraction from likely predicates and follow named dependency relations. Do not assume the first noun is always the actor.
Where parsers fail first
Headlines, chat fragments, code mixed with prose and domain abbreviations often produce surprising trees. Long attachment chains can also be ambiguous.
Measure relation-level accuracy for your use case rather than quoting a general parser score.
Inspect before adding exceptions
Print token, head and dependency columns for failed sentences. One visible tree is often more useful than ten new regex rules.
If failure is systematic, annotate domain examples and consider model adaptation.
From phrase structure to HMM sequence tagging
Three parsing views answer different questions. Constituency parsing groups spans into nested phrases such as noun phrase and verb phrase. Dependency parsing connects each token to a head with a labelled grammatical relation. Shallow parsing, often called chunking, identifies useful local phrases without committing to a complete sentence tree. A noun-phrase extractor may need chunks; a subject-object rule often benefits from dependencies; a grammar-learning exercise may need constituents.
Hidden Markov Models provide a classical probabilistic route to part-of-speech tagging. The grammatical tag is a hidden state and the observed word is an emission. Start probabilities describe likely opening tags, transition probabilities describe likely tag sequences, and emission probabilities describe how likely a word is under each tag. These assumptions are simplified, but the model makes sequence reasoning inspectable.
Viterbi decoding instead of greedy tags
A greedy decoder chooses the best tag for the current word and can miss a stronger complete sequence. Viterbi keeps the best partial path ending in every state, multiplies its transition and emission evidence, and backtracks from the strongest final state. Real implementations use log probabilities to avoid numerical underflow.
states = ['N', 'V']
words = ['time', 'flies']
start = {'N': 0.6, 'V': 0.4}
transition = {'N': {'N': 0.2, 'V': 0.8}, 'V': {'N': 0.7, 'V': 0.3}}
emission = {'N': {'time': 0.8, 'flies': 0.2}, 'V': {'time': 0.1, 'flies': 0.9}}
paths = {state: (start[state] * emission[state][words[0]], [state]) for state in states}
for word in words[1:]:
next_paths = {}
for state in states:
candidates = []
for previous, (score, path) in paths.items():
new_score = score * transition[previous][state] * emission[state][word]
candidates.append((new_score, path + [state]))
next_paths[state] = max(candidates, key=lambda item: item[0])
paths = next_paths
best_score, best_path = max(paths.values(), key=lambda item: item[0])
print(best_path, round(best_score, 4))Expected Viterbi result
['N', 'V'] 0.3456The toy model reads time as a noun followed by flies as a verb. It is not a general English tagger. Unknown words have no emission probability, the independence assumptions are restrictive, and the hand-written probabilities are only for tracing the algorithm. Add smoothing and train probabilities from labelled sequences before evaluating a larger HMM.
Build the Part-of-Speech Tagging and Dependency Parsing example
Install spaCy and download the named English pipeline before running. The table exposes the exact annotations used by the extraction rule.
The relation uses the verb lemma, so reviewed and reviews can map to review while the original span remains available.
Install:
python -m pip install spacy
python -m spacy download en_core_web_smimport spacy
nlp = spacy.load('en_core_web_sm')
doc = nlp('The analyst reviewed the report carefully.')
for token in doc:
print(f'{token.text:10} {token.pos_:6} {token.dep_:8} head={token.head.text}')
root = next(t for t in doc if t.dep_ == 'ROOT')
subject = next((c for c in root.children if c.dep_ in {'nsubj','nsubjpass'}), None)
obj = next((c for c in root.children if c.dep_ in {'dobj','obj'}), None)
print('relation:', subject.text, root.lemma_, obj.text)Expected Tags, Heads and Dependency Paths
The DET det head=analyst
analyst NOUN nsubj head=reviewed
reviewed VERB ROOT head=reviewed
...
relation: analyst review reportDependency label names can vary across languages and model versions. Keep tests close to the installed model.
A missing subject or object is a normal result for fragments and intransitive verbs, not always an exception.
Diagnose failures in Part-of-Speech Tagging and Dependency Parsing
Print the complete parse for one failure and compare it with the relation your product needs.
| Symptom | Likely cause | Useful check |
|---|---|---|
| StopIteration is raised | The sentence has no dependency matching the rigid next() call | Use a safe default and record incomplete relations |
| A passive sentence reverses roles | The rule handles only active nsubj and object labels | Add explicit passive tests and map semantic roles carefully |
| Product names are split or mistagged | The model lacks domain vocabulary | Add tokenizer exceptions only when stable, then evaluate a domain sample |
Map actions in support messages
Collect twenty short, anonymised support sentences and extract predicate, actor and affected object.
- Hand-label the three fields first
- Run the parser and rule
- Count complete, partial and wrong relations
- Group errors by fragments, passive voice and domain vocabulary
Definition of done: Your report contains examples and relation-level counts, with no invented actor for missing text.
Stretch task: Compare rules based on dependency arcs with a word-position baseline.
Check your Part-of-Speech Tagging and Dependency Parsing reasoning
Trace the grammatical decisions before revealing the explanations. Concentrate on ambiguity, parser evidence and sentence-level failures.
Primary references for Part-of-Speech Tagging and Dependency Parsing
- spaCy linguistic features: official tags, dependencies and entity guidance.
- Universal Dependencies relation inventory: cross-lingual definitions for dependency relations used when checking parser output.
Re-run the parsing examples after a language model or spaCy dependency parser update. Preserve the model identifier with the annotated sentences.
Save your place
Completion is stored only in this browser on this device.
Share this page
Share this page with the people who will use it next.
Discussion
No comments yet. Add the first useful question or observation.
You must log in to post a comment.