Language Detection and Identification
Language identification (LID) determines which natural language a piece of text is written in, a deceptively simple-sounding task that underpins routing text to the right downstream model, filtering content by language, and building multilingual datasets.
Classic Approaches
Character n-gram frequency models remain a strong, lightweight baseline: languages have distinctive letter and character-sequence frequency distributions, so a model comparing a text’s n-gram profile against known per-language profiles (using something like a naive Bayes classifier, see [[naive-bayes-classifier]]) can classify short text quickly and accurately for well-resourced languages.
"bonjour tout le monde" -> character trigrams closely match French frequency profile -> fr
Why Short and Mixed Text Is Hard
Very short text (a two-word search query, a single emoji-laden tweet) provides little statistical signal, and accuracy on strings under roughly 10 characters drops substantially compared to full sentences or paragraphs. Code-switched text — a single message mixing two languages, common in multilingual communities and casual online writing — breaks the assumption that a whole document belongs to one language, requiring segment-level rather than document-level identification. Closely related languages with high lexical overlap (Croatian and Serbian, Indonesian and Malay, Danish and Norwegian) are also a persistent source of misclassification for both classic and neural LID systems.
Neural Approaches
Neural LID models, typically a small classifier on top of character or subword embeddings, handle more languages and short-text cases better than pure n-gram models, especially when trained on diverse, noisy, real-world text (social media, transcribed speech) rather than only clean formal text like news articles, since real deployment text looks much more like the former.
Practical Guidance
Route text through language identification before applying any language-specific NLP step (tokenization choices, spell-checking, sentiment models trained on one language), since silently applying an English-tuned pipeline to non-English text produces confidently wrong results rather than an obvious failure. For applications handling many short user-generated inputs, budget for lower LID accuracy on short and code-switched text rather than assuming benchmark accuracy (usually reported on longer, cleaner text) will hold in production.