Upload canonicalize.py with huggingface_hub
Browse files- canonicalize.py +44 -0
canonicalize.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json, sys
|
| 2 |
+
|
| 3 |
+
D = "/root/alea/data/"
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def flat_to_canon(r):
|
| 7 |
+
kind = r["kind"]
|
| 8 |
+
if kind == "noul":
|
| 9 |
+
qs = {"q": {"type": "noul", "instructions": r["question"], "criteria": {}}}
|
| 10 |
+
tg = {"q": float(r["target"][1])}
|
| 11 |
+
else:
|
| 12 |
+
crit = {chr(65 + i): o for i, o in enumerate(r["options"])}
|
| 13 |
+
qs = {"q": {"type": kind, "instructions": r["question"], "criteria": crit}}
|
| 14 |
+
tg = {"q": [float(x) for x in r["target"]]}
|
| 15 |
+
return {**{k: v for k, v in r.items() if k not in ("kind", "question", "options", "target")},
|
| 16 |
+
"questions": qs, "targets": tg}
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def listq_to_canon(r):
|
| 20 |
+
qs, tg = {}, {}
|
| 21 |
+
for j, q in enumerate(r["questions"]):
|
| 22 |
+
qid = f"q{j}"
|
| 23 |
+
t = q["kind"]
|
| 24 |
+
if t == "noul":
|
| 25 |
+
qs[qid] = {"type": "noul", "instructions": q["text"], "criteria": {}}
|
| 26 |
+
tg[qid] = float(r["targets"][j][1])
|
| 27 |
+
else:
|
| 28 |
+
crit = {chr(65 + i): o for i, o in enumerate(q["options"])}
|
| 29 |
+
qs[qid] = {"type": t, "instructions": q["text"], "criteria": crit}
|
| 30 |
+
tg[qid] = [float(x) for x in r["targets"][j]]
|
| 31 |
+
return {**{k: v for k, v in r.items() if k not in ("questions", "targets")},
|
| 32 |
+
"questions": qs, "targets": tg}
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
for fn in sys.argv[1:]:
|
| 36 |
+
src, dst = D + fn, D + fn.replace(".jsonl", "") + ".canon.jsonl"
|
| 37 |
+
n = 0
|
| 38 |
+
with open(dst, "w") as f:
|
| 39 |
+
for line in open(src):
|
| 40 |
+
r = json.loads(line)
|
| 41 |
+
c = listq_to_canon(r) if isinstance(r.get("questions"), list) else flat_to_canon(r)
|
| 42 |
+
f.write(json.dumps(c, ensure_ascii=False) + "\n")
|
| 43 |
+
n += 1
|
| 44 |
+
print(fn, "->", dst.split("/")[-1], n)
|