Language Identification (LID)

Machine Learning
Type
NLP text-classification task

What does it do?

Language identification is a natural-language-processing task that automatically determines which language a piece of text is written in.

Off-the-shelf tools such as langid.py (Lui & Baldwin, 2012) provide ready-made implementations, and fastText-style classifiers are a common alternative. It is widely used to tag and filter multilingual data sets before further analysis.

The intuition behind the method

Different languages leave characteristic fingerprints in their letter and word patterns: the sequences of characters that appear in Turkish look different from those in English or Japanese. Instead of needing a human to read and recognise each document, language identification learns these statistical patterns and assigns each text a language label, so a multilingual collection can be sorted by language automatically.

History

Lui and Baldwin (2012) released langid.py as a self-contained, off-the-shelf language identification tool. Joulin et al. (2017) describe fastText, an efficient approach to general text classification; applying it to language identification requires training on labelled language data.

When to use it

Use language identification when you have text of unknown or mixed language and need to sort or filter it by language before further analysis, such as before applying a tokeniser, lexicon or language model built for one language. Each document should be long enough to carry a reliable signal; around 20 characters is a common rule of thumb, not a guaranteed minimum. Do not use it on very short fragments without a length filter, and skip the step if all your text is already known to be in one language.

Limitations and considerations

  • Very short texts carry too little signal for a reliable label. Around 20 characters is a common rule of thumb, not a guaranteed threshold.
  • Because the output is a single label per document, code-switched text, where two languages alternate within one document, cannot be represented correctly.
  • Accuracy can drop for low-resource languages and for text that mixes scripts.

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • Pretrained off-the-shelf tools can be applied without a labelled data set of your own, so a multilingual collection can be tagged quickly.
  • Character n-gram features do not require splitting text into words, so the same procedure applies to languages that do not separate words with spaces.
  • The output is one label per document, which turns an unsorted multilingual collection into language-tagged subsets that downstream steps can process separately.

Applications

  • Preprocessing multilingual data sets by tagging each document with its language.
  • Filtering a corpus to keep only documents in the target language before further analysis.
  • Routing incoming text to language-specific pipelines in social-science, humanities, and business research.
  • Cleaning web-scraped collections that mix many languages.

How it works

How it works

1

The text to be analysed is collected and checked for usable length. Very short fragments carry too little signal to label reliably. A minimum of around 20 characters is a common rule of thumb rather than an established cut-off, so check it against your own data before relying on it.

2

Each document is turned into the statistical signal that distinguishes one language from another, typically character n-gram patterns that capture how letters tend to follow one another in a given language. These patterns are what the classifier reads.

3

A trained model assigns the most likely language label to each document. Ready-made tools such as langid.py can be applied directly. fastText-style classifiers are supervised models: they must be trained on labelled text, either your own or a separately released pretrained language-identification model. The output is one predicted language per text.

4

The predicted labels are used downstream: a multilingual collection is sorted by language, and documents in unwanted languages are removed before further text processing. Where the tool reports a confidence score, set a threshold on it and send low-confidence documents for review rather than sorting them automatically. Because the label is a prediction, documents assigned to the wrong language are dropped or kept without any trace unless the labels are checked.

Common pitfalls

  • Running the classifier on very short snippets and trusting the label anyway; enforce the minimum length before classification.
  • Assigning one label to a code-switched document; the single predicted label will be misleading, and filtering on it may keep or discard the document for the wrong reason.
  • Treating the predicted language as ground truth without spot-checking a sample, especially for closely related languages, where mislabels are harder to notice.
  • Filtering on predicted labels without reviewing the removed documents, which silently shrinks the target set when the classifier errs.
  • Applying one confidence threshold to every language without checking the error rate for each one, since accuracy varies from language to language.

Frequently asked questions

How much text does language identification need?

Each document should be at least around 20 characters, as a common rule of thumb. Shorter fragments carry less statistical signal, and their predicted language is less reliable.

What happens with text that mixes two languages?

Code-switching is a known difficulty: when two languages appear in one document, a tool that returns a single label cannot represent both, and the result is often misleading. Segment or flag such texts rather than trusting one label.

Do I need labelled training data?

It depends on the tool. langid.py is a ready-made tool and can be applied directly. A fastText-style classifier is supervised: you need either a separately released pretrained language-identification model or your own labelled training data. In either case, a labelled sample of your own corpus is useful for measuring how accurate the labels are.

Why identify language before other text analysis?

A tokeniser, lexicon or language model built for one language gives meaningless results on text in another. Identifying and filtering by language first keeps each downstream tool within the language it was built for.

How do I check how accurate the labels are?

Hand-label a random sample of your own documents, then compare the predictions with those labels and compute precision and recall for each language. Do this for every language you plan to keep, since accuracy varies from language to language. If the tool returns a confidence score, use the sample to choose a threshold below which documents are reviewed or dropped.

Related methods

Sources

  1. Lui, M. & Baldwin, T. (2012). langid.py: An Off-the-shelf Language Identification Tool. Proceedings of the ACL 2012 System Demonstrations.

    Open publication reference
  2. Joulin, A., Grave, E., Bojanowski, P. & Mikolov, T. (2017). Bag of Tricks for Efficient Text Classification. Proceedings of the EACL 2017.

    Open publication reference