Major links



Quicklinks


📌 Quick Links
[ DBMS ] [ SQL ] [ DDB ] [ ML ] [ DL ] [ NLP ] [ DSA ] [ PDB ] [ DWDM ] [ Quizzes ]


Monday, April 6, 2020

Bigram, Trigram, and NGram Models in NLP


Bigram Trigram and NGram in NLP, How to calculate the unigram, bigram, trigram, and ngram probabilities of a sentence? Maximum likelihood estimation to calculate the ngram probabilities



Page 1    Page 2    Page 3


How to use N-gram model to estimate probability of a word sequence?
Let us consider Equation 1 again. For a Unigram model, how would we change the Equation 1?
Unigram probability formula: probability of a word sequence w1 to wn equals the product of individual word probabilities P(w1) times P(w2) times P(w3) ... times P(wn-1) 
Example:
Unigram probability example: probability of the sequence 'the, prime, minister, of, our, country' equals P(the) times P(prime) times P(minister) times P(of) times P(our) times P(country)
For a bigram model, how would we change the Equation 1?
bigram probability calculation: given a sequence of words, the probability of that sequence is the product of individual bigram probabilities (ie the probability of word i given word i-1)
Example:
bigram probability of a word sequence example

For a trigram model, how would we change the Equation 1?
 trigram probability equation: given a word sequence, the probability of that sequence as per trigram model is the product of individual trigrams (ie., probability of word i given word i-1 and word i-1
Example:



Now, let us generalize the above examples of Unigram, Bigram, and Trigram calculation of a word sequence into equations.
Unigram:
unigram probability calculation equation
Bigram:
bigram probability calculation equation
In a Bigram model, for i=1, either the sentence start marker (<s>) or an empty string could be used as the word wi-1. [The empty string could be used as the start of the sentence or word sequence].
Trigram:
trigram probability calculation equation
In a Trigram model, for i=1 and i=2, two empty strings could be used as the word wi-1, wi-2. [The empty strings could be used as the start of every sentence or word sequence].

How do we estimate these N-gram probabilities?
We can use Maximum Likelihood Estimation to estimate the Bigram and Trigram probabilities. We get the MLE estimate for the parameters of an N-gram model by taking counts from a corpus, and normalizing them so they lie between 0 and 1.
For Bigram probability,
Example:
The bigram probability is calculated by dividing the number of times the string “prime minister” appears in the given corpus by the total number of times the word “prime” appears in the same corpus.

For Trigram probability,
Example:
The trigram probability is calculated by dividing the number of times the string “prime minister of” appears in the given corpus by the total number of times the string “prime minister” appears in the same corpus.




Page 1    Page 2    Page 3
----------------------------------------------------------------------------------------------------------------------------



Go to Natural Language Processing Home page


How to estimate bigram probability?

How to estimate trigram probability?

how to estimate ngram probability?

MLE for calculating the ngram probabilities

What is the equation for unigram, bigram and trigram estimation

Example bigram and trigram probability estimates

1 comment:

  1. the formula for bigram and trigram is incorrect for bigram you will skip the first word probability and starts calculating the conditional probability from second word, same goes for trigram

    ReplyDelete

Please visit, subscribe and share 10 Minutes Lectures in Computer Science

Featured Content

Multiple choice questions in Natural Language Processing Home

MCQ in Natural Language Processing, Quiz questions with answers in NLP, Top interview questions in NLP with answers Multiple Choice Que...

All time most popular contents