Download canonicalize.py from Akahsizrr/alea-runs: direct link, hf CLI and curl.
- Browser
- Download file 1.64 kB
-
https://huggingface.co/datasets/Akahsizrr/alea-runs/resolve/main/canonicalize.py
- Command line
-
hf download hf://datasets/Akahsizrr/alea-runs/canonicalize.py
-
curl -L -o canonicalize.py https://huggingface.co/datasets/Akahsizrr/alea-runs/resolve/main/canonicalize.py
1.64 kB
| import json, sys | |
| D = "/root/alea/data/" | |
| def flat_to_canon(r): | |
| kind = r["kind"] | |
| if kind == "noul": | |
| qs = {"q": {"type": "noul", "instructions": r["question"], "criteria": {}}} | |
| tg = {"q": float(r["target"][1])} | |
| else: | |
| crit = {chr(65 + i): o for i, o in enumerate(r["options"])} | |
| qs = {"q": {"type": kind, "instructions": r["question"], "criteria": crit}} | |
| tg = {"q": [float(x) for x in r["target"]]} | |
| return {**{k: v for k, v in r.items() if k not in ("kind", "question", "options", "target")}, | |
| "questions": qs, "targets": tg} | |
| def listq_to_canon(r): | |
| qs, tg = {}, {} | |
| for j, q in enumerate(r["questions"]): | |
| qid = f"q{j}" | |
| t = q["kind"] | |
| if t == "noul": | |
| qs[qid] = {"type": "noul", "instructions": q["text"], "criteria": {}} | |
| tg[qid] = float(r["targets"][j][1]) | |
| else: | |
| crit = {chr(65 + i): o for i, o in enumerate(q["options"])} | |
| qs[qid] = {"type": t, "instructions": q["text"], "criteria": crit} | |
| tg[qid] = [float(x) for x in r["targets"][j]] | |
| return {**{k: v for k, v in r.items() if k not in ("questions", "targets")}, | |
| "questions": qs, "targets": tg} | |
| for fn in sys.argv[1:]: | |
| src, dst = D + fn, D + fn.replace(".jsonl", "") + ".canon.jsonl" | |
| n = 0 | |
| with open(dst, "w") as f: | |
| for line in open(src): | |
| r = json.loads(line) | |
| c = listq_to_canon(r) if isinstance(r.get("questions"), list) else flat_to_canon(r) | |
| f.write(json.dumps(c, ensure_ascii=False) + "\n") | |
| n += 1 | |
| print(fn, "->", dst.split("/")[-1], n) | |