ArticleslgStudy

mathematics

Word n-gram language model

Word n-gram language model is a mathematics topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Word n-gram language model rather than just read about it. In short: 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.

Word n-gram language model — main illustration
Word n-gram language model — illustration

Key takeaways

  • Word n-gram language model belongs to mathematics; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Word n-gram language model to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Word n-gram language model from memory before moving on to harder problems.

Reference excerpt

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.

Worked examples

Example 1 — a first encounter with Word n-gram language model

Start with the simplest possible case. Write down what Word n-gram language model claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In mathematics, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Word n-gram language model before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Word n-gram language model ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Word n-gram language model

In research
Word n-gram language model appears in mathematics research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Word n-gram language model in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Word n-gram language model is common in secondary-school and first-year university syllabi. It links to neighbouring topics Language modeling, Markov models, Statistical natural language processing, so understanding it makes those chapters shorter.
In everyday life
Look for Word n-gram language model outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Word n-gram language model” →

How to study Word n-gram language model in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Word n-gram language model means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Word n-gram language model out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Word n-gram language model in simple terms?

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.

Why does Word n-gram language model matter?

Because it connects several mathematics ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Word n-gram language model?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Word n-gram language model.

Tags

  • Language modeling
  • Markov models
  • Statistical natural language processing

Keep exploring