1 of 49

n-Gram Language Models

2 of 49

N-gram Language Models

Pawan Goyal (IIT Kharagpur)

Context Sensitive Spelling Correction

The office is about fifteen minuets from my house

Week 2: Lecture 4

2 / 24

3 of 49

N-gram Language Models

Pawan Goyal (IIT Kharagpur)

Context Sensitive Spelling Correction

The office is about fifteen minuets from my house

Week 2: Lecture 4

2 / 24

4 of 49

Context Sensitive Spelling Correction

Use a Language Model

P(about fifteen minutes from) > P(about fifteen minuets from)

N-gram Language Models

Pawan Goyal (IIT Kharagpur)

The office is about fifteen minuets from my house

Week 2: Lecture 4

2 / 24

5 of 49

Probablilistic Language Models: Applications

N-gram Language Models

Pawan Goyal (IIT Kharagpur)

Speech Recognition

P(I saw a van) >> P(eyes awe of an)

Week 2: Lecture 4

3 / 24

6 of 49

Probablilistic Language Models: Applications

N-gram Language Models

Pawan Goyal (IIT Kharagpur)

Speech Recognition

P(I saw a van) >> P(eyes awe of an)

Machine Translation

Which sentence is more plausible in the target language?

P(high winds) > P(large winds)

Week 2: Lecture 4

3 / 24

7 of 49

Probablilistic Language Models: Applications

N-gram Language Models

Pawan Goyal (IIT Kharagpur)

Speech Recognition

P(I saw a van) >> P(eyes awe of an)

Machine Translation

Which sentence is more plausible in the target language?

P(high winds) > P(large winds)

Other Applications

Context Sensitive Spelling Correction Natural Language Generation

...

Week 2: Lecture 4

3 / 24

8 of 49

Completion Prediction

N-gram Language Models

Pawan Goyal (IIT Kharagpur)

Language model also supports predicting the completion of a sentence.

  • Please turn off your cell ...
  • Your program does not ...

Week 2: Lecture 4

4 / 24

9 of 49

Completion Prediction

N-gram Language Models

Pawan Goyal (IIT Kharagpur)

Language model also supports predicting the completion of a sentence.

  • Please turn off your cell ...
  • Your program does not ...

Predictive text input systems can guess what you are typing and give choices on how to complete it.

Week 2: Lecture 4

4 / 24

10 of 49

Probabilistic Language Modeling

N-gram Language Models

Pawan Goyal (IIT Kharagpur)

Goal: Compute the probability of a sentence or sequence of words:

P(W)= P(w1, w2, w3,..., wn)

Week 2: Lecture 4

5 / 24

11 of 49

Probabilistic Language Modeling

N-gram Language Models

Pawan Goyal (IIT Kharagpur)

Goal: Compute the probability of a sentence or sequence of words:

P(W)= P(w1, w2, w3,..., wn)

Related Task: probability of an upcoming word:

P(w4|w1, w2, w3)

Week 2: Lecture 4

5 / 24

12 of 49

Probabilistic Language Modeling

N-gram Language Models

Pawan Goyal (IIT Kharagpur)

Goal: Compute the probability of a sentence or sequence of words:

P(W)= P(w1, w2, w3,..., wn)

Related Task: probability of an upcoming word:

P(w4|w1, w2, w3)

A model that computes either of these is called a language model

Week 2: Lecture 4

5 / 24

13 of 49

Computing P(W)

N-gram Language Models

Pawan Goyal (IIT Kharagpur)

How to compute the joint probability

P(about, fifteen, minutes, from)

Basic Idea

Rely on the Chain Rule of Probability

Week 2: Lecture 4

6 / 24

14 of 49

N-gram Language Models

Pawan Goyal (IIT Kharagpur)

The Chain Rule

Conditional Probabilities

P(B|A)= P(A, B)

P(A)

Week 2: Lecture 4

7 / 24

15 of 49

N-gram Language Models

Pawan Goyal (IIT Kharagpur)

