Foundations to Frontier
Tests

Part-of-Speech Tagging, Named Entity Recognition, HMMs and CRFs

Sequence labelling assigns a tag to every token. Part-of-speech tagging labels words as nouns, verbs and so on; named entity recognition marks people, organisations and places with BIO tags. Hidden Markov models are generative: they combine tag-to-tag transitions with tag-to-word emissions, and the Viterbi algorithm finds the best tag sequence. Conditional random fields are discriminative: they score whole tag sequences with arbitrary overlapping features of the words (suffixes, capitalisation, neighbours) plus transitions. This page covers tagsets, BIO encoding, HMM estimation, Viterbi and forward algorithms, maximum-entropy taggers, linear-chain CRFs and evaluation. In the exercises you implement Viterbi and the forward algorithm, then tag sequences from a known model with ambiguous words and noisy suffixes. Most-frequent-tag scored 0.77. The HMM reached 0.915 using context but only 0.58 on unseen words. A per-token maximum-entropy classifier reached 0.84 on unseen words but 0.85 overall. The CRF combined both: 0.947 overall and 0.89 on unseen words.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Labelling every word in a sentence

In “They test rockets”, “test” is a verb; in “The test failed” it is a noun. Taggers label each word with its role, or mark which words are names of people, places or companies. They use two clues: what the word looks like (an “-ing” ending suggests a verb, a capital letter a name) and what the neighbouring labels are (a word after “the” is rarely a verb). The best taggers combine both clues. The demo shows the kind of word-level cleaning that comes before tagging.

Language lab
Stage 4 (crudely stemmed): rocket launch launch successful. Each step shrinks the vocabulary but can lose information (here "launches" and "launched" both become "launch").
Why engineers care

Extracting part numbers, failure modes, locations and dates from maintenance logs, mission reports and air-traffic transcripts is named entity recognition. Taggers like these also feed parsers and search indexes.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A part-of-speech tagger labels each word with…
  2. 2. Named entity recognition finds…
  3. 3. A word after 'the' is rarely a…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Tagsets and encodings: Universal Dependencies has 17 POS tags. NER uses BIO tags (B-ORG, I-ORG, O) so that spans can be recovered; entities are scored by exact-span F1.
  • HMM: P(words, tags) = Π_t P(tag_t | tag_(t−1)) P(word_t | tag_t). Supervised estimation is counting (with smoothing); unknown words need a special emission.
  • Viterbi: dynamic programming over tags: δ_t(j) = max_i δ_(t−1)(i) + log A_ij + log B_j(w_t). Cost T·K² instead of K^T paths (200 vs 390,625 for 5 tags and 8 words).
  • Forward algorithm: the same recursion with sums instead of maxes gives P(words), used for training and for posterior marginals (forward–backward).
  • Maximum-entropy (log-linear) tagger: P(tag_t | features of the words) with arbitrary features: the word, its suffix, capitalisation, neighbouring words. No tag-to-tag interaction.
  • Linear-chain CRF: P(tags | words) ∝ exp(Σ_t θ·f(tag_(t−1), tag_t, words, t)). Combines rich features with transitions; trained by maximising conditional log-likelihood with forward–backward gradients; decoded with Viterbi.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Viterbi cost for K tags and T words:
  2. 2. The forward algorithm computes…
  3. 3. A CRF models…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Taggers in code

Exercise 1.

  • Implement Viterbi decoding for an HMM.
  • Implement the forward algorithm and check it against a brute-force sum over all paths.

Practice.

  • spaCy and Stanza provide trained POS and NER pipelines; sklearn-crfsuite trains CRFs from feature dictionaries; Hugging Face token-classification models (BERT + linear or CRF head) are the modern standard.
  • Work in log space to avoid underflow.
  • Align labels with subword tokens when using transformers (label only the first subword).
  • Report entity-level precision, recall and F1 (seqeval), not token accuracy.
import sklearn_crfsuite
feat = lambda s, i: {"w": s[i].lower(), "suf3": s[i][-3:], "cap": s[i][0].isupper(),
                     "prev": s[i - 1].lower() if i else "<s>"}
