Unigram Language Model Tokenization › Worked Example: Segmenting a Word
Language AI HandbookAll chapters Unigram Language Model Tokenization › Worked Example: Segmenting a WordOne section · Sections in this chapter6 / 11 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Let's trace through the core ideas with a small, concrete example. Working through a numerical example makes the abstract formulas tangible and reveals how the algorithm's decisions arise from the learned probabilities. Suppose our vocabulary contains these tokens with their probabilities:
| Token | ||
|---|---|---|
un | 0.05 | -1.301 |
happy | 0.04 | -1.398 |
ness | 0.03 | -1.523 |
unhappy | 0.02 | -1.699 |
happiness | 0.015 | -1.824 |
These probabilities reflect a hypothetical trained model. "un" is the most probable token (0.05). This reflects its frequent use as a prefix across many words. "happiness" has the lowest probability (0.015) because it is a longer, more specific token that appears less frequently in a typical corpus.
To segment "unhappiness," we consider the main candidate segmentations and compute the probability of each:
Segmentation A: ["un", "happy", "ness"]
We compute the probability as the product of the three token probabilities:
In log space:
Segmentation B: ["unhappy", "ness"]
In log space:
Segmentation C: ["un", "happiness"]
In log space:
Comparing these values: . The Viterbi algorithm chooses segmentation C, ["un", "happiness"], as the most probable.
This result deserves careful interpretation. Notice how the model preferred the two-token split using the compound token "happiness" over the three-token split ["un", "happy", "ness"]. Even though "happiness" has a lower individual probability (0.015) than "ness" (0.03) or "happy" (0.04), the two-token segmentation wins because multiplying two moderately small numbers gives a larger result than multiplying three small numbers. Each additional token in a segmentation multiplies in another factor less than one, so shorter segmentations tend to win unless the longer segmentation's tokens are individually much more probable.
This is the unigram LM's built-in Occam's razor: all else being equal, simpler (fewer tokens) segmentations are favored. The model will only use more tokens when the increased specificity of a longer segmentation brings enough probability gain to overcome the cost of the extra multiplication.
Now let's trace through the Viterbi recurrence for "unhappiness" (of length 11). We build the best table position by position:
- (empty string)
- (token "un" from positions 0-2)
- At position 9 ("unhappines"), we find "happiness" runs from positions 2-11, but we are only at position 9 here, so we must continue.
- : we check token "happiness" from positions 2-11. . We also check "ness" from positions 7-11: , where was computed as (if "unhappy" runs from 0 to 7). So this path gives .
The maximum at position 11 is (from the path via "un" then "happiness"), confirming our earlier calculation. The backtracking pointer at position 11 points to position 2, and the pointer at position 2 points to position 0, recovering the segmentation ["un", "happiness"].