MaskLID: Code-Switching Language Identification through Iterative Masking

Amir Hossein Kargaran, François Yvon, Hinrich Schütze

TL;DR

MaskLID is a simple, training-free method that enables standard monolingual language identifiers to detect multiple languages within a single "code-switched" sentence. By iteratively masking words associated with the dominant language, the model can "see" and identify minority languages that are usually overshadowed in a single-label prediction.

What Problem Does This Paper Address?

Code-switching (CS)—the practice of mixing two or more languages in a single conversation or sentence—is incredibly common in digital communication, yet it remains a major hurdle for Natural Language Processing (NLP).

Most high-performance Language Identification (LID) models, like FastText-based OpenLID or GlotLID, are trained on monolingual data. When these models encounter a mixed sentence (e.g., a Turkish sentence with an English phrase), they typically exhibit a "winner-take-all" bias, returning only the dominant language label. While word-level LID models exist, they are often restricted to specific language pairs and require massive amounts of annotated code-switched data, which is notoriously difficult to collect. The authors aim to bridge this gap by repurposing existing, high-quality monolingual models for multi-language detection without any additional training.

Methods

The core of MaskLID is an Iterative Masking strategy. It treats CS detection as a set prediction problem rather than a single-label classification task.

The algorithm works in rounds:

  1. Identify L1: The base LID model predicts the most likely language (L1) for the entire sentence.
  2. Internal Scoring: Instead of just looking at the final prediction, MaskLID looks "under the hood" at the model's internal logit scores for every word. It identifies which words contribute most strongly to the L1 prediction.
  3. Masking: Words that are strongly associated with L1 are "masked" (removed from the sentence).
  4. Iterate: The remaining, unmasked text is fed back into the LID model. If there is enough text left (above a byte-length threshold $\tau$), the model attempts to identify a second language (L2).

This process repeats for a set number of iterations ($\lambda$), allowing the system to identify arbitrary pairs or even triplets of languages. Because it relies on FastText's linear architecture, calculating these word-level contributions is computationally efficient and requires no external linguistic resources like dictionaries.

Datasets

The authors evaluated MaskLID across several code-switched and monolingual datasets:

  • Turkish–English: Sourced from Twitter and the Ekşi Sözlük forum.
  • Hindi–English & Nepali–English: Derived from the LinCE benchmark, a standard for code-switching evaluation.
  • Basque–Spanish: From the BaSCo corpus, representing a language pair where neither is English.
  • Monolingual Baselines: The authors also tested on purely monolingual versions of these languages to ensure the masking process didn't lead to "hallucinated" code-switching (false positives).

Key Insights and Ideas

  • Latent Knowledge: The most striking insight is that standard monolingual models already recognize minority language features; these features are simply suppressed by the final softmax layer. MaskLID acts as a filter to reveal this latent information.
  • Zero-Shot Multi-Labeling: MaskLID transforms a single-label classifier into a multi-label one without needing a single example of code-switched training data.
  • Explainability: Unlike black-box neural models, MaskLID provides a clear trail of evidence, showing exactly which words led to the identification of each language.
  • Scalability: By using FastText-based models, MaskLID can potentially support thousands of languages (GlotLID supports ~2100) and is fast enough to mine massive web corpora for real-world code-switching examples.

Key Results

MaskLID significantly outperformed baseline models in detecting code-switching, particularly in "Exact Match" (#EM) scenarios where both languages must be correctly identified.

As shown in the results table below, the improvements were dramatic:

  • Turkish–English: The number of exact matches jumped from 4 to 91.
  • Nepali–English: MaskLID detected 22 CS instances, whereas the baseline detected zero.
  • False Positives: Crucially, the False Positive (#FP) rate remained at or near zero, meaning the method rarely mistakes a monolingual sentence for a code-switched one.
Table 1: Performance comparison showing Exact Matches (#EM), Partial Matches (#PM), and False Positives (#FP) for MaskLID vs. Baselines.

Limitations and Future Work

Despite its success, MaskLID has a few notable limitations:

  • The "Short Text" Problem: LID models struggle with very short strings. If the second language in a sentence is only a single word (a loanword), it often falls below the length threshold ($\tau$) and goes undetected.
  • Tokenization: The current method relies on space-delimited words. This makes it less effective for languages like Chinese or Japanese that don't use spaces, or for morphologically rich languages where code-switching might happen at the sub-word level.
  • Hyperparameter Sensitivity: The performance depends on tuning parameters like $\alpha$ (masking threshold) and $\tau$ (length threshold), which may vary depending on the language pair.

Future work aims to extend the method to sub-word features and generalize the masking technique to other architectures (like Transformers) using feature-mapping tools like LIME.

Summary updated

BibTeX
@inproceedings{kargaran2024masklid,
  author = {Amir Hossein Kargaran and François Yvon and Hinrich Schütze},
  title = {{MaskLID: Code-Switching Language Identification through Iterative Masking}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2024},
  doi = {10.18653/v1/2024.acl-short.43},
}
Figure