Parts-Of-Speech Tagging
Part-of-speech tagging as a hidden Markov model, decoded with the Viterbi algorithm over tag-transition and word-emission probabilities.
╌╌╌╌
A hidden Markov model treats a sentence as a sequence of hidden states — the part-of-speech tags — that emit the observed words. Tagging recovers the tag sequence most likely to have produced the sentence, and the Viterbi algorithm finds it exactly in time linear in the sentence length.
The model rests on two distributions, both estimated by counting over a tagged
corpus: transition probabilities between adjacent tags,
and emission probabilities of a word given its tag. The HMM
class keeps them as two nested maps, states (tag to word to probability) and
transitions (tag to following tag to probability), trained by reading paired
sentence and tag files — the Brown corpus in brown-train-sentences.txt and
brown-train-tags.txt — with a # token marking each sentence start. Under the
Markov assumption, the joint probability of a sentence and a tag
sequence factors as
and tagging asks for the tag sequence that maximizes it. Enumerating all sequences is exponential, so the search is folded into a dynamic program.
The decoder works from a single recurrence. Let be the probability of the best tag sequence ending in tag at position ; it depends only on the previous column,
so one left-to-right sweep fills an table, and back-pointers recover the winning sequence. A greedy tagger that commits to the best tag at each word can be led astray by a locally attractive choice; Viterbi keeps every option open until the whole sentence is scored. Probabilities are accumulated in log space, turning the products into sums and avoiding underflow on long sentences.
Laid out as a grid of positions by tags, the recurrence fills one column from the one before it, and every cell records which predecessor it chose — the back-pointer that lets the winning sequence be traced once the last column is scored:
- 1input: a sentence , transitions , emissions
- 2for each tag do
- 3
- 4for to do
- 5for each tag do
- 6
- 7
- 8return the sequence traced back from
A word never seen in training has zero emission probability under every tag,
which would zero out any path through it. To keep such a word from killing an
otherwise good sequence, HMM.viterbi charges a fixed unseenPenalty of
in log space for an unseen word, letting the transition structure pick a
plausible tag from context alone. Run over held-out data, testFile tags brown-test-sentences.txt and
scores its output against brown-test-tags.txt, counting correct against
incorrect tags.
References
- Project repository
- Reference notes: Sequence Labeling: POS and NER
- Reference notes: Probabilistic Reasoning over Time
╌╌ END ╌╌