To solve the ambiguity between phrases like "recognize speech" and "wreck a nice beach," a language model must determine which word sequence is more probable. Calculating the probability of an entire sentence from scratch, however, is a complex task. For a sentence with k words, w1,w2,…,wk, the chain rule of probability states:
P(w1,w2,…,wk)=P(w1)×P(w2∣w1)×P(w3∣w1,w2)×⋯×P(wk∣w1,…,wk−1)As you can see, the context required to predict each subsequent word grows longer and longer. This approach is not only computationally intensive but also requires an impossible amount of data to accurately estimate the probability for every conceivable word history.
N-gram models provide a practical solution by making a simplifying assumption: the probability of a word depends only on a small, fixed number of preceding words, not the entire sequence. This is known as the Markov assumption.
The most straightforward N-gram model is the bigram model, where we assume the probability of a word depends only on the single word that came before it. A sequence of two adjacent words is called a bigram.
The probability of a word wi given the previous word wi−1 is written as P(wi∣wi−1). To find the probability of a whole sentence, we multiply the probabilities of its constituent bigrams.
Let's apply this to our example, "recognize speech":
P(recognize speech)≈P(recognize)×P(speech∣recognize)We estimate these probabilities by counting word occurrences in a massive dataset of text, known as a corpus. The formula for the conditional probability is:
P(speech∣recognize)=Count("recognize")Count("recognize speech")If "recognize" appears 1,000 times in our corpus and is followed by "speech" 400 times, then P(speech∣recognize)=400/1000=0.4. If it's followed by "a" only 5 times, then P(a∣recognize)=5/1000=0.005. A bigram model learns that "speech" is a much more likely word to follow "recognize" than "a".
Probabilities derived from a text corpus. The phrase "recognize speech" is a more common pairing than "nice beach," making it more probable in a bigram model.
A bigram model helps, but sometimes one word of context isn't enough. A trigram model extends the context by assuming the probability of a word depends on the two preceding words. A sequence of three adjacent words is called a trigram.
The probability is written as P(wi∣wi−2,wi−1).
Let's look at the phrase "a nice beach". A trigram model would calculate the probability of "beach" based on the two words before it:
P(beach∣a nice)=Count("a nice")Count("a nice beach")This additional context is powerful. The word "nice" can be followed by many things ("nice day", "nice car", "nice person"). However, the sequence "a nice" provides a stronger signal that "beach" might be coming next, compared to what a bigram model sees with only "nice". This helps the ASR system make a more informed choice.
Dependency diagrams for a bigram and a trigram model. The trigram model uses a wider two-word context to predict the next word, which can capture more detailed language patterns.
You can extend this pattern to 4-grams (using 3 words of context), 5-grams (using 4 words), and so on. However, as N increases, a significant problem emerges: data sparsity.
For a bigram like "recognize speech", you might find thousands of examples in a text corpus. For a trigram like "to recognize speech", you'll find fewer examples. For a 5-gram like "I am trying to recognize speech", you might find very few, or even zero, instances. When a specific N-gram doesn't appear in the training data, its count is zero, and its probability cannot be calculated properly. Because of this trade-off, bigram and trigram models have historically been a sweet spot between context and reliability.
How do you calculate the probability of the very first word in a sentence? It has no preceding words. To solve this, a special "start-of-sentence" token, often written as <s>, is added to the beginning of every sentence in the training data. The probability of the first word "wreck" is then calculated as a bigram P(wreck∣<s>). Similarly, the probability of the second word "a" would be calculated as a trigram P(a∣<s>,wreck). This ensures every word has the required amount of context.
Was this section helpful?
© 2026 ApX Machine LearningEngineered with