"""Robustness pass (no inference): for every atlas/math/rewrite row add lid_noquote GlotLID profile after dropping segments that quote the prompt (passage/options/problem) lingua_eng English share by an independent detector (Lingua) lingua_eng_noquote the same, quote-stripped Rows are rewritten in place. Usage: python relid.py""" import json, os from concurrent.futures import ThreadPoolExecutor from datasets import load_dataset from lid import profile, lingua_eng_share from atlas import LANGS as ATLAS_LANGS, mc_prompt tps = json.load(open("data/atlas/templates.json", encoding="utf-8")) par = json.load(open("data/math/parallel.json", encoding="utf-8")) bel = {} def belebele(lang): if lang not in bel: bel[lang] = {(r["link"], r["question_number"]): r for r in load_dataset("facebook/belebele", lang, split="test")} return bel[lang] def prompt_of(r): if "link" in r: return mc_prompt(belebele(r["lang"])[(r["link"], r["qnum"])], tps[r["lang"]]) return par[str(r["id"])][r["lang"]]["text"] def enrich(r): p = prompt_of(r) r["lid_noquote"] = profile(r["reasoning"], r["lang"], exclude=p) r["lingua_eng"] = lingua_eng_share(r["reasoning"]) r["lingua_eng_noquote"] = lingua_eng_share(r["reasoning"], exclude=p) return r for path in ("data/atlas/runs_low.jsonl", "data/atlas/runs_high.jsonl", "data/math/runs_low.jsonl", "data/math/runs_high.jsonl"): if not os.path.exists(path): continue rows = [json.loads(l) for l in open(path, encoding="utf-8")] for lang in {r["lang"] for r in rows if "link" in r}: belebele(lang) # load once, single-threaded with ThreadPoolExecutor(4) as ex: rows = list(ex.map(enrich, rows)) with open(path, "w", encoding="utf-8") as f: f.writelines(json.dumps(r, ensure_ascii=False) + "\n" for r in rows) print("enriched", path, len(rows))