**Language models** are models which assign **probabilities** to a sentence or a sequence of words or, probability of an upcoming word given previous set of words. Language models are used in fields such as speech recognition, spelling correction, machine translation etc. Language models are primarily of two kinds:

- N-Gram language models
- Grammar-based language models such as probabilistic context-free grammars (PCFGs)

In this post, you will learn about some of the following:

- Introduction to Language Models
- N-Grams language models

### Introduction to Language Models

Language models, as mentioned above, is used to determine the probability of occurrence of a sentence or a sequence of words. Language models are created based on following two scenarios:

**Scenario 1**: The probability of a sequence of words is calculated based on the product of probabilities of each word. Let’s say, we need to calculate the probability of occurrence of the sentence, “car insurance must be bought carefully”. The probability of occurrence of this sentence will be calculated based on following formula:

In above formula, the probability of each word can be calculated based on following:

\(P(car)=\frac{No.\text{ }of\text{ }times\text{ }word\text{ }car\text{ }occurred\text{ }in\text{ }the\text{ }corpus)}{total\text{ }no\text{ }of\text{ }words\text{ }in\text{ }the\text{ }corpus}\)

Generalizing above, the following can be said:

\(P(w_{i}) = \frac{c(w_{i})}{c(w)}\)

In above formula, \(w_{i}\) is any specific word, \(c(w_{i})\) is count of specific word, and \(c(w)\) is count of all words.

**Scenario 2**: The probability of a sequence of words is calculated based on the product of probabilities of words given occurrence of previous words. Let’s say, we need to calculate the probability of occurrence of the sentence, “best websites for comparing car insurances”. The probability of occurrence of this sentence will be calculated based on following formula:

In above formula, the probability of a word given the previous word can be calculated using the formula such as following:

\(P(websites/best) = \frac{P(best\text{ }websites)}{P(best)}\)

Generalizing above, the following can be said:

\(P(\frac{w_{i}}{w_{i-1}}) = \frac{P(w_{i-1}w_{i})}{P(w_{i-1})}\)### N-Grams Language models

As defined earlier, Language models are used to determine the probability of a sequence of words. The sequence of words can be 2 words, 3 words, 4 words…n-words etc. **N-grams is also termed as a sequence of n words**. The language model which is based on determining probability based on the count of the sequence of words can be called as **N-gram language model**. Based on the count of words, N-gram can be:

**Unigram**: Sequence of just 1 word**Bigram**: Sequence of 2 words**Trigram**: Sequence of 3 words- …so on and so forth

#### Unigram Language Model Example

Let’s say we want to determine the probability of the sentence, “Which is the best car insurance package”. Based on Unigram language model, probability can be calculated as following:

\(P(“Which\text{ }is\text{ }best\text{ }car\text{ }insurance\text{ }package”) =\) \(P(which)P(is)…P(insurance)P(package)\)

Above represents product of probability of occurrence of each of the words in the corpus. The probability of any word, \(w_{i}\) can be calcuted as following:

\(P(w_{i}) = \frac{c(w_{i})}{c(w)}\)where \(w_{i}\) is ith word, \(c(w_{i})\) is count of \(w_{i}\) in the corpus, and \(c(w)\) is count of all the words.

#### Bigram Language Model Example

Using above sentence as example and Bigram language model, the probability can be determined as following:

\(P(“Which\text{ }is\text{ }best\text{ }car\text{ }insurance\text{ }package”) = \) \(P(\frac{which}{startOfSentence})P(\frac{is}{which})P(\frac{best}{is})..P(\frac{endOfSentence}{package})\)

The following represents example of how to calculate each of the probabilities:

\(P(\frac{car}{best}) = \frac{P(best\text{ }car)}{P(best)}\)

The above can also be calculated as following:

\(P(\frac{car}{best}) =\) \(\frac{c(best\text{ }car)}{c(best)}\)

The above could be read as: Probability of word “car” given word “best” has occurred is probability of word “best car” divided by probability of word “best”. Alternatively, Probability of word “car” given word “best” has occurred is count of word “best car” divided by count of word “best”.

Above represents product of probability of occurrence of each of the word given earlier/previous word. Generally speaking, the probability of any word given previous word, \(\frac{w_{i}}{w_{i-1}}\) can be calculated as following:

\(P(\frac{w_{i}}{w_{i-1}}) = \frac{P(w_{i-1},w_{i})}{P(w_{i-1})}\)#### Trigram Language Model Example

Let’s say we want to determine probability of the sentence, “Which company provides best car insurance package”. Using trigram language model, the probability can be determined as following:

\(P(“Which\text{ }company\text{ }provides\text{ }best\text{ }car\text{ }insurance\text{ }package”) = \) \(P(\frac{company}{which\text{ }startOfSentence})P(\frac{provides}{which\text{ }company})P(\frac{best}{company\text{ }provides})…P(\frac{endOfSentence}{insurance\text{ }package})\)

The following represents example of how to calculate each of the probabilities:

\(P(\frac{provides}{which\text{ }company}) =\) \(\frac{P(which\text{ }company\text{ }provides)}{P(which\text{ }company)}\)

The above can also be calculated as following:

\(P(\frac{provides}{which\text{ }company}) = \) \(\frac{c(which\text{ }company\text{ }provides)}{c(which\text{ }company)}\)

The above could be read as: Probability of word “provides” given words “which company” has occurred is probability of word “which company provides” divided by probability of word “which company”. Alternatively, Probability of word “provides” given words “which company” has occurred is count of word “which company provides” divided by count of word “which company”.

Generalizing above, the probability of any word given two previous words, \(\frac{w_{i}}{w_{i-2},w_{i-1}}\) can be calculated as following:

\(P(\frac{w_{i}}{w_{i-2},w_{i-1}}) = \frac{P(w_{i-2},w_{i-1},w_{i})}{P(w_{i-2},w_{i-1})}\)### Further Reading

### Summary

In this post, you learned about **different types of N-grams language models** and also saw **examples**.

Did you find this article useful? Do you have any questions or suggestions about this article or understanding **N-grams language models**? Leave a comment and ask your questions and I shall do my best to address your queries.

- Generative Modeling in Machine Learning: Examples - March 19, 2023
- Data Analytics Training Program (Beginners) - March 18, 2023
- Histogram Plots using Matplotlib & Pandas: Python - March 18, 2023

## Leave a Reply