"""Build reranker training groups {lang, query, passages[≤16], labels} from permissive sources. zh: THUIR/T2Ranking (Apache-2.0) graded qrels (0-3) + collection.tsv ja/ko/th/en: castorini/mr-tydi (Apache-2.0) train: positive + negative passages Any query whose text appears in the MIRACL reranking eval sets is dropped (no test leakage). Usage: python rr_prep.py [--zh 40000] [--seed 0] """ import argparse, csv, glob, gzip, json, os, random, sys csv.field_size_limit(sys.maxsize) def main(): ap = argparse.ArgumentParser(); ap.add_argument("rr_dir"); ap.add_argument("out") ap.add_argument("--zh", type=int, default=40000); ap.add_argument("--group", type=int, default=16); ap.add_argument("--seed", type=int, default=0) a = ap.parse_args(); r = random.Random(a.seed); d = a.rr_dir import pandas as pd eval_q = set() for f in glob.glob(f"{d}/miracl_rr/*-queries/*.parquet"): eval_q |= {str(t).strip() for t in pd.read_parquet(f)["text"]} print("eval queries to exclude:", len(eval_q)) out = open(a.out, "w"); stats = {} def emit(lang, q, pos, neg): if q.strip() in eval_q or not pos: return negs = r.sample(neg, min(len(neg), a.group - min(len(pos), 2))) ps = [(p, 1) for p in pos[:2]] + [(n, 0) for n in negs] r.shuffle(ps) out.write(json.dumps({"lang": lang, "query": q, "passages": [p for p, _ in ps], "labels": [l for _, l in ps]}, ensure_ascii=False) + "\n") stats[lang] = stats.get(lang, 0) + 1 # Mr.TyDi for lang, code in [("japanese", "ja"), ("korean", "ko"), ("thai", "th"), ("english", "en")]: rows = [json.loads(l) for l in gzip.open(f"{d}/mrtydi/mrtydi-v1.1-{lang}/train.jsonl.gz", "rt")] if code == "en": rows = r.sample(rows, min(4000, len(rows))) for x in rows: fmt = lambda p: (p.get("title", "") + "\n" + p["text"]).strip() emit(code, x["query"], [fmt(p) for p in x["positive_passages"]], [fmt(p) for p in x["negative_passages"]]) # T2Ranking (zh): pick queries having >=1 passage with rel>=2 and >=3 judged passages q = {row[0]: row[1] for row in csv.reader(open(f"{d}/t2/data/queries.train.tsv"), delimiter="\t") if row[0] != "qid"} judged = {} for row in csv.reader(open(f"{d}/t2/data/qrels.train.tsv"), delimiter="\t"): if row[0] == "qid": continue judged.setdefault(row[0], []).append((row[2], int(row[3]))) good = [qid for qid, js in judged.items() if len(js) >= 3 and any(rel >= 2 for _, rel in js) and qid in q] pick = r.sample(good, min(a.zh, len(good))); need = {pid for qid in pick for pid, _ in judged[qid]} text = {} for row in csv.reader(open(f"{d}/t2/data/collection.tsv"), delimiter="\t"): if row and row[0] in need: text[row[0]] = row[1] for qid in pick: js = [(text[p], rel) for p, rel in judged[qid] if p in text] pos = [t for t, rel in js if rel >= 2]; neg = [t for t, rel in js if rel <= 1] # T2Ranking has few judged negatives per query: add random passages from other queries as easy negatives while len(neg) < 7: neg.append(text[r.choice(list(need))]) emit("zh", q[qid], pos, neg) print(stats) if __name__ == "__main__": main()