Overview
The Viterbi algorithm finds the most likely sequence of hidden states (POS tags) given observed emissions (words). It uses dynamic programming for O(n) efficiency.Requirements
The Viterbi POS tagger requires probability tables in the database to function effectively:| Table | Purpose | Required? |
|---|---|---|
pos_unigrams | P(tag) - Tag frequency distribution | Recommended |
pos_bigrams | P(tag₂ | tag₁) - Tag transition probabilities | Required |
pos_trigrams | P(tag₃ | tag₁, tag₂) - Trigram transitions | Recommended |
Building Database with POS Probabilities
POS probability tables are populated when you build a database with a POS tagger:Checking Database for POS Tables
Fallback Behavior
If POS probability tables are empty or missing:- Viterbi falls back to morphological analysis for tag prediction
- Accuracy drops from ~85% to ~70% (similar to rule-based tagger)
- A warning is logged: “Provider does not support bigram probabilities”
How It Works
Hidden Markov Model
Algorithm Steps
- Initialization: Set probabilities for first position
- Recursion: For each position, compute best path to each state
- Termination: Find best final state
- Backtracking: Reconstruct optimal path
Implementation
Python Wrapper
Cython Acceleration
The library includes a Cython implementation for 10x speedup:Configuration
Note: Unknown word handling is controlled via themin_prob(default:1e-10) andunknown_word_tag(default:"UNK") parameters on theViterbiTaggerconstructor, not via a separate penalty parameter.
Beam Search
Beam search prunes unlikely paths for efficiency:| Beam Width | Speed | Accuracy |
|---|---|---|
| 5 | Fast | ~83% |
| 10 | Balanced (default) | ~85% |
| 20+ | Slow | ~86% |
Joint Segmentation + Tagging
The Viterbi algorithm can jointly segment and tag text:Emission Score Logic
For the first token (t=1), emission scores are handled specially:Performance
| Operation | Cython | Python | Speedup |
|---|---|---|---|
| Tag 10 words | 0.5ms | 5ms | 10x |
| Tag 100 words | 3ms | 30ms | 10x |
| Tag 1000 words | 25ms | 250ms | 10x |
Integration
See Also
- POS Tagging - Feature documentation
- Joint Segment Tagger - Combined segmentation + tagging
- Transformer POS - Neural POS tagger