crf = sklearn_crfsuite.CRF(c1=0.1, c2=0.1, max_iterations=200)
crf.fit([[feat(s, i) for i in range(len(s))] for s in sents], tags)
🐍 Exercise 1: Viterbi and the forward algorithm
Decode the most likely state path and compute the sequence probability of an HMM, checking against brute force.
📝 Quick check (Level 2)
0/3 answered
  1. 1. A library for training CRFs from feature dicts:
  2. 2. NER is evaluated with…
  3. 3. HMM computations are done in…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Generative vs discriminative. An HMM models P(words, tags) and must explain every word. Adding overlapping features (suffix and word identity are not independent) breaks its independence assumptions. A CRF models only P(tags | words), so it can use any features of the whole input without modelling their distribution (Lafferty, McCallum and Pereira, 2001).

Why transitions matter. A per-token classifier decides each tag independently and cannot enforce that a determiner is followed by a noun or adjective, or that I-ORG follows B-ORG. The label bias problem of locally normalised models (MEMMs) is avoided by the CRF’s global normalisation.

Training. The CRF log-likelihood is concave in θ. Its gradient is observed feature counts minus expected counts under the model, where the expectations come from forward–backward marginals.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Overlapping features violate the assumptions of…
  2. 2. CRFs avoid label bias through…
  3. 3. The CRF gradient is…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

What do transitions and rich features each contribute to sequence tagging?

History.

  • L. R. Rabiner, “A tutorial on hidden Markov models and selected applications in speech recognition” (Proc. IEEE, 1989).
  • J. Lafferty, A. McCallum and F. Pereira, “Conditional random fields” (ICML 2001).
  • Z. Huang, W. Xu and K. Yu, “Bidirectional LSTM-CRF models for sequence tagging” (2015).

Exercise 2.

  • World: 5 tags (DET, NOUN, VERB, ADJ, PROPN) with fixed transitions, and 300 Zipfian word forms per open tag. 60% of nouns, verbs and adjectives carry a tag-specific suffix; the 20 most frequent noun and verb forms are shared (ambiguous); proper nouns are capitalised.
  • Data: 300 training sentences of 8 tokens; 1000 test sentences, in which 12.9% of tokens are unseen words.
  • Models:
    • most frequent tag per word;
    • supervised HMM (add-0.1 emissions) with Viterbi;
    • a per-token maximum-entropy classifier (word, 3-letter suffix and capitalisation features);
    • a linear-chain CRF with the same features plus transitions (exact forward–backward, Adam, 150 steps).
model accuracy accuracy on unseen words
most frequent tag 0.770 0.232
HMM 0.915 0.581
per-token max-ent 0.848 0.837
CRF 0.947 0.892
  • Transitions resolve ambiguity: the HMM uses context to tag shared noun/verb forms correctly, beating the per-token classifier overall (0.915 against 0.848).
  • Features handle unseen words: suffix and capitalisation let the max-ent classifier tag unseen words at 0.84, against 0.58 for the HMM, whose only clue for an unseen word is its neighbours.
  • The CRF gets both: highest overall and on unseen words.
  • Seeds: seeds 1 and 2 gave HMM 0.901/0.910, max-ent 0.836/0.830 and CRF 0.943/0.949.
  • A design step: my first world had every open-class word carry its tag’s suffix and no ambiguous words. Then the per-token classifier and the CRF both scored 1.00. Real language has ambiguous forms and imperfect morphology, so I added both.
🐍 Exercise 2 (research): what do transitions and rich features each contribute to sequence tagging?
Tag sequences from a known model with a most-frequent-tag baseline, a supervised HMM, a per-token maximum-entropy classifier and a linear-chain CRF; compare overall and unseen-word accuracy.

Sequence tagging in AI, robotics and aerospace

  • Information extraction: part numbers, fault codes, locations and times from maintenance and incident reports.
  • Speech interfaces: slot filling in voice commands (“set heading two-seven-zero”).
  • Robotics: labelling segments of sensor streams (activity or terrain segmentation) with HMMs and CRFs.
  • Design lesson: combine local evidence with structure over neighbouring labels.

References

  1. L. R. Rabiner (1989). “A tutorial on hidden Markov models and selected applications in speech recognition.” Proc. IEEE 77(2).
  2. J. Lafferty, A. McCallum and F. Pereira (2001). “Conditional random fields: probabilistic models for segmenting and labeling sequence data.” Proc. ICML.
  3. C. Sutton and A. McCallum (2012). “An introduction to conditional random fields.” Foundations and Trends in Machine Learning 4(4).