"""Build the request stream from TAB (public train split, MIT): one request = one sentence of an ECHR judgment. Documents are shuffled (seed 0) and split by document: HOLDOUT_DOCS documents give the spot-check holdout, the rest give the stream. Gold spans (TAB annotator 1, the three format types) are kept for reference only; the demo learns from the teacher, never from gold. usage: prep.py TAB.parquet OUT_DIR """ import json import random import re import sys import pyarrow.parquet as pq TYPES = {"DATETIME", "CODE", "QUANTITY"} N_STREAM, N_HOLDOUT, HOLDOUT_DOCS = 3000, 400, 120 ABBR = r"(?= a2 and e <= a2 + len(t)}) rows[part].append({"doc_id": d["doc_id"], "text": t, "gold": [{"start": s, "end": e, "type": ty, "text": t[s:e]} for s, e, ty in gold]}) rng = random.Random(1) sel = {"stream": rng.sample(rows["stream"], N_STREAM), "holdout": rng.sample(rows["holdout"], N_HOLDOUT)} with open(f"{out}/requests.jsonl", "w") as f: for part, rs in sel.items(): for i, r in enumerate(rs): f.write(json.dumps({"id": f"{part[0]}{i:04d}", "part": part, **r}) + "\n") for part, rs in sel.items(): print(part, len(rs), "of", len(rows[part]), "docs", len({r["doc_id"] for r in rs}), "with gold", sum(1 for r in rs if r["gold"]), "gold spans", sum(len(r["gold"]) for r in rs)) if __name__ == "__main__": main(sys.argv[1], sys.argv[2])