Language Identification › Classical Approaches: N-gram Language Models
Language AI HandbookAll chapters Language Identification › Classical Approaches: N-gram Language ModelsOne section · Sections in this chapter3 / 11 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Before neural methods, the dominant approach to LangID was character-level n-gram language models. The intuition is clean: every language has a characteristic distribution of character sequences. The trigram sch is much more common in German than in English. The sequence zione is quintessentially Italian. The bigram th is a hallmark of English. The character sequence ão signals Portuguese. These distributions are stable, compact, and highly discriminative even on modest amounts of training data.
The Mathematical Foundation
For each candidate language in a set of languages , we train a character-level n-gram language model on a corpus of text in that language. Given a query document , we compute the log-probability of the document under each language model and classify by maximum likelihood:
where:
- : the predicted language label
- : the set of all candidate languages
- : the probability assigned to document by the language model for language
- : the log-probability, computed as the sum of log-probabilities of each n-gram in
The log-probability of a document decomposes over its character n-grams using the chain rule:
where:
- : the -th character in the document
- : the conditional probability of character given the preceding characters, according to language model
Each language model is a lookup table of n-gram counts, normalized by the total count of all trigrams sharing the same two-character prefix. Smoothing is essential: unseen n-grams would receive zero probability under a maximum likelihood model, making the log-probability for any document containing an unseen trigram. Laplace (add-one) smoothing assigns a small non-zero probability to every n-gram, even those not observed in training.
The choice of matters. Unigrams (single characters) carry almost no language-specific information beyond script identity. Bigrams start to capture language-specific patterns but are still quite general. Trigrams represent the sweet spot for most languages: they are specific enough to capture morphological signatures but common enough to appear frequently in training data. Quadgrams and longer n-grams are more discriminative but require larger training corpora to estimate reliably.
Normalization by document length is necessary when comparing documents of different lengths. A long document will accumulate a lower total log-probability simply because it is longer, not because it is less likely under any particular language model. Dividing by the number of n-grams converts absolute log-probability to average per-n-gram log-probability, making scores comparable across document lengths.
Caveat: Rank-Based Methods
An influential alternative, introduced by Cavnar and Trenkle in 1994, avoids probability estimation entirely. Instead, it creates a ranked list of the 300 most frequent n-grams for each language. To classify a document, the algorithm computes the ranked n-gram list for the document and compares it to each language profile using a rank-distance metric called the "out-of-place" measure:
where:
- : a character n-gram from the document's top- most frequent n-grams
- : the rank of n-gram in the document's frequency list
- : the rank of n-gram in language 's profile (set to a large penalty value if does not appear in the profile)
The language with the smallest out-of-place distance wins the classification. This approach is robust to short text because it depends on relative frequencies rather than absolute counts, and it does not require careful probability smoothing. The approach remains competitive for clean, well-formed text in common languages.
Why Character N-grams Beat Word-Level Models for LangID
A word-level language model for LangID would assign probabilities based on which words appear in the document. The problem is that word-level models require a fixed vocabulary, and any word outside the vocabulary contributes nothing to classification. For a new document in a rare language, or for a document containing technical vocabulary, many words may be unknown.
Character n-grams sidestep this entirely. Every character sequence of length is a valid n-gram. A medical article in German will contain medical vocabulary that does not appear in a general German training corpus, but it will still contain German morphological patterns (the suffixes -ung, -keit, -lich, characteristic prepositions), function words spelled out character by character, and orthographic conventions like the use of ß. These signals persist regardless of domain shift.
Character n-gram models also handle agglutinative languages well. Finnish, Turkish, Hungarian, and many other languages form very long words by combining morphemes, meaning that word-level models face severe vocabulary explosion while character n-gram models see stable distributions.