The Chain Rule

Conditional Probabilities

P(B|A)= P(A, B)

P(A)

P(A, B)= P(A)P(B|A)

Week 2: Lecture 4

7 / 24

16 of 49

The Chain Rule

N-gram Language Models

Pawan Goyal (IIT Kharagpur)

Conditional Probabilities

P(B|A)= P(A, B)

P(A)

P(A, B)= P(A)P(B|A)

More Variables

P(A, B, C, D)= P(A)P(B|A)P(C|A, B)P(D|A, B, C)

Week 2: Lecture 4

7 / 24

17 of 49

The Chain Rule

N-gram Language Models

Pawan Goyal (IIT Kharagpur)

Conditional Probabilities

P(B|A)= P(A, B)

P(A)

P(A, B)= P(A)P(B|A)

More Variables

P(A, B, C, D)= P(A)P(B|A)P(C|A, B)P(D|A, B, C)

The Chain Rule in General

P(x1, x2,..., xn)= P(x1)P(x2|x1)P(x3|x1, x2) .. . P(xn|x1,..., xn1)

Week 2: Lecture 4

7 / 24

18 of 49

Probability of words in sentences

N-gram Language Models

Pawan Goyal (IIT Kharagpur)

 

Week 2: Lecture 4

8 / 24

19 of 49

Probability of words in sentences

N-gram Language Models

Pawan Goyal (IIT Kharagpur)

P(“about fifteen minutes from”) =

P(about) x P(fifteen | about) x P(minutes | about fifteen) x P(from | about fifteen minutes)

Week 2: Lecture 4

8 / 24

 

20 of 49

Estimating These Probability Values

N-gram Language Models

Pawan Goyal (IIT Kharagpur)

Count and divide

P(office | about fifteen minutes from) = Count (about fifteen minutes from office)

Count (about fifteen minutes from)

The counts can be estimated from a large corpus of text.

Week 2: Lecture 4

9 / 24

21 of 49

Estimating These Probability Values

N-gram Language Models

Pawan Goyal (IIT Kharagpur)

Count and divide

P(office | about fifteen minutes from) = Count (about fifteen minutes from office)

Count (about fifteen minutes from)

What is the problem

We may never see enough data for estimating these

Week 2: Lecture 4

9 / 24

Circa 2026:

We have seen “enough” English data

        • lexicon, syntax, frequent usage, popular usage, correct usage

… but not enough for

  • - reasoning
  • - neutral > hateful
  • - Unbiased > biased (this is worse)
  • - Other languages

22 of 49

Markov Assumption

N-gram Language Models

Pawan Goyal (IIT Kharagpur)

Simplifying Assumption: Use only the previous word

P(office | about fifteen minutes from) P(office | from)

Week 2: Lecture 4

10 / 24

23 of 49

Markov Assumption

N-gram Language Models

Pawan Goyal (IIT Kharagpur)

Simplifying Assumption: Use only the previous word

P(office | about fifteen minutes from) P(office | from)

Or the couple previous words

P(office | about fifteen minutes from) P(office | minutes from)

We can hope to get many more occurrences of these shorter phrases in a large corpus, which will help us better estimate the probabilities.

Week 2: Lecture 4

10 / 24

24 of 49

Markov Assumption

N-gram Language Models

Pawan Goyal (IIT Kharagpur)

More Formally: kth order Markov Model

Chain Rule:

Week 2: Lecture 4

11 / 24

25 of 49

Markov Assumption

N-gram Language Models

Pawan Goyal (IIT Kharagpur)

More Formally: kth order Markov Model

Chain Rule:

We approximate each component in the product

 

Week 2: Lecture 4

11 / 24

26 of 49

N-Gram Models

N-gram Language Models

Pawan Goyal (IIT Kharagpur)

P(office | about fifteen minutes from)

An N-gram model uses only N 1 words of prior context.

Week 2: Lecture 4

12 / 24

27 of 49

N-Gram Models

N-gram Language Models

