Tagging, parsing and Naive Bayes
Sequence labelling with hidden Markov models, the Naive Bayes classifier, and the precision/recall confusion matrix used to evaluate both.
Knowing the words of a sentence is only the beginning. Grammar lives in the
labels attached to them — noun, verb, adjective — and in the tree of phrases
those labels form. A parser that knows book is a verb in "they book a room"
and a noun in "read the book" unlocks search, translation, question answering
and a hundred downstream tasks.
This is sequence labelling: the label of one word depends on the word, but also on the labels around it. The classical tool is the hidden Markov model, and the classifier at its heart — Naive Bayes — is one of the most reusable ideas in machine learning.
Classification with a confusion matrix
Every tagger is a classifier being graded. Gold tag on one axis, predicted tag on the other, and the counts tell you exactly how it fails: which distinctions it confuses, and whether it is over- or under-predicting a tag. Precision, recall and F1 are just summaries of which axis you care about.
The widget runs two toy taggers over a labelled test set and rebuilds the confusion matrix, precision, recall and macro-F1 for each.
Gold tag down the rows, predicted tag across the columns. Switch taggers and watch the off-diagonal cells, and macro-F1, move.
| gold ↓ / pred → | NOUN | VERB | ADJ | ADV | ADP | DET |
|---|---|---|---|---|---|---|
| NOUN | 7 | 0 | 0 | 0 | 0 | 0 |
| VERB | 4 | 0 | 0 | 0 | 0 | 0 |
| ADJ | 5 | 0 | 0 | 0 | 0 | 0 |
| ADV | 1 | 0 | 0 | 2 | 0 | 0 |
| ADP | 0 | 0 | 0 | 0 | 4 | 0 |
| DET | 0 | 0 | 0 | 0 | 0 | 1 |
Hover a cell to see which test words fell into it.
NOUN
P 0.41 · R 1.00 · F1 0.58
VERB
P 0.00 · R 0.00 · F1 0.00
ADJ
P 0.00 · R 0.00 · F1 0.00
ADV
P 1.00 · R 0.67 · F1 0.80
ADP
P 1.00 · R 1.00 · F1 1.00
DET
P 1.00 · R 1.00 · F1 1.00
Real evaluation arithmetic on a hand-labelled toy test set. The “memorised” tagger is the degenerate case of a Naive Bayes tagger with no smoothing; the rule tagger is the kind of baseline a learned HMM or CRF must beat.
Naive Bayes, the generative classifier
For a document or a word with observed features and a class , Bayes' rule gives
The "naive" step assumes the features are conditionally independent given the class. That assumption is usually false, yet the resulting classifier is fast, needs little data, and remains remarkably competitive on text. In a unigram tagger the feature is just the word identity and ; with no smoothing it simply memorises the most frequent tag per word, which is exactly the first tagger in the widget.
Hidden Markov models and Viterbi
A tagger should use context, not just the word. An HMM models the label sequence as a Markov chain and emits words from labels:
with transition probabilities and emission probabilities . Decoding the best label sequence is dynamic programming: the Viterbi algorithm fills a trellis of best scores ending in each tag and back-pointers a path. It is — linear in sentence length.
Parsing in one paragraph
Tagging produces a flat label per word. Parsing produces structure: a context-free grammar rewrites non-terminals into sequences of symbols and a chart parser (CKY) fills a table of spans to recover the tree. Constituency trees group words into phrases; dependency trees link heads to dependents. Both are sequence-structure problems with the same dynamic-programming flavour as Viterbi, now over spans rather than positions.
Evaluation you must quote
For a tag : , , . Averaging over tags gives macro-F1, which weights rare tags equally; micro-averaging weights them by frequency. Quoting the wrong average is one of the most common ways a result looks better than it is.
Accuracy hides class imbalance
If 90% of tokens are nouns, a tagger that always predicts NOUN scores 90% accuracy and is useless. The confusion matrix and macro-F1 expose that immediately; always report per-class numbers, never a single accuracy figure, on imbalanced label sets.
Illustrative vs real
Both taggers in the widget are tiny: a memorised unigram model and a suffix/closed-class rule baseline over about twenty tags' worth of toy data. Real taggers use trigram HMMs with suffix-based unknown-word models, or neural BiLSTM/Transformer taggers with CRF decoding, evaluated on the Penn Treebank. The confusion matrix, precision/recall/F1 arithmetic and the HMM factorisation are exactly those used in practice.
Check yourself
Eduspheria wiki · Applied AI, Classical NLP
0 / 5 answered
From the exam paper
Modeled on NITJ AI-502, End-Sem May 2025
0 / 5 answered
Where next: video analytics — where the input is a stream of frames and motion itself becomes the signal.