sadf

1,277 views
1,217 views

Published on

sdf

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,277
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • sadf

    1. 1. CS60057 Speech &Natural Language Processing Autumn 2007 Lecture 5 2 August 2007
    2. 2. WORDS The Building Blocks of Language
    3. 3. <ul><li>Language can be divided up into pieces of varying sizes, ranging from morphemes to paragraphs. </li></ul><ul><li>Words -- the most fundamental level for NLP. </li></ul>
    4. 4. Tokens, Types and Texts <ul><li>This process of segmenting a string of characters into words is known as tokenization. </li></ul><ul><li>>>> sentence = &quot;This is the time -- and this is the record of the time.&quot; </li></ul><ul><li>>>> words = sentence.split() </li></ul><ul><li>>>> len(words) </li></ul><ul><li>13 </li></ul><ul><li>Compile a list of the unique vocabulary items in a string by using set() to eliminate duplicates </li></ul><ul><li>>>> len(set(words)) </li></ul><ul><li>10 </li></ul><ul><li>A word token is an individual occurrence of a word in a concrete context. </li></ul><ul><li>A word type is what we're talking about when we say that the three occurrences of the in sentence are &quot;the same word.&quot; </li></ul>
    5. 5. <ul><li>>>> set(words) </li></ul><ul><li>set(['and', 'this', 'record', 'This', 'of', 'is', '--', 'time.', 'time', 'the'] </li></ul><ul><li>Extracting text from files </li></ul><ul><li>>>> f = open('corpus.txt', 'rU') </li></ul><ul><li>>>> f.read() </li></ul><ul><li>'Hello World! This is a test file. ' </li></ul><ul><li>We can also read a file one line at a time using the for loop construct: </li></ul><ul><li>  >>> f = open('corpus.txt', 'rU') </li></ul><ul><li>>>> for line in f: </li></ul><ul><li>... print line[:-1] </li></ul><ul><li>Hello world! </li></ul><ul><li>This is a test file. </li></ul><ul><li>Here we use the slice [:-1] to remove the newline character at the end of the input line. </li></ul>
    6. 6. Extracting text from the Web <ul><li>>>> from urllib import urlopen </li></ul><ul><li>>>> page = urlopen(&quot;http://news.bbc.co.uk/&quot;).read() </li></ul><ul><li>>>> print page[:60] </li></ul><ul><li><!doctype html public &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot; </li></ul><ul><li>Web pages are usually in HTML format. To extract the text, we need to strip out the HTML markup, i.e. remove all material enclosed in angle brackets. Let's digress briefly to consider how to carry out this task using regular expressions. Our first attempt might look as follows: </li></ul><ul><li>>>> line = '<title>BBC NEWS | News Front Page</title>‘ </li></ul><ul><li>>>> new = re.sub(r'<.*>', '', line) </li></ul><ul><li>>>> new </li></ul><ul><li>‘ ' </li></ul>
    7. 7. <ul><li>What has happened here? </li></ul><ul><li>The wildcard '.' matches any character other than ' ', so it will match '>' and '<'. </li></ul><ul><li>The '*' operator is &quot;greedy&quot;, it matches as many characters as it can. In the above example, '.*' will return not the shortest match, namely 'title', but the longest match, 'title>BBC NEWS | News Front Page</title'. To get the shortest match we have to use the '*?' operator. We will also normalise whitespace, replacing any sequence of one or more spaces, tabs or newlines (these are all matched by 's+') with a single space character: </li></ul><ul><li>>>> page = re.sub('<.*?>', '', page) </li></ul><ul><li>>>> page = re.sub('s+', ' ', page) </li></ul><ul><li>>>> print page[:60] </li></ul><ul><li>BBC NEWS | News Front Page News Sport Weather World Service </li></ul>
    8. 8. Extracting text from NLTK Corpora <ul><li>NLTK is distributed with several corpora and corpus samples and many are supported by the corpus package. </li></ul><ul><li>>>> corpus.gutenberg.items </li></ul><ul><li>['austen-emma', 'austen-persuasion', 'austen-sense', 'bible-kjv', 'blake-poems', 'blake-songs', 'chesterton-ball', 'chesterton-brown', 'chesterton-thursday', 'milton-paradise', 'shakespeare-caesar', 'shakespeare-hamlet', 'shakespeare-macbeth', 'whitman-leaves'] </li></ul><ul><li>Next we iterate over the text content to find the number of word tokens: </li></ul><ul><li>>>> count = 0 </li></ul><ul><li>>>> for word in corpus.gutenberg.read('whitman-leaves'): </li></ul><ul><li>... count += 1 </li></ul><ul><li>>>> print count </li></ul><ul><li>154873 </li></ul>
    9. 9. Brown Corpus <ul><li>The Brown Corpus was the first million-word, part-of-speech tagged electronic corpus of English, created in 1961 at Brown University. Each of the sections a through r represents a different genre. </li></ul><ul><li>>>> corpus.brown.items </li></ul><ul><li>['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'r'] </li></ul><ul><li>>>> corpus.brown.documents['a'] </li></ul><ul><li>'press: reportage' </li></ul><ul><li>We can extract individual sentences (as lists of words) from the corpus using the read() function. Here we will specify section a, and indicate that only words (and not part-of-speech tags) should be produced. </li></ul><ul><li>>>> a = corpus.brown.tokenized('a') </li></ul><ul><li>>>> a[0] </li></ul><ul><li>['The', 'Fulton', 'County', 'Grand', 'Jury', 'said', 'Friday', 'an', 'investigation', 'of', &quot;Atlanta's&quot;, 'recent', 'primary', 'election', 'produced', '``', 'no', 'evidence', &quot;''&quot;, 'that', 'any', 'irregularities', 'took', 'place', '.'] </li></ul>
    10. 11. Corpus Linguistics <ul><li>1. Text-corpora: Brown corpus. One million words, tagged, representative of American English. </li></ul><ul><li>2. Text-corpora: Project Gutenberg. 17,000 uncopyrighted literary texts (Tom Sawyer, etc.) </li></ul><ul><li>3. Text-corpora: OMIM: Comprehensive list of medical conditions. </li></ul><ul><li>4. Word frequencies. </li></ul><ul><li>5. Zipf's First Law. </li></ul>
    11. 12. What’s a word? <ul><li>I have a can opener; but I can’t open these cans. </li></ul><ul><li>how many words? </li></ul><ul><li>Word form </li></ul><ul><ul><li>inflected form as it appears in the text </li></ul></ul><ul><ul><li>can and cans ... different word forms </li></ul></ul><ul><li>Lemma </li></ul><ul><ul><li>a set of lexical forms having the same stem, same POS and same meaning </li></ul></ul><ul><ul><li>can and cans … same lemma </li></ul></ul><ul><li>Word token: </li></ul><ul><ul><li>an occurrence of a word </li></ul></ul><ul><ul><li>I have a can opener; but I can’t open these cans. 11 word tokens (not counting punctuation) </li></ul></ul><ul><li>Word type: </li></ul><ul><ul><li>a different realization of a word </li></ul></ul><ul><ul><li>I have a can opener; but I can’t open these cans. 10 word types (not counting punctuation) </li></ul></ul>
    12. 13. Another example <ul><li>Mark Twain’s Tom Sawyer </li></ul><ul><ul><li>71,370 word tokens </li></ul></ul><ul><ul><li>8,018 word types </li></ul></ul><ul><ul><li>tokens/type ratio = 8.9 (indication of text complexity) </li></ul></ul><ul><li>Complete Shakespeare work </li></ul><ul><ul><li>884,647 word tokens </li></ul></ul><ul><ul><li>29,066 word types </li></ul></ul><ul><ul><li>tokens/type ratio = 30.4 </li></ul></ul>
    13. 14. Some Useful Empirical Observations <ul><li>A small number of events occur with high frequency </li></ul><ul><li>A large number of events occur with low frequency </li></ul><ul><li>You can quickly collect statistics on the high frequency events </li></ul><ul><li>You might have to wait an arbitrarily long time to get valid statistics on low frequency events </li></ul><ul><li>Some of the zeroes in the table are really zeros But others are simply low frequency events you haven't seen yet. How to address? </li></ul>
    14. 15. Common words in Tom Sawyer but words in NL have an uneven distribution…
    15. 16. Text properties (formalized) Sample word frequency data
    16. 17. Frequency of frequencies <ul><li>most words are rare </li></ul><ul><ul><li>3993 (50%) word types appear only once </li></ul></ul><ul><ul><li>they are called happax legomena ( read only once ) </li></ul></ul><ul><li>but common words are very common </li></ul><ul><ul><li>100 words account for 51% of all tokens (of all text) </li></ul></ul>
    17. 18. Zipf’s Law <ul><li>Count the frequency of each word type in a large corpus </li></ul><ul><li>List the word types in order of their frequency </li></ul><ul><li>Let: </li></ul><ul><ul><li>f = frequency of a word type </li></ul></ul><ul><ul><li>r = its rank in the list </li></ul></ul><ul><li>Zipf’s Law says: f  1/r </li></ul><ul><li>In other words: </li></ul><ul><ul><li>there exists a constant k such that: f × r = k </li></ul></ul><ul><ul><li>The 50 th most common word should occur with 3 times the frequency of the 150 th most common word. </li></ul></ul>
    18. 19. Zipf’s Law <ul><li>If probability of word of rank r is p r and N is the total number of word occurrences: </li></ul>
    19. 20. Zipf curve
    20. 21. Predicting Occurrence Frequencies <ul><li>By Zipf, a word appearing n times has rank r n = AN/n </li></ul><ul><li>If several words may occur n times, assume rank r n applies to the last of these. </li></ul><ul><li>Therefore, r n words occur n or more times and r n+ 1 words occur n+ 1 or more times. </li></ul><ul><li>So, the number of words appearing exactly n times is: </li></ul>Fraction of words with frequency n is: Fraction of words appearing only once is therefore ½.
    21. 22. Explanations for Zipf’s Law <ul><li>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></ul>
    22. 23. Zipf’s First Law <ul><li>1. f ∝ 1/r , </li></ul><ul><li>f = word-frequency, </li></ul><ul><li>r = word-frequency rank, </li></ul><ul><li>m = number of meetings per word. </li></ul><ul><li>2. There exists a k such that f × r = k. </li></ul><ul><li>3. Alternatively, log f = log k - log r. </li></ul><ul><li>4. English literature, Johns Hopkins Autopsy Resource, German, and Chinese. </li></ul><ul><li>5. Most famous of Zipf’s Laws. </li></ul>
    23. 24. Zipf’s Second Law <ul><li>1. Meanings, m ∝ √f </li></ul><ul><li>2. There exists a k such that k × f = m 2 . </li></ul><ul><li>3. Corollary: m ∝ 1/√r </li></ul>
    24. 25. Zipf’s Third Law <ul><li>1. Frequency ∝ 1/wordlength: </li></ul><ul><li>2. There exists a k such that f × wordlength = k. </li></ul><ul><li>3. Many other minor laws stated. </li></ul>
    25. 26. Zipf’s Law Impact on Language Analysis <ul><li>Good News : Stopwords will account for a large fraction of text so eliminating them greatly reduces size of vocabulary in a text </li></ul><ul><li>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. </li></ul>
    26. 27. Vocabulary Growth <ul><li>How does the size of the overall vocabulary (number of unique words) grow with the size of the corpus? </li></ul><ul><li>This determines how the size of the inverted index will scale with the size of the corpus. </li></ul><ul><li>Vocabulary not really upper-bounded due to proper names, typos, etc. </li></ul>
    27. 28. Heaps’ Law <ul><li>If V is the size of the vocabulary and the n is the length of the corpus in words: </li></ul><ul><li>Typical constants: </li></ul><ul><ul><li>K  10  100 </li></ul></ul><ul><ul><li>  0.4  0.6 (approx. square-root ) </li></ul></ul>
    28. 29. Heaps’ Law Data
    29. 30. Word counts are interesting... <ul><li>As an indication of a text’s style </li></ul><ul><li>As an indication of a text’s author </li></ul><ul><li>But, because most words appear very infrequently, </li></ul><ul><ul><li>it is hard to predict much about the behavior of words (if they do not occur often in a corpus) </li></ul></ul><ul><li>--> Zipf’s Law </li></ul>
    30. 31. Zipf’s Law on Tom Saywer <ul><li>k ≈ 8000-9000 </li></ul><ul><li>except for </li></ul><ul><ul><li>The 3 most frequent words </li></ul></ul><ul><ul><li>Words of frequency ≈ 100 </li></ul></ul>
    31. 32. Plot of Zipf’s Law <ul><li>On chap. 1-3 of Tom Sawyer (≠ numbers from p. 25&26) </li></ul><ul><li>f×r = k </li></ul>
    32. 33. Plot of Zipf’s Law (con’t) <ul><li>On chap. 1-3 of Tom Sawyer </li></ul><ul><li>f×r = k ==> log(f×r) = log(k) ==> log(f)+log(r) = log(k) </li></ul>
    33. 34. Zipf’s Law, so what? <ul><li>There are: </li></ul><ul><ul><li>A few very common words </li></ul></ul><ul><ul><li>A medium number of medium frequency words </li></ul></ul><ul><ul><li>A large number of infrequent words </li></ul></ul><ul><li>Principle of Least effort: Tradeoff between speaker and hearer’s effort </li></ul><ul><ul><li>Speaker communicates with a small vocabulary of common words (less effort) </li></ul></ul><ul><ul><li>Hearer disambiguates messages through a large vocabulary of rare words (less effort) </li></ul></ul><ul><li>Significance of Zipf’s Law for us: </li></ul><ul><ul><li>For most words, our data about their use will be very sparse </li></ul></ul><ul><ul><li>Only for a few words will we have a lot of examples </li></ul></ul>
    34. 35. N-Grams and Corpus Linguistics
    35. 36. A bad language model N-grams & Language Modeling
    36. 37. A bad language model
    37. 38. A bad language model Herman is reprinted with permission from LaughingStock Licensing Inc., Ottawa Canada. All rights reserved.
    38. 39. What’s a Language Model <ul><li>A Language model is a probability distribution over word sequences </li></ul><ul><li>P(“And nothing but the truth”)  0.001 </li></ul><ul><li>P(“And nuts sing on the roof”)  0 </li></ul>
    39. 40. What’s a language model for? <ul><li>Speech recognition </li></ul><ul><li>Handwriting recognition </li></ul><ul><li>Spelling correction </li></ul><ul><li>Optical character recognition </li></ul><ul><li>Machine translation </li></ul><ul><li>(and anyone doing statistical modeling) </li></ul>
    40. 41. Next Word Prediction <ul><li>From a NY Times story... </li></ul><ul><ul><li>Stocks ... </li></ul></ul><ul><ul><li>Stocks plunged this …. </li></ul></ul><ul><ul><li>Stocks plunged this morning, despite a cut in interest rates </li></ul></ul><ul><ul><li>Stocks plunged this morning, despite a cut in interest rates by the Federal Reserve, as Wall ... </li></ul></ul><ul><ul><li>Stocks plunged this morning, despite a cut in interest rates by the Federal Reserve, as Wall Street began </li></ul></ul>
    41. 42. <ul><ul><li>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 … </li></ul></ul><ul><ul><li>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. </li></ul></ul>
    42. 43. Human Word Prediction <ul><li>Clearly, at least some of us have the ability to predict future words in an utterance. </li></ul><ul><li>How? </li></ul><ul><ul><li>Domain knowledge </li></ul></ul><ul><ul><li>Syntactic knowledge </li></ul></ul><ul><ul><li>Lexical knowledge </li></ul></ul>
    43. 44. Claim <ul><li>A useful part of the knowledge needed to allow Word Prediction can be captured using simple statistical techniques </li></ul><ul><li>In particular, we'll rely on the notion of the probability of a sequence (a phrase, a sentence) </li></ul>
    44. 45. Applications <ul><li>Why do we want to predict a word, given some preceding words? </li></ul><ul><ul><li>Rank the likelihood of sequences containing various alternative hypotheses, e.g. for ASR </li></ul></ul><ul><ul><li>Theatre owners say popcorn/unicorn sales have doubled... </li></ul></ul><ul><ul><li>Assess the likelihood/goodness of a sentence, e.g. for text generation or machine translation </li></ul></ul><ul><ul><li>The doctor recommended a cat scan. </li></ul></ul><ul><ul><li>El doctor recommendó una exploración del gato. </li></ul></ul>
    45. 46. Overview <ul><li>N-grams </li></ul><ul><ul><li>Smoothing </li></ul></ul><ul><ul><li>Backoff </li></ul></ul><ul><ul><li>Caching </li></ul></ul><ul><ul><li>Skipping </li></ul></ul><ul><li>Beyond N-grams </li></ul><ul><ul><li>Parsing </li></ul></ul><ul><ul><li>Trigger Words </li></ul></ul>
    46. 47. Simple N-Grams <ul><li>Assume a language has V word types in its lexicon, how likely is word x to follow word y? </li></ul><ul><ul><li>Simplest model of word probability: 1/V </li></ul></ul><ul><ul><li>Alternative 1: estimate likelihood of x occurring in new text based on its general frequency of occurrence estimated from a corpus ( unigram probability) </li></ul></ul><ul><ul><ul><li>popcorn is more likely to occur than unicorn </li></ul></ul></ul><ul><ul><li>Alternative 2: condition the likelihood of x occurring in the context of previous words (bigrams, trigrams,…) </li></ul></ul><ul><ul><ul><li>mythical unicorn is more likely than mythical popcorn </li></ul></ul></ul>
    47. 48. N-grams <ul><li>A simple model of language </li></ul><ul><li>Computes a probability for observed input. </li></ul><ul><li>Probability is the likelihood of the observation being generated by the same source as the training data </li></ul><ul><li>Such a model is often called a language model </li></ul>
    48. 49. Computing the Probability of a Word Sequence <ul><li>P (w 1 , …, w n ) = </li></ul><ul><li>P (w 1 ). P( w 2 |w 1 ).P( w 3 |w 1 ,w 2 ). … P( w n |w 1 , …,w n-1 ) P( the mythical unicorn ) = P( the ) P( mythical | the ) P( unicorn | the mythical ) </li></ul><ul><li>The longer the sequence, the less likely we are to find it in a training corpus </li></ul><ul><ul><ul><li>P( Most biologists and folklore specialists believe that in fact the mythical unicorn horns derived from the narwhal ) </li></ul></ul></ul><ul><li>Solution: approximate using n-grams </li></ul>
    49. 50. Bigram Model <ul><li>Approximate by </li></ul><ul><ul><li>P( unicorn | the mythical ) by P( unicorn | mythical ) </li></ul></ul><ul><li>Markov assumption : the probability of a word depends only on the probability of a limited history </li></ul><ul><li>Generalization: the probability of a word depends only on the probability of the n previous words </li></ul><ul><ul><li>trigrams, 4-grams, … </li></ul></ul><ul><ul><li>the higher n is, the more data needed to train </li></ul></ul><ul><ul><li>backoff models </li></ul></ul>
    50. 51. Using N-Grams <ul><li>For N-gram models </li></ul><ul><ul><li> </li></ul></ul><ul><ul><li>P(w n-1 ,w n ) = P(w n | w n-1 ) P(w n-1 ) </li></ul></ul><ul><ul><li>By the Chain Rule we can decompose a joint probability, e.g. P(w 1 ,w 2 ,w 3 ) </li></ul></ul><ul><ul><ul><li>P(w 1 ,w 2 , ...,w n ) = P(w 1 |w 2 ,w 3 ,...,w n ) P(w 2 |w 3 , ...,w n ) … P(w n-1 |w n ) P(w n ) </li></ul></ul></ul><ul><ul><ul><li>For bigrams then, the probability of a sequence is just the product of the conditional probabilities of its bigrams </li></ul></ul></ul><ul><ul><ul><li>P( the,mythical,unicorn ) = P( unicorn | mythical ) P( mythical | the ) P( the|<start> ) </li></ul></ul></ul>
    51. 52. The n -gram Approximation <ul><li>Assume each word depends only on the previous (n- 1) words ( n words total) </li></ul><ul><li>For example for trigrams (3-grams): </li></ul><ul><li>P(“the|… whole truth and nothing but”) </li></ul><ul><li> P(“the|nothing but”) </li></ul><ul><li>P(“truth|… whole truth and nothing but the”)  P(“truth|but the”) </li></ul>
    52. 53. n- grams, continued <ul><li>How do we find probabilities? </li></ul><ul><li>Get real text, and start counting! </li></ul><ul><ul><li>P(“the | nothing but”)  </li></ul></ul><ul><ul><li>C(“nothing but the”) / C(“nothing but”) </li></ul></ul>
    53. 54. <ul><li>Unigram probabilities (1-gram) </li></ul><ul><ul><li>http://www.wordcount.org/main.php </li></ul></ul><ul><ul><li>Most likely to transition to “the”, least likely to transition to “conquistador”. </li></ul></ul><ul><li>Bigram probabilities (2-gram) </li></ul><ul><ul><li>Given “the” as the last word, more likely to go to “conquistador” than to “the” again. </li></ul></ul>
    54. 55. N-grams for Language Generation <ul><li>C. E. Shannon, ``A mathematical theory of communication,'' Bell System Technical Journal, vol. 27, pp. 379-423 and 623-656, July and October, 1948. </li></ul>Unigram: 5. …Here words are chosen independently but with their appropriate frequencies. REPRESENTING AND SPEEDILY IS AN GOOD APT OR COME CAN DIFFERENT NATURAL HERE HE THE A IN CAME THE TO OF TO EXPERT GRAY COME TO FURNISHES THE LINE MESSAGE HAD BE THESE. Bigram: 6. Second-order word approximation. The word transition probabilities are correct but no further structure is included. THE HEAD AND IN FRONTAL ATTACK ON AN ENGLISH WRITER THAT THE CHARACTER OF THIS POINT IS THEREFORE ANOTHER METHOD FOR THE LETTERS THAT THE TIME OF WHO EVER TOLD THE PROBLEM FOR AN UNEXPECTED.
    55. 56. N-Gram Models of Language <ul><li>Use the previous N-1 words in a sequence to predict the next word </li></ul><ul><li>Language Model (LM) </li></ul><ul><ul><li>unigrams, bigrams, trigrams,… </li></ul></ul><ul><li>How do we train these models? </li></ul><ul><ul><li>Very large corpora </li></ul></ul>
    56. 57. Counting Words in Corpora <ul><li>What is a word? </li></ul><ul><ul><li>e.g., are cat and cats the same word? </li></ul></ul><ul><ul><li>September and Sept ? </li></ul></ul><ul><ul><li>zero and oh ? </li></ul></ul><ul><ul><li>Is _ a word? * ? ‘ ( ‘ ? </li></ul></ul><ul><ul><li>How many words are there in don’t ? Gonna ? </li></ul></ul><ul><ul><li>In Japanese and Chinese text -- how do we identify a word? </li></ul></ul>
    57. 58. Terminology <ul><li>Sentence : unit of written language </li></ul><ul><li>Utterance : unit of spoken language </li></ul><ul><li>Word Form : the inflected form that appears in the corpus </li></ul><ul><li>Lemma : an abstract form, shared by word forms having the same stem , part of speech, and word sense </li></ul><ul><li>Types : number of distinct words in a corpus (vocabulary size) </li></ul><ul><li>Tokens : total number of words </li></ul>
    58. 59. Corpora <ul><li>Corpora are online collections of text and speech </li></ul><ul><ul><li>Brown Corpus </li></ul></ul><ul><ul><li>Wall Street Journal </li></ul></ul><ul><ul><li>AP news </li></ul></ul><ul><ul><li>Hansards </li></ul></ul><ul><ul><li>DARPA/NIST text/speech corpora (Call Home, ATIS, switchboard, Broadcast News, TDT, Communicator) </li></ul></ul><ul><ul><li>TRAINS, Radio News </li></ul></ul>
    59. 60. Simple N-Grams <ul><li>Assume a language has V word types in its lexicon, how likely is word x to follow word y? </li></ul><ul><ul><li>Simplest model of word probability: 1/V </li></ul></ul><ul><ul><li>Alternative 1: estimate likelihood of x occurring in new text based on its general frequency of occurrence estimated from a corpus ( unigram probability) </li></ul></ul><ul><ul><ul><li>popcorn is more likely to occur than unicorn </li></ul></ul></ul><ul><ul><li>Alternative 2: condition the likelihood of x occurring in the context of previous words (bigrams, trigrams,…) </li></ul></ul><ul><ul><ul><li>mythical unicorn is more likely than mythical popcorn </li></ul></ul></ul>
    60. 61. Computing the Probability of a Word Sequence <ul><li>Compute the product of component conditional probabilities? </li></ul><ul><ul><li>P( the mythical unicorn ) = P( the ) P( mythical | the ) P( unicorn | the mythical ) </li></ul></ul><ul><li>The longer the sequence, the less likely we are to find it in a training corpus </li></ul><ul><ul><ul><li>P( Most biologists and folklore specialists believe that in fact the mythical unicorn horns derived from the narwhal ) </li></ul></ul></ul><ul><li>Solution: approximate using n-grams </li></ul>
    61. 62. Bigram Model <ul><li>Approximate by </li></ul><ul><ul><li>P( unicorn | the mythical ) by P( unicorn | mythical ) </li></ul></ul><ul><li>Markov assumption : the probability of a word depends only on the probability of a limited history </li></ul><ul><li>Generalization: the probability of a word depends only on the probability of the n previous words </li></ul><ul><ul><li>trigrams, 4-grams, … </li></ul></ul><ul><ul><li>the higher n is, the more data needed to train </li></ul></ul><ul><ul><li>backoff models </li></ul></ul>
    62. 63. Using N-Grams <ul><li>For N-gram models </li></ul><ul><ul><li> </li></ul></ul><ul><ul><li>P(w n-1 ,w n ) = P(w n | w n-1 ) P(w n-1 ) </li></ul></ul><ul><ul><li>By the Chain Rule we can decompose a joint probability, e.g. P(w 1 ,w 2 ,w 3 ) </li></ul></ul><ul><ul><ul><li>P(w 1 ,w 2 , ...,w n ) = P(w 1 |w 2 ,w 3 ,...,w n ) P(w 2 |w 3 , ...,w n ) … P(w n-1 |w n ) P(w n ) </li></ul></ul></ul><ul><ul><ul><li>For bigrams then, the probability of a sequence is just the product of the conditional probabilities of its bigrams </li></ul></ul></ul><ul><ul><ul><li>P( the,mythical,unicorn ) = P( unicorn | mythical ) P( mythical | the ) P( the|<start> ) </li></ul></ul></ul>
    63. 64. Training and Testing <ul><li>N-Gram probabilities come from a training corpus </li></ul><ul><ul><li>overly narrow corpus: probabilities don't generalize </li></ul></ul><ul><ul><li>overly general corpus: probabilities don't reflect task or domain </li></ul></ul><ul><li>A separate test corpus is used to evaluate the model, typically using standard metrics </li></ul><ul><ul><li>held out test set; development test set </li></ul></ul><ul><ul><li>cross validation </li></ul></ul><ul><ul><li>results tested for statistical significance </li></ul></ul>
    64. 65. A Simple Example <ul><ul><li>P(I want to each Chinese food) = </li></ul></ul><ul><ul><li>P(I | <start>) P(want | I) P(to | want) P(eat | to) P(Chinese | eat) P(food | Chinese) </li></ul></ul>
    65. 66. A Bigram Grammar Fragment from BERP .001 Eat British .03 Eat today .007 Eat dessert .04 Eat Indian .01 Eat tomorrow .04 Eat a .02 Eat Mexican .04 Eat at .02 Eat Chinese .05 Eat dinner .02 Eat in .06 Eat lunch .03 Eat breakfast .06 Eat some .03 Eat Thai .16 Eat on
    66. 67. .01 British lunch .05 Want a .01 British cuisine .65 Want to .15 British restaurant .04 I have .60 British food .08 I don’t .02 To be .29 I would .09 To spend .32 I want .14 To have .02 <start> I’m .26 To eat .04 <start> Tell .01 Want Thai .06 <start> I’d .04 Want some .25 <start> I
    67. 68. <ul><li>P( I want to eat British food ) = P(I|<start>) P(want|I) P(to|want) P(eat|to) P(British|eat) P(food|British) = .25*.32*.65*.26*.001*.60 = .000080 </li></ul><ul><li>vs. I want to eat Chinese food = .00015 </li></ul><ul><li>Probabilities seem to capture ``syntactic'' facts, ``world knowledge'' </li></ul><ul><ul><li>eat is often followed by an NP </li></ul></ul><ul><ul><li>British food is not too popular </li></ul></ul><ul><li>N-gram models can be trained by counting and normalization </li></ul>
    68. 69. BERP Bigram Counts 0 1 0 0 0 0 4 Lunch 0 0 0 0 17 0 19 Food 1 120 0 0 0 0 2 Chinese 52 2 19 0 2 0 0 Eat 12 0 3 860 10 0 3 To 6 8 6 0 786 0 3 Want 0 0 0 13 0 1087 8 I lunch Food Chinese Eat To Want I
    69. 70. BERP Bigram Probabilities <ul><li>Normalization: divide each row's counts by appropriate unigram counts for w n-1 </li></ul><ul><li>Computing the bigram probability of I I </li></ul><ul><ul><li>C(I,I)/C(all I) </li></ul></ul><ul><ul><li>p (I|I) = 8 / 3437 = .0023 </li></ul></ul><ul><li>Maximum Likelihood Estimation (MLE): relative frequency of e.g. </li></ul>459 1506 213 938 3256 1215 3437 Lunch Food Chinese Eat To Want I
    70. 71. What do we learn about the language? <ul><li>What's being captured with ... </li></ul><ul><ul><li>P(want | I) = .32 </li></ul></ul><ul><ul><li>P(to | want) = .65 </li></ul></ul><ul><ul><li>P(eat | to) = .26 </li></ul></ul><ul><ul><li>P(food | Chinese) = .56 </li></ul></ul><ul><ul><li>P(lunch | eat) = .055 </li></ul></ul><ul><li>What about... </li></ul><ul><ul><li>P(I | I) = .0023 </li></ul></ul><ul><ul><li>P(I | want) = .0025 </li></ul></ul><ul><ul><li>P(I | food) = .013 </li></ul></ul>
    71. 72. <ul><ul><li>P(I | I) = .0023 I I I I want </li></ul></ul><ul><ul><li>P(I | want) = .0025 I want I want </li></ul></ul><ul><ul><li>P(I | food) = .013 the kind of food I want is ... </li></ul></ul>
    72. 73. Approximating Shakespeare <ul><li>As we increase the value of N, the accuracy of the n-gram model increases, since choice of next word becomes increasingly constrained </li></ul><ul><li>Generating sentences with random unigrams... </li></ul><ul><ul><li>Every enter now severally so, let </li></ul></ul><ul><ul><li>Hill he late speaks; or! a more to leg less first you enter </li></ul></ul><ul><li>With bigrams... </li></ul><ul><ul><li>What means, sir. I confess she? then all sorts, he is trim, captain. </li></ul></ul><ul><ul><li>Why dost stand forth thy canopy, forsooth; he is this palpable hit the King Henry. </li></ul></ul>
    73. 74. <ul><li>Trigrams </li></ul><ul><ul><li>Sweet prince, Falstaff shall die. </li></ul></ul><ul><ul><li>This shall forbid it should be branded, if renown made it empty. </li></ul></ul><ul><li>Quadrigrams </li></ul><ul><ul><li>What! I will go seek the traitor Gloucester. </li></ul></ul><ul><ul><li>Will you not tell me who I am? </li></ul></ul>
    74. 75. <ul><li>There are 884,647 tokens, with 29,066 word form types, in about a one million word Shakespeare corpus </li></ul><ul><li>Shakespeare produced 300,000 bigram types out of 844 million possible bigrams: so, 99.96% of the possible bigrams were never seen (have zero entries in the table) </li></ul><ul><li>Quadrigrams worse: What's coming out looks like Shakespeare because it is Shakespeare </li></ul>
    75. 76. N-Gram Training Sensitivity <ul><li>If we repeated the Shakespeare experiment but trained our n-grams on a Wall Street Journal corpus, what would we get? </li></ul><ul><li>This has major implications for corpus selection or design </li></ul>
    76. 77. Some Useful Empirical Observations <ul><li>A small number of events occur with high frequency </li></ul><ul><li>A large number of events occur with low frequency </li></ul><ul><li>You can quickly collect statistics on the high frequency events </li></ul><ul><li>You might have to wait an arbitrarily long time to get valid statistics on low frequency events </li></ul><ul><li>Some of the zeroes in the table are really zeros But others are simply low frequency events you haven't seen yet. How to address? </li></ul>
    77. 78. Smoothing Techniques <ul><li>Every n-gram training matrix is sparse, even for very large corpora ( Zipf’s law ) </li></ul><ul><li>Solution: estimate the likelihood of unseen n-grams </li></ul><ul><li>Problems: how do you adjust the rest of the corpus to accommodate these ‘phantom’ n-grams? </li></ul>
    78. 79. Smoothing Techniques <ul><li>Every n-gram training matrix is sparse, even for very large corpora ( Zipf’s law ) </li></ul><ul><li>Solution: estimate the likelihood of unseen n-grams </li></ul><ul><li>Problems: how do you adjust the rest of the corpus to accommodate these ‘phantom’ n-grams? </li></ul>
    79. 80. Add-one Smoothing <ul><li>For unigrams: </li></ul><ul><ul><li>Add 1 to every word (type) count </li></ul></ul><ul><ul><li>Normalize by N (tokens) /(N (tokens) +V (types)) </li></ul></ul><ul><ul><li>Smoothed count (adjusted for additions to N) is </li></ul></ul><ul><ul><li>Normalize by N to get the new unigram probability: </li></ul></ul><ul><li>For bigrams: </li></ul><ul><ul><li>Add 1 to every bigram c(w n-1 w n ) + 1 </li></ul></ul><ul><ul><li>Incr unigram count by vocabulary size c(w n-1 ) + V </li></ul></ul>
    80. 81. <ul><ul><li>Discount: ratio of new counts to old (e.g. add-one smoothing changes the BERP bigram (to|want) from 786 to 331 (d c =.42) and p(to|want) from .65 to .28) </li></ul></ul><ul><ul><li>But this changes counts drastically: </li></ul></ul><ul><ul><ul><li>too much weight given to unseen ngrams </li></ul></ul></ul><ul><ul><ul><li>in practice, unsmoothed bigrams often work better ! </li></ul></ul></ul>
    81. 82. <ul><li>A zero ngram is just an ngram you haven’t seen yet…but every ngram in the corpus was unseen once…so... </li></ul><ul><ul><li>How many times did we see an ngram for the first time? Once for each ngram type (T) </li></ul></ul><ul><ul><li>Est. total probability of unseen bigrams as </li></ul></ul><ul><ul><li>View training corpus as series of events, one for each token (N) and one for each new type (T) </li></ul></ul>Witten-Bell Discounting
    82. 83. <ul><ul><li>We can divide the probability mass equally among unseen bigrams….or we can condition the probability of an unseen bigram on the first word of the bigram </li></ul></ul><ul><ul><li>Discount values for Witten-Bell are much more reasonable than Add-One </li></ul></ul>
    83. 84. <ul><li>Re-estimate amount of probability mass for zero (or low count) ngrams by looking at ngrams with higher counts </li></ul><ul><ul><li>Estimate </li></ul></ul><ul><ul><li>E.g. N 0 ’s adjusted count is a function of the count of ngrams that occur once, N 1 </li></ul></ul><ul><ul><li>Assumes: </li></ul></ul><ul><ul><ul><li>word bigrams follow a binomial distribution </li></ul></ul></ul><ul><ul><ul><li>We know number of unseen bigrams (VxV-seen) </li></ul></ul></ul>Good-Turing Discounting
    84. 85. Backoff methods (e.g. Katz ‘87) <ul><li>For e.g. a trigram model </li></ul><ul><ul><li>Compute unigram, bigram and trigram probabilities </li></ul></ul><ul><ul><li>In use: </li></ul></ul><ul><ul><ul><li>Where trigram unavailable back off to bigram if available, o.w. unigram probability </li></ul></ul></ul><ul><ul><ul><li>E.g An omnivorous unicorn </li></ul></ul></ul>
    85. 86. Summary <ul><li>N-gram probabilities can be used to estimate the likelihood </li></ul><ul><ul><li>Of a word occurring in a context (N-1) </li></ul></ul><ul><ul><li>Of a sentence occurring at all </li></ul></ul><ul><li>Smoothing techniques deal with problems of unseen words in a corpus </li></ul>

    ×