Pawan Goyal (IIT Kharagpur)

P(office | about fifteen minutes from)

An N-gram model uses only N 1 words of prior context.

Unigram: P(office) Bigram: P(office | from)

Trigram: P(office | minutes from)

Unigram model: no context is used

Week 2: Lecture 4

12 / 24

28 of 49

N-Gram Models

N-gram Language Models

Pawan Goyal (IIT Kharagpur)

P(office | about fifteen minutes from)

An N-gram model uses only N 1 words of prior context.

Unigram: P(office) Bigram: P(office | from)

Trigram: P(office | minutes from)

Markov model and Language Model

Week 2: Lecture 4

12 / 24

29 of 49

N-Gram Models

N-gram Language Models

Pawan Goyal (IIT Kharagpur)

P(office | about fifteen minutes from)

An N-gram model uses only N 1 words of prior context.

Unigram: P(office) Bigram: P(office | from)

Trigram: P(office | minutes from)

Markov model and Language Model

An N-gram model is an N 1-order Markov Model

Week 2: Lecture 4

12 / 24

30 of 49

N-Gram Models

N-gram Language Models

Pawan Goyal (IIT Kharagpur)

We can extend to trigrams, 4-grams, 5-grams In general, an insufficient model of language:

Week 2: Lecture 4

13 / 24

31 of 49

N-Gram Models

We can extend to trigrams, 4-grams, 5-grams In general, an insufficient model of language:

language has long-distance dependencies:

“The computer which I had just put into the machine room on the fifth floor crashed.”

For any reasonable value of n, a n-gram language model cannot completely model a natural language.

N-gram Language Models

Pawan Goyal (IIT Kharagpur)

Week 2: Lecture 4

13 / 24

Theoretically, true..

But, LLMs are also statistical..

Approximate (1000-gram model)

+ IF

+RLHF

32 of 49

N-Gram Models

We can extend to trigrams, 4-grams, 5-grams In general, an insufficient model of language:

language has long-distance dependencies:

“The computer which I had just put into the machine room on the fifth floor crashed.”

In most of the applications, we can get away with N-gram models

N-gram Language Models

Pawan Goyal (IIT Kharagpur)

Week 2: Lecture 4

13 / 24

33 of 49

Estimating N-grams probabilities

N-gram Language Models

Pawan Goyal (IIT Kharagpur)

How to estimate the n-gram probabilities from a large text corpus?

Week 2: Lecture 4

14 / 24

34 of 49

Estimating N-grams probabilities

N-gram Language Models

Pawan Goyal (IIT Kharagpur)

Maximum Likelihood Estimate

Value that makes the observed data the “most probable”

Week 2: Lecture 4

14 / 24

35 of 49

An Example

N-gram Language Models

Pawan Goyal (IIT Kharagpur)

<s>I am here </s>

<s>who am I </s>

<s>I would like to know </s>

Week 2: Lecture 4

15 / 24

36 of 49

An Example

N-gram Language Models

Pawan Goyal (IIT Kharagpur)

<s>I am here </s>

<s>who am I </s>

<s>I would like to know </s>

Estimating bigrams

P(I|<s>) =

P(</s>|here) = P(would | I) = P(here | am) = P(know | like) =

Week 2: Lecture 4

15 / 24

37 of 49

An Example

N-gram Language Models

Pawan Goyal (IIT Kharagpur)

<s>I am here </s>

<s>who am I </s>

<s>I would like to know </s>

Estimating bigrams

P(I|<s>) = 2/3

P(</s>|here) =1 P(would | I) = 1/3 P(here | am) = 1/2 P(know | like) = 0

So, given a corpus, we can easily estimate such probabilities.

The larger the corpus, the better will be the probability estimates.

Week 2: Lecture 4

15 / 24

38 of 49

Bigram counts from 9222 Restaurant Sentences

N-gram Language Models

16 / 24

Pawan Goyal (IIT Kharagpur)

Some frequent bigrams:

I want want to to eat

eat lunch to spend

