A word n-gram language model is a statistical model of language which calculates the probability of the next word in a sequence from a fixed size window of previous words. If one previous word is considered, it is a bigram model; if two words, a trigram model; if n − 1 words, an n-gram model. Special tokens are introduced to denote the start and end of a sentence ⟨s⟩ and ⟨/s⟩. To prevent a zero probability being assigned to unseen words, the probability of each seen word is slightly lowered to make room for the unseen words in a given corpus. To achieve this, various smoothing methods are used, from simple "add-one" smoothing (assigning a count of 1 to unseen n-grams, as an uninformative prior) to more sophisticated techniques, such as Good–Turing discounting or back-off models. Word n-gram models have largely been superseded by recurrent neural network–based models, which in turn have been superseded by Transformer-based models often referred to as large language models.
Unigram model
A special case, where n = 1, is called a unigram model. Probability of each word in a sequence is independent from probabilities of other word in the sequence. Each word's probability in the sequence is equal to the word's probability in an entire document. Puni(t1t2t3)=P(t1)P(t2)P(t3). The model consists of units, each treated as one-state finite automata. Words with their probabilities in a document can be illustrated as follows.
Total mass of word probabilities distributed across the document's vocabulary, is 1. ∑word in docP(word)=1 The probability generated for a specific query is calculated as P(query)=∏word in queryP(word) Unigram models of different documents have different probabilities of words in it. The probability distributions from different documents are used to generate hit probabilities for each query. Documents can be ranked for a query according to the probabilities. Example of unigram models of two documents:
Bigram model In a bigram word (n = 2) language model, the probability of the sentence I saw the red house is approximated as P(I, saw, the, red, house)≈P(I∣⟨s⟩)P(saw∣I)P(the∣saw)P(red∣the)P(house∣red)P(⟨/s⟩∣house)
Trigram model In a trigram (n = 3) language model, the approximation is P(I, saw, the, red, house)≈P(I∣⟨s⟩,⟨s⟩)P(saw∣⟨s⟩,I)P(the∣I, saw)P(red∣saw, the)P(house∣the, red)P(⟨/s⟩∣red, house) Note that the context of the first n – 1 n-grams is filled with start-of-sentence markers, typically denoted <s>. Additionally, without an end-of-sentence marker, the probability of an ungrammatical sequence *I saw the would always be higher than that of the longer sentence I saw the red house.
Approximation method The approximation method calculates the probability P(w1,…,wm) of observing the sentence w1,…,wm P(w1,…,wm)=∏i=1mP(wi∣w1,…,wi−1)≈∏i=2mP(wi∣wi−(n−1),…,wi−1) It is assumed that the probability of observing the ith word wi (in the context window consisting of the preceding i − 1 words) can be approximated by the probability of observing it in the shortened context window consisting of the preceding n − 1 words (nth-order Markov property). To clarify, for n = 3 and i = 2 we have P(wi∣wi−(n−1),…,wi−1)=P(w2∣w1). The conditional probability can be calculated from n-gram model frequency counts: P(wi∣wi−(n−1),…,wi−1)=count(wi−(n−1),…,wi−1,wi)count(wi−(n−1),…,wi−1)
Out-of-vocabulary words
An issue when using n-gram language models are out-of-vocabulary (OOV) words. They are encountered in computational linguistics and natural language processing when the input includes words which were not present in a system's dictionary or database during its preparation. By default, when a language model is estimated, the entire observed vocabulary is used. In some cases, it may be necessary to estimate the language model with a specific fixed vocabulary. In such a scenario, the n-grams in the corpus that contain an out-of-vocabulary word are ignored. The n-gram probabilities are smoothed over all the words in the vocabulary even if they were not observed. Nonetheless, it is essential in some cases to explicitly model the probability of out-of-vocabulary words by introducing a special token (e.g. <unk>) into the vocabulary. Out-of-vocabulary words in the corpus are effectively replaced with this special <unk> token before n-grams counts are cumulated. With this option, it is possible to estimate the transition probabilities of n-grams involving out-of-vocabulary words.
n-grams for approximate matching
n-grams were also used for approximate matching. If we convert strings (with only letters in the English alphabet) into character 3-grams, we get a 263-dimensional space (the first dimension measures the number of occurrences of "aaa", the second "aab", and so forth for all possible combinations of three letters). Using this representation, we lose information about the string. However, we know empirically that if two strings of real text have a similar vector representation (as measured by cosine distance) then they are likely to be similar. Other metrics have also been applied to vectors of n-grams with varying, sometimes better, results. For example, z-scores have been used to compare documents by examining how many standard deviations each n-gram differs from its mean occurrence in a large collection, or text corpus, of documents (which form the "background" vector). In the event of small counts, the g-score (also known as g-test) gave better results. It is also possible to take a more principled approach to the statistics of n-grams, modeling similarity as the likelihood that two strings came from the same source directly in terms of a problem in Bayesian inference. n-gram-based searching was also used for plagiarism detection.
Bias–variance tradeoff
To choose a value for n in an n-gram model, it is necessary to find the right trade-off between the stability of the estimate against its appropriateness. This means that trigram (i.e. triplets of words) is a common choice with large training corpora (millions of words), whereas a bigram is often used with smaller ones.
… excerpt ends here. Continue reading the full article.
