mb

n-gram language model

an n-gram model guesses the next word from the few words before it, by counting how often each word followed that context in a corpus. a bigram looks back one word, a trigram two. add-k smoothing pretends every word was seen k extra times, so nothing gets probability zero.

looks at: the

nextseenpbar
dog40.40
cat30.30
log20.20
mat10.10
8 others00.00 each

#: one word that followed this context. dots: every word that never did, together. </s> ends the sentence

k = 0

press generate for a sentence

vocab
12
unseen share
0.00
unseen picks
—

after “the”: seen 10 times, 4 different next words

p(word | context) = (count + k) / (context count + k × 12), one k for every word in the vocabulary. sentences start with <s> and end with </s>, so the first words have a context too. underlined dashed: a pick that never followed its context in the corpus.

try this

  1. at k = 0, press generate a few times. every pair in the output occurs somewhere in the corpus: a pair it never saw has probability 0.
  2. slide k up to 1 and generate again. about two in three picks are now words that never followed the one before them. the rare words take over.
  3. switch to trigram with k at 0. most two-word contexts occur only once, so about half the sentences come out word for word from the corpus.