Week 2: Lecture 4

39 of 49

Computing bigram probabilities

N-gram Language Models

Pawan Goyal (IIT Kharagpur)

Normlize by unigrams

To compute the bigram probabilities, we also need the frequencies of each unigram.

P( want | i ) = c( i want ) / c( i ) = 827 / 2533 = 0.33

Week 2: Lecture 4

17 / 24

40 of 49

N-gram Language Models

Pawan Goyal (IIT Kharagpur)

Computing bigram probabilities

Normlize by unigrams

Bigram Probabilities

Week 2: Lecture 4

17 / 24

41 of 49

Computing Sentence Probabilities

N-gram Language Models

Pawan Goyal (IIT Kharagpur)

Now, we know how to estimate bigram probabilities from a given text corpus.

Next, how to estimate the probability of a sentence / phrase?

P(<s> I want english food </s>)

= P(I | <s>) x P(want | I) x P(english | want) x P(food | english ) x P(</s> | food)

Week 2: Lecture 4

18 / 24

42 of 49

Computing Sentence Probabilities

N-gram Language Models

Pawan Goyal (IIT Kharagpur)

P(<s> I want english food </s>)

= P(I | <s>) x P(want | I) x P(english | want) x P(food | english ) x P(</s> | food)

= 0.000031

Using the chain rule of probabilities, with a first order Markov assumption.

Week 2: Lecture 4

18 / 24

43 of 49

Now that we know how to compute the probability of a sentence / phrase, we can do many practical tasks:

Query completion

Predicting the next word as one types Deciding which translation is more ‘natural’ Context-sensitive spelling correction

44 of 49

What knowledge does n-gram represent?

N-gram Language Models

19 / 24

Pawan Goyal (IIT Kharagpur)

P(english|want) = .0011

P(chinese|want) = .0065

P(to|want) = .66 P(eat | to) = .28 P(food | to) = 0 P(want | spend) = 0 P (i | <s>) = .25

For the given corpus (food), Chinese is more desirable then English

Week 2: Lecture 4

Usually, a verb comes after ‘to’, not a noun

Many sentences start with ‘I’

Two verbs, e.g., ‘want’ and ‘spend’ do not come consecutively

Next word prediction ideally requires reasoning?

Example by Ilya Sutskever!

45 of 49

Practical Issues

N-gram Language Models

20 / 24

Pawan Goyal (IIT Kharagpur)

Adding is faster than multiplying

log(p1 p2 p3 p4)= logp1 + logp2 + logp3 + logp4

Everything in log space

Avoids underflow

Handling zeros

Use smoothing

Suppose a sentence contains a bigram that never occurs in

Week 2: Lecture 4

the corpus (from which the language model is learned)

Multiplication of many probability values can lead to underflow (can lead to zero values for small probabilities)

46 of 49

N-gram Language Models

21 / 24

Pawan Goyal (IIT Kharagpur)

Language Modeling Toolkit

Week 2: Lecture 4

47 of 49

Google N-grams

N-gram Language Models

22 / 24

Pawan Goyal (IIT Kharagpur)

Number of tokens: 1,024,908,267,229 Number of sentences: 95,119,665,584 Number of unigrams: 13,588,391 Number of bigrams: 314,843,401 Number of trigrams: 977,069,902 Number of fourgrams: 1,313,818,354 Number of fivegrams: 1,176,470,663

http://googleresearch.blogspot.in/2006/08/ all-our-n-gram-are-belong-to-you.html

Week 2: Lecture 4

48 of 49

Example from the 4-gram data

N-gram Language Models

23 / 24

Pawan Goyal (IIT Kharagpur)

serve as the inspector 66 serve as the inspiration 1390 serve as the installation 136 serve as the institute 187 serve as the institution 279 serve as the institutional 461

Week 2: Lecture 4

49 of 49

N-gram Language Models

24 / 24

Pawan Goyal (IIT Kharagpur)

Google books Ngram Data

Week 2: Lecture 4