Download N-Gram Language Models - Lecture Notes | CMSC 723 and more Study notes Computer Science in PDF only on Docsity!
CMSC 723/LING 723
Computational Linguistics I
N-gram Language Models
Lecture 9
October 31, 2007
Professor : Bonnie Dorr
Co-instructor : Nitin Madnani
TAs : Hamid Shahri, Alexandros Tzannes
1
Administrivia
Take-home midterm due now!
Assignment 3 graded; Assignment 4 out
Assignment Submission
Use the subject “CMSC 723: Assignment N”
Print out written portion and submit in class
Submit zips/tarballs; not individual files
Forum open for use
N-grams
Partially overlapping word sequences of length N This is a sentence N= 1 (Unigrams) No overlap/dependence This, is, a, sentence (4)
N-grams
Partially overlapping word sequences of length N This is a sentence N= 2 (Bigrams) This is, is a, a sentence (3)
Computing Probabilities
Is this practical?
No! Can’t keep track of all possible histories of all words!
P (w
1
, w
2
,... , w
T
[ By chain rule ]
= P (w
1
)P (w
2
|w
1
)P (w
3
|w
1
, w
2
)... P (w
T
|w
1
,... , w
T − 1
Approximating Probabilities
Basic idea: limit history to fixed number of words (N)
(Markov Assumption)
N= 1
⇒ P (w
1
, w
2
,... , w
T
) ≈ P (w
1
)P (w
2
)... P (w
T
Unigram Language Model
or
th
order Markov Model
P (w
k
|w
1
,... , w
k− 1
) ≈ P (w
k
P (w
k
|w
1
,... , w
k− 1
) ≈ P (w
k
|w
k−N +
,... , w
k− 1
Approximating Probabilities
Basic idea: limit history to fixed number of words (N)
(Markov Assumption)
N= 3
Trigram Language Model
or
nd
order Markov Model
P (w
k
|w
1
,... , w
k− 1
) ≈ P (w
k
|w
k−N +
,... , w
k− 1
P (w
k
|w
1
,... , w
k− 1
) ≈ P (w
k
|w
k− 2
, w
k− 1
⇒ P (w 1 , w 2 ,... , wT ) ≈ P (w 1 |< S >< S >)... P (wT |wT − 2 wT − 1 )
Building N-gram Models
- Use existing English sentences to compute n-gram probability estimates (training)
- Terminology:
N = total number of words in training data ( tokens )
V = vocabulary size / number of unique words in training data ( types )
C(w 1 , ..., wk) = frequency of n-gram w 1 , ..., wk in training data
P(w 1 , ..., wk) = probability estimate for n-gram w 1 ... wk
P(wk|w 1 , ..., wk- 1 ) = conditional probability of producing wk given the history w 1 , ... wk-
Building N-gram Models
Example: Bigram Language Model I am Sam Sam I am I do not like green eggs and ham Training Corpus P(I|) = 2/3 = 0.67 P(Sam|) = 1/3 = 0. P(am|I) = 2/3 = 0.67 P(do|I) = 1/3 = 0. P(|Sam)= 1/2 = 0.50 P(Sam|am) = 1/2 = 0. ... Bigram Probability Estimates
More context, More work
- Larger N = Larger Context
Lexical co-occurrences
Local syntactic relations
However, larger N also means a more complex model
- For example, assume that we have 20,000 unique words in our training data
- Larger N has another more serious problem! Model Number of parameters to estimate Unigram 20000 Bigram (20000) 2 Trigram (20000) 3
Data Sparsity
Serious problem for practical language modeling
Becomes more severe as N increases (precision/
recall tradeoff)
Solution 1: Use larger training data
- Can’t always work; Blame Zipf ’s Law
Solution 2: Assign some non-zero probability to
unseen n-grams
Discounting
Zeros are bad for any statistical estimator
Need better estimators because MLEs give us a lot of zeros
Basic idea: Take from the rich (seen n- grams) and give to the poor (unseen n- grams)
Also called Smoothing; A distribution without zeros is smoother
Laplace’s Law: Probabilities
Unigrams PM LE (wi) = C(wi) N PLAP (wi) = C(wi) + 1 N + V Bigrams PM LE (wi, wj ) = C(wiwj ) N PLAP (wi, wj ) = C(wiwj ) + 1 N + V 2 PLAP (wj |wi) = PLAP (wi, wj ) PLAP (wi)
C(wiwj ) + 1 C(wi) + V
Laplace’s Law: Frequencies
Expected Frequency Estimates d 1 = CLAP (wi) C(wi) d 2 = CLAP (wiwj ) C(wiwj ) Relative Discount CLAP (wi) = PLAP (wi)N CLAP (wiwj ) = PLAP (wi, wj )N