"""Convert BIG-bench (tasksource mirror) + LogiQA + ReClor into Alea canonical rows. All are MCQ with exact labels -> choice rows. Group-disjoint dev via hash split.""" import hashlib, json, random, re from collections import Counter from datasets import load_dataset random.seed(7) D = "/root/alea/data/" rows, per_src, fps = [], Counter(), set() BB_CONFIGS = ["logical_deduction", "tracking_shuffled_objects", "web_of_lies", "boolean_expressions", "date_understanding", "temporal_sequences", "multistep_arithmetic_two", "navigate", "hyperbaton", "dyck_languages", "reasoning_about_colored_objects", "penguins_in_a_table", "formal_fallacies", "minute_mysteries_qa", "snarks", "analytic_entailment", "general_knowledge", "disambiguation_qa"] CAP = 2600 def gid(*xs): return hashlib.sha256("|".join(xs).encode()).hexdigest()[:16] for cfg in BB_CONFIGS: n = 0 for split in ("train", "validation"): try: ds = load_dataset("tasksource/bigbench", cfg, split=split, streaming=True) except Exception as e: print(cfg, split, "ERR", str(e)[:60]); continue for r in ds: if n >= CAP: break opts = [str(o).strip() for o in (r.get("multiple_choice_targets") or [])] sc = r.get("multiple_choice_scores") or [] if len(opts) < 2 or len(opts) != len(sc) or len(opts) > 14: # non-MCQ: fall back to noul verification on the text target t = r.get("targets") or [] if not t: continue ans = str(t[0]).strip() if len(ans) > 60 or not ans: continue correct = random.random() < 0.5 shown = ans if correct else (ans + "x" if ans else "x") if not correct and shown == ans: continue state = {"task": cfg.replace("_", " "), "problem": str(r["inputs"]).strip()[:1800]} qs = {"q": {"type": "noul", "instructions": f"Is the proposed answer correct? Proposed answer: {shown}", "criteria": {}}} tg = {"q": float(correct)} kind = "noul" else: ti = sc.index(max(sc)) state = {"task": cfg.replace("_", " "), "problem": str(r["inputs"]).strip()[:1800]} crit = {chr(65 + i): o for i, o in enumerate(opts)} qs = {"q": {"type": "choice", "instructions": "Choose the correct answer.", "criteria": crit}} vec = [0.0] * len(opts); vec[ti] = 1.0 tg = {"q": vec} g = f"bb:{cfg}:{r.get('idx', n)}" fp = gid(state["problem"], json.dumps(qs, sort_keys=True)) if fp in fps: continue fps.add(fp) rows.append({"state": state, "id": g, "questions": qs, "targets": tg, "source": f"bigbench:{cfg}", "group_id": g, "family": f"bigbench:{cfg}", "split0": split}) n += 1 per_src[cfg] += 1 print(cfg, per_src[cfg]) # LogiQA try: for r in load_dataset("lucasmccabe/logiqa", split="train", streaming=True): opts = [str(o).strip() for o in r["options"]] if len(opts) < 2 or len(opts) > 10: continue try: ti = int(r["correct_option"]) except Exception: continue state = {"context": str(r["context"]).strip()[:1400], "question": str(r["query"]).strip()} crit = {chr(65 + i): o for i, o in enumerate(opts)} vec = [0.0] * len(opts); vec[ti] = 1.0 g = f"logiqa:{r.get('id', len(rows))}" rows.append({"state": state, "id": g, "questions": {"q": {"type": "choice", "instructions": "Choose the logically correct answer.", "criteria": crit}}, "targets": {"q": vec}, "source": "logiqa", "group_id": g, "family": "logiqa"}) per_src["logiqa"] += 1 except Exception as e: print("logiqa ERR", e) # ReClor try: for r in load_dataset("metaeval/reclor", split="train", streaming=True): opts = [str(o).strip() for o in r["answers"]] lab = r.get("label") if lab is None or lab < 0 or len(opts) != 4: continue state = {"context": str(r["context"]).strip()[:1400], "question": str(r["question"]).strip()} crit = {chr(65 + i): o for i, o in enumerate(opts)} vec = [0.0] * 4; vec[lab] = 1.0 g = f"reclor:{r.get('id_string', len(rows))}" rows.append({"state": state, "id": g, "questions": {"q": {"type": "choice", "instructions": "Choose the logically correct answer.", "criteria": crit}}, "targets": {"q": vec}, "source": "reclor", "group_id": g, "family": "reclor"}) per_src["reclor"] += 1 except Exception as e: print("reclor ERR", e) random.shuffle(rows) dev = [r for r in rows if hashlib.md5(r["group_id"].encode()).hexdigest()[0] == "0"] train = [r for r in rows if hashlib.md5(r["group_id"].encode()).hexdigest()[0] != "0"] tg = {r["group_id"] for r in train} dev = [r for r in dev if r["group_id"] not in tg] open(D + "reason_train.jsonl", "w").write("".join(json.dumps(r, ensure_ascii=False) + "\n" for r in train)) open(D + "reason_dev.jsonl", "w").write("".join(json.dumps(r, ensure_ascii=False) + "\n" for r in dev[:4000])) print("train", len(train), "dev", min(4000, len(dev)), dict(per_src))