Code-Switching and Mixed-Language Text › Evaluating Mixed-Language Output

Michael BrenndoerferNovember 26, 20266 min read
Language AI HandbookAll chapters
Part 26 of 67Multilingual Language Models and Cross-Lingual Transfer
Chapter 188Code-Switching and Mixed-Language Text › Evaluating Mixed-Language Output
Reading section 9 of 13 · Evaluating Mixed-Language Output · 6 minSection 9 of 13 · 6 min

Section 9 of 13 · 6 min read

Reading settings

Definitions

Choose how many terms have tooltips.

Article links

Make inline references clickable

Notes & comments

Choose metrics for the decision at hand. First check what their units, denominators, and reference representations permit them to measure.

Why accuracy and BLEU mislead

Sentence-level LID accuracy compares each selected label with a single reference label. If 95% of the dataset has the same monolingual reference label and the remaining 5% is labeled mixed, always returning the majority label scores 95% and misses all mixed items.

Token-level LID accuracy is better but still misleads if it is computed only on tokens within the dominant language. Report it stratified by gold label, and report switch-point F1F_1 alongside it. Note also that token-level accuracy hides the granularity mismatch: a token is not the same unit as a switch point, and a system can score high per-token while systematically erring exactly at the boundaries. This is the LID version of the class-imbalance problem, and it has the same fix: report the metric per class and report the metric on the rare class separately.

CMI matched against gold. The code-mixing index, computed on predicted versus gold labels, gives a scalar sense of how mixed the prediction is compared with the reference. A much lower CMI than gold can indicate a dominant-label bias. Check coverage because unknown labels can also change the denominator. CMI is invariant to token order and language-name permutations. It measures label balance rather than switch placement or the direction of a monolingual collapse.

BLEU on mixed output. Compare outputs with references written for the same target language policy and stated tokenization. BLEU rewards matching n-grams, so alternate switch placements and paraphrases can change the score. Use semantic and style judgments for distinctions that the overlap score leaves open.

Perplexity. Specify the evaluated model, token representation, and matching inputs before comparing likelihoods. Evaluate whether readers find the outputs natural separately.

Metrics that measure mixing

Four metric families are worth building into your evaluation harness.

  1. Language proportions. Measure each language's share of the labeled words and check it against a specified target range. Report tagger coverage and validate tags on a sample of the intended style.
  2. Switch-point density. Divide the number of switches by the number of adjacent word pairs. The exercise reports zero for a one-word response, which has no adjacent pair. Compare with a reference corpus measured the same way.
  3. Script and spelling consistency. Use Unicode properties to check script composition. Comparing romanization variants requires a separate spelling convention and annotations, even when all the characters are Latin.
  4. Human judgments. Ask bilingual readers to assess meaning, naturalness, and register. Report mixed and monolingual slices separately.

The following four responses are hand-written. The helper accepts one hi or en label per whitespace-delimited word, and the response must contain at least one word. Punctuation stays attached to its word. The Hindi-only control uses Latin letters. For inputs with unknown labels, report annotation coverage before choosing a denominator, then use a calculation designed for those labels.

PythonIn [24]
CollapseExpand
candidates = {
    "Mixed": (
        "Pehle charger aur cable check karo yaar. Agar woh theek hai to battery calibration try karo.",
        "hi en hi en en hi hi hi hi hi hi hi en en en hi",
    ),
    "English only": (
        "First check the charger and cable. Then try calibrating the battery.",
        "en en en en en en en en en en en",
    ),
    "Hindi only": (
        "Pehle taar aur jod ki jaanch karo. Phir dobara koshish karo.",
        "hi hi hi hi hi hi hi hi hi hi hi",
    ),
    "Shifts to English": (
        "Pehle charger aur cable check karo yaar. Then run a battery calibration cycle.",
        "hi en hi en en hi hi en en en en en en",
    ),
}


def evaluate_response(text, label_string):
    answer_tokens, labels = text.split(), label_string.split()
    if not answer_tokens or len(answer_tokens) != len(labels):
        raise ValueError("Provide a nonempty response and one label per word.")
    if set(labels) - hinglish_languages:
        raise ValueError("The fixture accepts only hi and en word labels.")
    n = len(labels)
    switches = len(switch_points(labels, hinglish_languages))
    full_english = labels.count("en") / n
    end_english = labels[-5:].count("en") / len(labels[-5:])
    return {
        "words": n,
        "switches": switches,
        "switch_density": switches / (n - 1) if n > 1 else 0.0,
        "cmi": code_mixing_index(labels, hinglish_languages),
        "english_share": full_english,
        "end_english_share": end_english,
        "latin_only_share": sum(
            token_scripts(t) == ["Latin"] for t in answer_tokens
        )
        / n,
    }


reports = {name: evaluate_response(*item) for name, item in candidates.items()}
drift_x = np.arange(len(reports))
whole_english = [r["english_share"] for r in reports.values()]
last_english = [r["end_english_share"] for r in reports.values()]
ConsoleOut [25]
CollapseExpand
Mixed              switches= 6  density=0.400  CMI= 37.5  English=37.5%  final five English=60%  Latin-only=100%
English only       switches= 0  density=0.000  CMI=  0.0  English=100.0%  final five English=100%  Latin-only=100%
Hindi only         switches= 0  density=0.000  CMI=  0.0  English=0.0%  final five English=0%  Latin-only=100%
Shifts to English  switches= 5  density=0.417  CMI= 30.8  English=69.2%  final five English=100%  Latin-only=100%

The mixed reply has six switches across 15 adjacent pairs. Its CMI is 37.5, and its English share is 6/16 (37.5%) overall and 3/5 (60%) at the end. The drift reply has five switches across 12 pairs and a CMI of about 30.8. Its English share is 9/13 (69.2%) overall and 5/5 (100%) at the end. Both monolingual controls have zero CMI and zero switches, with English shares of 100% and 0% respectively. All four replies have a Latin-only share of 100%.

VisualizationOut [26]
CollapseExpand
Grouped bars compare full-response and ending English shares for mixed, monolingual, and drifting fixtures.
Share of English labels in each hand-annotated response and in its last five words. The response that shifts to English has both languages overall and only English in its final window.

Data contamination and benchmark blindness

Two contamination-adjacent problems bite code-switched evaluation specifically.

Check benchmark provenance. GLUECoS and LinCE are public. Check for overlap where the training corpus is available and record which benchmark split you used. Report results on a documented held-out sample when available.

Check annotation policies. Before combining corpora, compare their treatment of loans and unknown words. State the adopted policy in the reported results.

GLUECoS and LinCE

The original GLUECoS paper covers Hindi–English and Spanish–English across language identification, part-of-speech tagging, named entity recognition, sentiment, question answering, and natural language inference. LinCE combines data for Spanish–English, Nepali–English, Hindi–English, and Modern Standard Arabic–Egyptian Arabic across four tasks. Report the pair, task, and split when comparing results.

Comments

Add to the conversation

A question, a correction, or an example from your own work.

No comments yet. Be the first to share your thoughts!

Newsletter

Stay up to date

Get new articles, book updates, and notes on AI and technology.

Unsubscribe at any time. Privacy policy