Aakash Kattelu
WritingLanguage Modeling
Aakash Kattelu

Language Modeling

Written by Aakash Kattelu on
Contents

A language model is a system that is able to assign a probability to a sequence of words in a given language. It is a foundational task in natural language processing that has lots of practical applications. I’ll go over why we would want to do something like this and then a derivation of how we arrive at what is called “n-gram language modeling”.

Uses

Language modeling in a lot of NLP applications where you need ton select a possibility based on how likely it was of occuring.

In a word-auto-suggestion application, you would like to be able to suggest the word that is most likely to occur, based on the prior words. In a speech recognition application, you would want to be able to pick the most likely choice out of a series of words that sound really similar. In a translation application, you would want choose a translation that is the most likely (natural) to occur.

Word Probabilities

First off, calculating the probability of a sequence would be easier if we could think of it like a series of random events. If we consider fishing, the probability of “dog” being followed by “happy” could be similar to the probability of catching a salmon if your previous catch was a trout.

Regardless of the analogy, we would need something like a “lake”. Some sample data that we consider to be observations. We will use a corpus (a collection of text) as “training data” for this model. Depending on your application you can use anything from a news report to a file of code.

Then, we can define the following:

  • Joint Probabilities: The number of times words appear in sequence, out of the number of total tuples of these words. For example, “probability to a”, appears 2 times in this post.
  • Conditional Probabilities: The number of times a word appears with a sequence immediately prior. For example, the probability of “model” given “language” is 1⁄6, because “language” appears 6 times, and “language model” only appears once.

Given a document of text, and query for probability, we can now try to roughly approximate the likelihood of that query.

Derivations

The task is to be able to assign a probability to a sequence of words. So, given any sentence, our output should:

  • Be a value between 0 and 1
  • Sum with the other outputs to 1

If we consider our sentence to be a list of tokens (words), we are essentially trying to calculate:

Equation: P(w_1, w_2, w_3 ... w_n)

The chain rule of probability states that:

Equation: P(\bigcap_{k=1}^n w_k) = \prod_{k=1}^n P(w_k \mid \bigcap_{j=1}^{k-1} w_j )

So we can re-write our objective as:

Equation: \prod_{k=1}^n P(w_k \mid \bigcap_{j=1}^{k-1} w_j )

This formulation is basically the product of conditional probabilities of a word given all words before it. For example, if we were trying to find the probability of “dogs are nice pets”, the expression would be:

Equation: P(\text{dogs}) P(\text{are} \mid \text{dogs}) P(\text{nice}\mid \text{dogs are}) P(\text{pets} \mid \text{dogs are nice})

This helps us break down our problem, but ultimately, it doesn’t really bring us closer to an efficient evaluation. There are a couple of issues with it. Firstly, it takes a while. Counting the number of pairs, triplets, quadruplets, and so on, of words can be very taxing on space and time. Secondly, it can potentially be very inaccurate, and result in a lot of 0 probabilities. Consider the sentence, “the dog ate and slept while he prepared and ate the food” as a corpus. We would like to compute the probability of “the dog ate the food”. Using the above formulation, we would have to compute the probability of “the” given “the dog ate”, and that would be 0, resulting in the entire probability being 0. This is inaccurate, because it seems like this sentence receive a higher probability, as it is viable and probable.

We will try to eliminate both of these problems by making some assumptions.

N-Grams

N-gram assumptions (or markov assumptions) are assumptions that the conditional probability of a word only depends on the prior n-1 words. For example, a 3-gram (a.k.a. trigram) assumption would mean that a word only depends on the prior two words. A unigram assumption means that a word is independent of what comes before it.

Let’s analyze the effect of a bigram assumption on our probability expression. If a word only depends on the word immediately before it, we need not consider anything else in the conditional probability. This reduces our expression to:

Equation: \prod_{k=1}^n P(w_k \mid \bigcap_{j=1}^{k-1} w_j ) \to \prod_{k=1}^n P(w_k \mid w_{k-1})

A trigram assumption would yield:

Equation: \prod_{k=1}^n P(w_k \mid \bigcap_{j=1}^{k-1} w_j ) \to \prod_{k=1}^n P(w_k \mid w_{k-2}, w_{k-1})

This means that a unigram assumption would simplify our expression to:

Equation: \prod_{k=1}^n P(w_k \mid \bigcap_{j=1}^{k-1} w_j ) \to \prod_{k=1}^n P(w_k)

Having to compute only bigram or trigram frequencies would save a lot of time, and in the case of the above example, the bigram model would not assign a 0 probability to the specified sentence.

Conclusion

N-gram language models are very useful models in several aspects of NLP. They can also be relatively quick to implement, due to their simplicity. You should note that the more n-grams you use, the more accurate your model will be, because it allows your model to better reflect the training data’s sub-sentences. However, the model you use should also be dependent on how much training data you have available for your task.

Using simple n-gram models still bears problems though. Words like “the” that are extremely frequent are assigned unnaturally high probabilities, and the issue of 0 probabilities is still not entirely resolved, especially for higher-order n-grams. However, both of these issues can be combated with smoothing techniques (possibly a topic for a future blog post).

If you’re interested, I wrote a auto-completion feature using language models. You can check it out here!