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:

$$P(car\text{ }insurance\text{ }must\text{ }be\text{ }bought\text{ }carefully) =$$ $$P(car)*P(insurance)*P(must)*P(be)*P(bought)*P(carefully)$$

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:

$$P(“best\text{ }websites\text{ }for\text{ }comparing\text{ }car\text{ }insurance”) =$$ $$P(best/startOfSentence)P(websites/best)P(for/websites)…P(endOfSentence/insurance)$$

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})}$$

Summary

In this post, you learned about different types of N-grams language models and also saw examples.
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 1, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.7530292272567749, "perplexity": 961.2065176099493}, "config": {"markdown_headings": false, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2019-22/segments/1558232256797.20/warc/CC-MAIN-20190522103253-20190522125253-00481.warc.gz"}