N-Gram Language Modelling with NLTK
Language modeling is the way of determining the probability of any sequence of words. Language modeling is used in a wide variety of applications such as Speech Recognition, Spam filtering, etc. In fact, language modeling is the key aim behind the implementation of many state-of-the-art Natural Language Processing models.
Methods of Language Modelings:
Two types of Language Modelings:
- Statistical Language Modelings: Statistical Language Modeling, or Language Modeling, is the development of probabilistic models that are able to predict the next word in the sequence given the words that precede. Examples such as N-gram language modeling.
- Neural Language Modelings: Neural network methods are achieving better results than classical methods both on standalone language models and when models are incorporated into larger models on challenging tasks like speech recognition and machine translation. A way of performing a neural language model is through word embeddings.
N-gram can be defined as the contiguous sequence of n items from a given sample of text or speech. The items can be letters, words, or base pairs according to the application. The N-grams typically are collected from a text or speech corpus (A long text dataset).
N-gram Language Model:
An N-gram language model predicts the probability of a given N-gram within any sequence of words in the language. A good N-gram model can predict the next word in the sentence i.e the value of p(w|h)
Example of N-gram such as unigram (“This”, “article”, “is”, “on”, “NLP”) or bi-gram (‘This article’, ‘article is’, ‘is on’,’on NLP’).
Now, we will establish a relation on how to find the next word in the sentence using
. We need to calculate p(w|h), where is the candidate for the next word. For example in the above example, lets’ consider, we want to calculate what is the probability of the last word being “NLP” given the previous words:
After generalizing the above equation can be calculated as:
But how do we calculate it? The answer lies in the chain rule of probability:
Now generalize the above equation:
Simplifying the above formula using Markov assumptions:
- For unigram:
- For Bigram:
he said he said kotc he said kotc made he said kotc made profits he said kotc made profits of he said kotc made profits of 265 he said kotc made profits of 265 , he said kotc made profits of 265 , 457 he said kotc made profits of 265 , 457 vs he said kotc made profits of 265 , 457 vs loss he said kotc made profits of 265 , 457 vs loss eight he said kotc made profits of 265 , 457 vs loss eight cts he said kotc made profits of 265 , 457 vs loss eight cts net he said kotc made profits of 265 , 457 vs loss eight cts net loss he said kotc made profits of 265 , 457 vs loss eight cts net loss 343 he said kotc made profits of 265 , 457 vs loss eight cts net loss 343 , he said kotc made profits of 265 , 457 vs loss eight cts net loss 343 , 266 he said kotc made profits of 265 , 457 vs loss eight cts net loss 343 , 266 , he said kotc made profits of 265 , 457 vs loss eight cts net loss 343 , 266 , 000 he said kotc made profits of 265 , 457 vs loss eight cts net loss 343 , 266 , 000 shares
Metrics for Language Modelings
- Entropy: Entropy, as a measure of the amount of information conveyed by Claude Shannon. Below is the formula for representing entropy
H(p) is always greater than equal to 0.
- Cross-Entropy: It measures the ability of the trained model to represent test data().
The cross-entropy is always greater than or equal to Entropy i.e the model uncertainty can be no less than the true uncertainty.
- Perplexity: Perplexity is a measure of how good a probability distribution predicts a sample. It can be understood as a measure of uncertainty. The perplexity can be calculated by cross-entropy to the exponent of 2.
Following is the formula for the calculation of Probability of the test set assigned by the language model, normalized by the number of words:
- Let’s take an example of the sentence: ‘Natural Language Processing’. For predicting the first word, let’s say the word has the following probabilities:
|word||P(word | <start>)|
- Now, we know the probability of getting the first word as natural. But, what’s the probability of getting the next word after getting the word ‘Language‘ after the word ‘Natural‘.
|word||P(word | ‘Natural’ )|
- After getting the probability of generating words ‘Natural Language’, what’s the probability of getting ‘Processing‘.
|word||P(word | ‘Language’ )|
- Now, the perplexity can be calculated as:
- From that we can also calculate entropy:
- To get a better context of the text, we need higher values of n, but this will also increase computational overhead.
- The increasing value of n in n-gram can also lead to sparsity.
Attention reader! Don’t stop learning now. Get hold of all the important Machine Learning Concepts with the Machine Learning Foundation Course at a student-friendly price and become industry ready.