NGrams 09/16/2004 Instructor: Rada Mihalcea Note: some of the material in this slide set was adapted from an NLP course taught by Bonnie Dorr at Univ.

Slides:



Advertisements
Similar presentations
1 CS 388: Natural Language Processing: N-Gram Language Models Raymond J. Mooney University of Texas at Austin.
Advertisements

N-Grams and Corpus Linguistics 6 July Linguistics vs. Engineering “But it must be recognized that the notion of “probability of a sentence” is an.
N-gram model limitations Important question was asked in class: what do we do about N-grams which were not in our training corpus? Answer given: we distribute.
SI485i : NLP Set 4 Smoothing Language Models Fall 2012 : Chambers.
Smoothing N-gram Language Models Shallow Processing Techniques for NLP Ling570 October 24, 2011.
CMSC 723 / LING 645: Intro to Computational Linguistics February 25, 2004 Lecture 5 (Dorr): Intro to Probabilistic NLP and N-grams (chap ) Prof.
CS 4705 N-Grams and Corpus Linguistics Julia Hirschberg CS 4705.
CSC 9010: Special Topics, Natural Language Processing. Spring, Matuszek & Papalaskari 1 N-Grams CSC 9010: Special Topics. Natural Language Processing.
1 N-Grams and Corpus Linguistics September 2009 Lecture #5.
NATURAL LANGUAGE PROCESSING. Applications  Classification ( spam )  Clustering ( news stories, twitter )  Input correction ( spell checking )  Sentiment.
N-Grams and Corpus Linguistics.  Regular expressions for asking questions about the stock market from stock reports  Due midnight, Sept. 29 th  Use.
1 I256: Applied Natural Language Processing Marti Hearst Sept 13, 2006.
Part II. Statistical NLP Advanced Artificial Intelligence N-Gramms Wolfram Burgard, Luc De Raedt, Bernhard Nebel, Lars Schmidt-Thieme Most slides taken.
Part II. Statistical NLP Advanced Artificial Intelligence Markov Models and N-gramms Wolfram Burgard, Luc De Raedt, Bernhard Nebel, Kristian Kersting Some.
N-Grams and Corpus Linguistics
September BASIC TECHNIQUES IN STATISTICAL NLP Word prediction n-grams smoothing.
CS 4705 Lecture 13 Corpus Linguistics I. From Knowledge-Based to Corpus-Based Linguistics A Paradigm Shift begins in the 1980s –Seeds planted in the 1950s.
Page 1 Language Modeling. Page 2 Next Word Prediction From a NY Times story... Stocks... Stocks plunged this …. Stocks plunged this morning, despite a.
CS 4705 Lecture 6 N-Grams and Corpus Linguistics.
Smoothing Bonnie Dorr Christof Monz CMSC 723: Introduction to Computational Linguistics Lecture 5 October 6, 2004.
N-gram model limitations Q: What do we do about N-grams which were not in our training corpus? A: We distribute some probability mass from seen N-grams.
1 Language Model (LM) LING 570 Fei Xia Week 4: 10/21/2009 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAAAAA A A.
N-Grams and Language Modeling
CS 4705 Lecture 15 Corpus Linguistics III. Training and Testing Probabilities come from a training corpus, which is used to design the model. –overly.
IN350: Text properties, Zipf’s Law,and Heap’s Law. Judith A. Molka-Danielsen September 12, 2002 Notes are based on Chapter 6 of the Article Collection.
Introduction to Language Models Evaluation in information retrieval Lecture 4.
CS 4705 N-Grams and Corpus Linguistics. Homework Use Perl or Java reg-ex package HW focus is on writing the “grammar” or FSA for dates and times The date.
LING 438/538 Computational Linguistics Sandiway Fong Lecture 18: 10/26.
CS 4705 N-Grams and Corpus Linguistics. Spelling Correction, revisited M$ suggests: –ngram: NorAm –unigrams: anagrams, enigmas –bigrams: begrimes –trigrams:
CS 4705 Lecture 14 Corpus Linguistics II. Relating Conditionals and Priors P(A | B) = P(A ^ B) / P(B) –Or, P(A ^ B) = P(A | B) P(B) Bayes Theorem lets.
Language Models Instructor: Paul Tarau, based on Rada Mihalcea’s original slides Note: some of the material in this slide set was adapted from an NLP course.
SI485i : NLP Set 3 Language Models Fall 2012 : Chambers.
1 Advanced Smoothing, Evaluation of Language Models.
8/27/2015CPSC503 Winter CPSC 503 Computational Linguistics Lecture 5 Giuseppe Carenini.
Natural Language Processing Lecture 6—9/17/2013 Jim Martin.
1 N-Grams and Corpus Linguistics September 6, 2012 Lecture #4.
Machine Translation Course 3 Diana Trandab ă ț Academic year:
Formal Models of Language. Slide 1 Language Models A language model an abstract representation of a (natural) language phenomenon. an approximation to.
Natural Language Processing Language Model. Language Models Formal grammars (e.g. regular, context free) give a hard “binary” model of the legal sentences.
1 LIN6932 Spring 2007 LIN6932: Topics in Computational Linguistics Hana Filip Lecture 5: N-grams.
Lecture 1, 7/21/2005Natural Language Processing1 CS60057 Speech &Natural Language Processing Autumn 2007 Lecture 7 8 August 2007.
6. N-GRAMs 부산대학교 인공지능연구실 최성자. 2 Word prediction “I’d like to make a collect …” Call, telephone, or person-to-person -Spelling error detection -Augmentative.
Information Retrieval and Web Search Text properties (Note: some of the slides in this set have been adapted from the course taught by Prof. James Allan.
Chapter 6: Statistical Inference: n-gram Models over Sparse Data
Chapter 6: N-GRAMS Heshaam Faili University of Tehran.
1 Statistical Properties for Text Rong Jin. 2 Statistical Properties of Text  How is the frequency of different words distributed?  How fast does vocabulary.
Exploring Text: Zipf’s Law and Heaps’ Law. (a) (b) (a) Distribution of sorted word frequencies (Zipf’s law) (b) Distribution of size of the vocabulary.
Chapter6. Statistical Inference : n-gram Model over Sparse Data 이 동 훈 Foundations of Statistic Natural Language Processing.
Resolving Word Ambiguities Description: After determining word boundaries, the speech recognition process matches an array of possible word sequences from.
Ngram models and the Sparcity problem. The task Find a probability distribution for the current word in a text (utterance, etc.), given what the last.
Estimating N-gram Probabilities Language Modeling.
A COMPARISON OF HAND-CRAFTED SEMANTIC GRAMMARS VERSUS STATISTICAL NATURAL LANGUAGE PARSING IN DOMAIN-SPECIFIC VOICE TRANSCRIPTION Curry Guinn Dave Crist.
Exploring Text: Zipf’s Law and Heaps’ Law. (a) (b) (a) Distribution of sorted word frequencies (Zipf’s law) (b) Distribution of size of the vocabulary.
Natural Language Processing Statistical Inference: n-grams
Statistical Properties of Text
2/29/2016CPSC503 Winter CPSC 503 Computational Linguistics Lecture 5 Giuseppe Carenini.
Statistical Methods for NLP Diana Trandab ă ț
Speech and Language Processing Lecture 4 Chapter 4 of SLP.
Language Modeling Part II: Smoothing Techniques Niranjan Balasubramanian Slide Credits: Chris Manning, Dan Jurafsky, Mausam.
Tasneem Ghnaimat. Language Model An abstract representation of a (natural) language. An approximation to real language Assume we have a set of sentences,
Language Model for Machine Translation Jang, HaYoung.
Statistical Methods for NLP
N-Grams Chapter 4 Part 2.
CSC 594 Topics in AI – Natural Language Processing
CSCI 5832 Natural Language Processing
N-Grams and Corpus Linguistics
N-Grams and Corpus Linguistics
CPSC 503 Computational Linguistics
Lecture 13 Corpus Linguistics I CS 4705.
Presentation transcript:

NGrams 09/16/2004 Instructor: Rada Mihalcea Note: some of the material in this slide set was adapted from an NLP course taught by Bonnie Dorr at Univ. of Maryland

Slide 1 Why N-grams? Build language models Determine the probability of a sequence of words Make word predictions “I’d like to make a collect ….” N-gram = use previous N-1 words to predict the next one –Also language model, LM, grammar Important for –Speech recognition –Spelling correction –Hand-writing recognition…

Slide 2 Why N-grams? Compute likelihood P([ni]|w) Unigram approach: ignores context Need to factor in context (n-gram) -Use P(need|I) instead of just P(need) -Note: P(new|I) < P(need|I) Word P(term|w) P(w) P(term|w)P(w) new neat need knee P([ni]|new)P(new) P([ni]|neat)P(neat) P([ni]|need)P(need) P([ni]|knee)P(knee)

Slide 3 Next Word Prediction From a NY Times story... Stocks plunged this …. Stocks plunged this morning, despite a cut in interest rates Stocks plunged this morning, despite a cut in interest rates by the Federal Reserve, as Wall... Stocks plunged this morning, despite a cut in interest rates by the Federal Reserve, as Wall Street began Stocks plunged this morning, despite a cut in interest rates by the Federal Reserve, as Wall Street began trading for the first time since last Tuesday's terrorist attacks.

Slide 4 Human Word Prediction Domain knowledge Syntactic knowledge Lexical knowledge

Slide 5 Claim A useful part of the knowledge needed to allow Word Prediction can be captured using simple statistical techniques. Compute: -probability of a sequence -likelihood of words co-occurring Why would we want to do this? –Rank the likelihood of sequences containing various alternative alternative hypotheses –Assess the likelihood of a hypothesis

Slide 6 Why is this useful? Speech recognition Handwriting recognition Spelling correction Machine translation systems Optical character recognizers

Slide 7 Handwriting Recognition Assume a note is given to a bank teller, which the teller reads as I have a gub. (cf. Woody Allen) NLP to the rescue …. gub is not a word gun, gum, Gus, and gull are words, but gun has a higher probability in the context of a bank

Slide 8 Real Word Spelling Errors They are leaving in about fifteen minuets to go to her house. The study was conducted mainly be John Black. Hopefully, all with continue smoothly in my absence. Can they lave him my messages? I need to notified the bank of…. He is trying to fine out.

Slide 9 For Spell Checkers Collect list of commonly substituted words piece/peace, whether/weather, their/there... Example: “On Tuesday, the whether …’’ “On Tuesday, the weather …”

Slide 10 Language Model Definition: Language model is a model that enables one to compute the probability, or likelihood, of a sentence S, P(S). Let’s look at different ways of computing P(S) in the context of Word Prediction

Slide 11 Word Prediction: Simple vs. Smart Smarter: probability of each next word is related to word frequency (unigram) – Likelihood of sentence S = P(w 1 ) × P(w 2 ) × … × P(w n ) – Assumes probability of each word is independent of probabilities of other words. Even smarter: Look at probability given previous words (N-gram) – Likelihood of sentence S = P(w 1 ) × P(w 2 |w 1 ) × … × P(w n |w n-1 ) – Assumes probability of each word is dependent on probabilities of other words. Simple: Every word follows every other word w/ equal probability (0-gram) –Assume |V| is the size of the vocabulary –Likelihood of sentence S of length n is = 1/|V| × 1/|V| … × 1/|V| –If English has 100,000 words, probability of each next word is 1/ = n times

Slide 12 Chain Rule Conditional Probability P(A 1,A 2 ) = P(A 1 ) · P(A 2 |A 1 ) The Chain Rule generalizes to multiple events P(A 1, …,A n ) = P(A 1 ) P(A 2 |A 1 ) P(A 3 |A 1,A 2 )…P(A n |A 1 …A n-1 ) Examples: P(the dog) = P(the) P(dog | the) P(the dog bites) = P(the) P(dog | the) P(bites| the dog)

Slide 13 Relative Frequencies and Conditional Probabilities Relative word frequencies are better than equal probabilities for all words In a corpus with 10K word types, each word would have P(w) = 1/10K Does not match our intuitions that different words are more likely to occur (e.g. the) Conditional probability more useful than individual relative word frequencies Dog may be relatively rare in a corpus But if we see barking, P(dog|barking) may be very large

Slide 14 For a Word String In general, the probability of a complete string of words w 1 …w n is P(w 1 n ) = P(w 1 )P(w 2 |w 1 )P(w 3 |w 1..w 2 )…P(w n |w 1 …w n-1 ) = But this approach to determining the probability of a word sequence is not very helpful in general – gets to be computationally very expensive

Slide 15 Markov Assumption How do we compute P(w n |w 1 n-1 )? Trick: Instead of P(rabbit|I saw a), we use P(rabbit|a). This lets us collect statistics in practice A bigram model: P(the barking dog) = P(the| )P(barking|the)P(dog|barking) Markov models are the class of probabilistic models that assume that we can predict the probability of some future unit without looking too far into the past Specifically, for N=2 (bigram): P(w 1 n ) ≈ Π k=1 n P(w k |w k-1 ) Order of a Markov model: length of prior context bigram is first order, trigram is second order, …

Slide 16 Counting Words in Corpora What is a word? e.g., are cat and cats the same word? September and Sept? zero and oh? Is seventy-two one word or two? AT&T? Punctuation? How many words are there in English? Where do we find the things to count?

Slide 17 Corpora Corpora are (generally online) collections of text and speech Examples: Brown Corpus (1M words) Wall Street Journal and AP News corpora ATIS, Broadcast News (speech) TDT (text and speech) Switchboard, Call Home (speech) TRAINS, FM Radio (speech) Compiled corpora (for specific tasks) TREC (500 mil. words) – for Information Retrieval evaluations WSJ, AP, ATIS, … British National Corpus (100 mil. words) – balanced corpus

Slide 18 Training and Testing Probabilities come from a training corpus, which is used to design the model. overly narrow corpus: probabilities don't generalize overly general corpus: probabilities don't reflect task or domain A separate test corpus is used to evaluate the model, typically using standard metrics held out test set cross validation evaluation differences should be statistically significant

Slide 19 Terminology Sentence: unit of written language Utterance: unit of spoken language Word Form: the inflected form that appears in the corpus Lemma: lexical forms having the same stem, part of speech, and word sense Types (V): number of distinct words that might appear in a corpus (vocabulary size) Tokens (N): total number of words in a corpus Types seen so far (T): number of distinct words seen so far in corpus (smaller than V and N)

Slide 20 Simple N-Grams An N-gram model uses the previous N-1 words to predict the next one: P(w n | w n-N+1 w n-N+2… w n-1 ) unigrams: P(dog) bigrams: P(dog | big) trigrams: P(dog | the big) quadrigrams: P(dog | chasing the big)

Slide 21 Using N-Grams Recall that N-gram: P(w n |w 1 n-1 ) ≈ P(w n |w n-N+1 n-1 ) Bigram: P(w 1 n ) ≈ Π P(w k |w k-1 ) For a bigram grammar P(sentence) can be approximated by multiplying all the bigram probabilities in the sequence Example: P(I want to eat Chinese food) = P(I | ) P(want | I) P(to | want) P(eat | to) P(Chinese | eat) P(food | Chinese)

Slide 22 A Bigram Grammar Fragment Eat on.16Eat Thai.03 Eat some.06Eat breakfast.03 Eat lunch.06Eat in.02 Eat dinner.05Eat Chinese.02 Eat at.04Eat Mexican.02 Eat a.04Eat tomorrow.01 Eat Indian.04Eat dessert.007 Eat today.03Eat British.001

Slide 23 Additional Grammar I.25Want some.04 I’d.06Want Thai.01 Tell.04To eat.26 I’m.02To have.14 I want.32To spend.09 I would.29To be.02 I don’t.08British food.60 I have.04British restaurant.15 Want to.65British cuisine.01 Want a.05British lunch.01

Slide 24 Computing Sentence Probability P(I want to eat British food) = P(I| ) P(want|I) P(to|want) P(eat|to) P(British|eat) P(food|British) =.25×.32×.65×.26×.001×.60 = vs. P(I want to eat Chinese food) = Probabilities seem to capture “syntactic'' facts, “world knowledge'' -eat is often followed by a NP -British food is not too popular N-gram models can be trained by counting and normalization

Slide 25 N-grams issues Sparse data –Not all n-grams found in training data, need smoothing Change of domain –Train on WSJ, attempt to identify Shakespeare – won’t work N-grams more reliable than (N-1)-grams –But even more sparse –Generating Shakespeare sentences with random unigrams... -Every enter now severally so, let -Hill he late speaks; or! a more to leg less first you enter -With bigrams... -What means, sir. I confess she? then all sorts, he is trim, captain. -Why dost stand forth thy canopy, forsooth; he is this palpable hit the King Henry. -Trigrams -Sweet prince, Falstaff shall die. -This shall forbid it should be branded, if renown made it empty. -Quadrigrams -What! I will go seek the traitor Gloucester. -Will you not tell me who I am? (King Lear)

Slide 26 Example of bad language model

Slide 27 A bad language model

Slide 28 A bad language model

Slide 29 A Good Language Model Determine reliable sentence probability estimates –should have smoothing capabilities (avoid the zero-counts) –apply back-off strategies: if N-grams are not possible, back-off to (N-1) grams P(“And nothing but the truth”)  P(“And nuts sing on the roof”)  0

Slide 30 Bigram Counts IWantToEatChineseFoodlunch I Want To Eat Chinese Food Lunch

Slide 31 Bigram Probabilities: Use Unigram Count Normalization: divide bigram count by unigram count of first word. Computing the probability of I I P(I|I) = C(I I)/C(I) = 8 / 3437 =.0023 A bigram grammar is an VxV matrix of probabilities, where V is the vocabulary size IWantToEatChineseFoodLunch

Slide 32 Learning a Bigram Grammar The formula P(w n |w n-1 ) = C(w n-1 w n )/C(w n-1 ) is used for bigram “parameter estimation”

Slide 33 Smoothing Techniques Every ngram training matrix is sparse, even for very large corpora (Zipf’s law ) Solution: estimate the likelihood of unseen ngrams

Slide 34 Add-one smoothing –Add 1 to every ngram count –Normalize by N/(N+V) –Smoothed count is: –Smoothed probability: P′(w i ) = c i ′/N c i ′=(c i +1)

Slide 35 Add-one smoothed bigrams P(w n |w n-1 ) = C(w n-1 w n )/C(w n-1 ) P ′ (w n |w n-1 ) = [C(w n-1 w n )+1]/[C(w n-1 )+V]

Slide 36 Add-one smoothing flaw c i ′=(c i +1) cici N values I 3437 want1215 to3256 eat938 Chinese213 food1506 lunch459 V = 1616

Slide 37 Discounted smoothing: Witten-Bell Witten-Bell Discounting –A zero ngram is just an ngram you haven’t seen yet… –Model unseen bigrams by the ngrams you’ve only seen once: total number of n-gram types in the data. –Total probability of unseen bigrams estimated as the probability of things we’ve already seen We can divide the probability mass equally among unseen bigrams. Let Z = number of unseen n-grams: c i ′ = T/Z · if c i =0; else c i ′ = c i · -T is the number of types we’ve already seen (conditioned on previous word) -N is the total number of bigram tokens -Z is the number of types with count zero

Slide 38 Witten-Bell Bigram Counts c i ′=(c i +1) c i ′ = T/Z · if c i =0 c i · otherwise cici T & N values I want to eat Chinese20213 food lunch45459 V = 1616

Slide 39 Other smoothing methods: Good-Turing Imagine you are fishing You have caught 10 Carp, 3 Cod, 2 tuna, 1 trout, 1 salmon, 1 eel. How likely is it that next species is new? 3/18 How likely is it that next is tuna? Less than 2/18

Slide 40 Smoothing: Good Turing How many species (words) were seen once? Estimate for how many are unseen. All other estimates are adjusted (down) to give probabilities for unseen

Slide 41 Smoothing: Good Turing Example 10 Carp, 3 Cod, 2 tuna, 1 trout, 1 salmon, 1 eel. How likely is new data (p 0 ). Let n 1 be number occurring once (3), N be total (18). p 0 =3/18 How likely is eel? 1 * n 1 =3, n 2 =1 1 * =2  1/3 = 2/3 P(eel) = 1 * /N = (2/3)/18 = 1/27

Slide 42 Back-off methods Notice that: –N-grams are more precise than (N-1)grams (remember the Shakespeare example) –But also, N-grams are more sparse than (N-1) grams How to combine things? –Attempt N-grams and back-off to (N-1) if counts are not available –E.g. attempt prediction using 4-grams, and back-off to trigrams (or bigrams, or unigrams!) if counts are not available

Slide 43 Some Useful Empirical Observations -A small number of events occur with high frequency -A large number of events occur with low frequency -You can quickly collect statistics on the high frequency events

Slide 44 Text properties (formalized) Sample word frequency data

Slide 45 Zipf’s Law Rank (r): The numerical position of a word in a list sorted by decreasing frequency (f ). Zipf (1949) “discovered” that: If probability of word of rank r is p r and N is the total number of word occurrences:

Slide 46 Zipf curve

Slide 47 Predicting Occurrence Frequencies By Zipf, a word appearing n times has rank r n =AN/n If several words may occur n times, assume rank r n applies to the last of these. Therefore, r n words occur n or more times and r n+1 words occur n+1 or more times. So, the number of words appearing exactly n times is: Fraction of words with frequency n is: Fraction of words appearing only once is therefore ½.

Slide 48 Explanations for Zipf’s Law -Zipf’s explanation was his “principle of least effort.” Balance between speaker’s desire for a small vocabulary and hearer’s desire for a large one. -Li (1992) shows that just random typing of letters including a space will generate “words” with a Zipfian distribution.

Slide 49 Zipf’s Law Impact on Language Analysis Good News: Stopwords will account for a large fraction of text so eliminating them greatly reduces size of vocabulary in a text Bad News: For most words, gathering sufficient data for meaningful statistical analysis (e.g. for correlation analysis for query expansion) is difficult since they are extremely rare.

Slide 50 Vocabulary Growth How does the size of the overall vocabulary (number of unique words) grow with the size of the corpus? This determines how the size of the inverted index will scale with the size of the corpus. Vocabulary not really upper-bounded due to proper names, typos, etc.

Slide 51 Heaps’ Law If V is the size of the vocabulary and the n is the length of the corpus in words: Typical constants: K  10  100   0.4  0.6 (approx. square-root)

Slide 52 Heaps’ Law Data