Intermediate level · Module 5: Part-of-Speech Tagging and Hidden Markov Models · Lesson 3 of 3
The Viterbi Algorithm and Tagger Evaluation
Learners implement Viterbi decoding to find the most likely tag sequence, evaluate tagging accuracy and compare with spaCy.
In this lesson you will
- Implement the Viterbi algorithm
- Evaluate and compare taggers
Key concepts
Viterbi algorithmdynamic programmingbackpointerstagging accuracyunknown-word accuracy
The lesson
Checking every possible tag sequence is impossible for long sentences. Viterbi uses dynamic programming: for each word and each tag, it keeps only the best score of any path ending in that tag, plus a backpointer. At the end, it follows the backpointers to recover the best sequence.
The work grows with sentence length × (number of tags)², which is fast with the 12 universal tags. Use log probabilities to avoid underflow.
Evaluate on held-out sentences: overall accuracy, accuracy on unknown words, and the most common confusions. Compare with spaCy's tagger mapped to the same tags, and with NLTK's built-in HMM trainer as a check on your implementation.
Step by step
- Implement viterbi(words, trans, emit, tags) with log probabilities and backpointers.
- Tag the held-out sentences and compute accuracy.
- Compute accuracy on unknown words separately.
- Compare with spaCy and with NLTK's HiddenMarkovModelTrainer.
Practice activity
Implement, evaluate and compare an HMM tagger.
Expected result: A working Viterbi tagger with overall and unknown-word accuracy, compared with two references.
Check your understanding
-
What does Viterbi keep at each step?
Show answer
The best score for each tag and a backpointer to the previous tag.
-
Why report accuracy on unknown words separately?
Show answer
Unknown words are much harder, and the overall figure hides this.
Further learning
Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.