Akahsizrr commited on
Commit
ff88fe9
·
verified ·
1 Parent(s): aa04508

Upload convert_tot.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. convert_tot.py +72 -0
convert_tot.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import ast, hashlib, json, random, re
2
+ from collections import Counter
3
+ from itertools import chain
4
+ from datasets import load_dataset
5
+
6
+ random.seed(42)
7
+ MAX_ROWS = 42000
8
+ rows, per_qt, fps = [], Counter(), set()
9
+
10
+ ds = chain(load_dataset("baharef/ToT", "tot_arithmetic", split="test", streaming=True),
11
+ load_dataset("baharef/ToT", "tot_semantic", split="test", streaming=True),
12
+ load_dataset("baharef/ToT", "tot_semantic_large", split="test", streaming=True))
13
+
14
+ NUM = re.compile(r"-?\d+")
15
+ for r in ds:
16
+ qt = r.get("question_type") or "misc"
17
+ q = str(r.get("question", "")).strip()
18
+ p = str(r.get("prompt", "")).strip()
19
+ lab = r.get("label")
20
+ if isinstance(lab, str):
21
+ try:
22
+ lab = ast.literal_eval(lab)
23
+ except Exception:
24
+ pass
25
+ ans = lab.get("answer") if isinstance(lab, dict) else (lab if isinstance(lab, str) else None)
26
+ if not q or not ans or len(p) > 1400:
27
+ continue
28
+ ans = str(ans).strip()
29
+ m = NUM.search(ans.replace(",", ""))
30
+ if not m:
31
+ continue
32
+ val = int(m.group(0))
33
+ # wrong candidates: same answer string, first number perturbed
34
+ def swap(a, v):
35
+ return NUM.sub(str(v), a, count=1)
36
+ cands = {val}
37
+ for d in (1, -1, 2, -2, 10, -10, 4, -4, 8, -8, 100, -100, 5, -5):
38
+ cands.add(val + d)
39
+ if len(cands) >= 4:
40
+ break
41
+ opts = [ans] + [swap(ans.replace(",", ""), x) for x in random.sample(sorted(cands - {val}), 3)]
42
+ random.shuffle(opts)
43
+ fp = hashlib.sha256(f"{p}|{q}|{ans}".encode()).hexdigest()
44
+ if fp in fps:
45
+ continue
46
+ fps.add(fp)
47
+ qq = re.sub(r"format your answer.*$", "", q, flags=re.I).strip()
48
+ st = {"context": p} if p else {"context": qq}
49
+ tgt = [0.0] * len(opts)
50
+ ti = opts.index(ans)
51
+ tgt[ti] = 1.0
52
+ gid = f"tot:{qt}:{fp[:12]}"
53
+ rows.append({"state": st, "id": f"tot:{qt}:{fp[:16]}", "kind": "choice",
54
+ "options": opts, "target": tgt,
55
+ "question": qq + " Choose the correct answer.",
56
+ "source": f"tot:{qt}", "group_id": gid})
57
+ per_qt[qt] += 1
58
+ if len(rows) >= MAX_ROWS:
59
+ break
60
+
61
+ random.shuffle(rows)
62
+ dev = [r for r in rows if hashlib.md5(r["group_id"].encode()).hexdigest()[0] == "0"]
63
+ train = [r for r in rows if hashlib.md5(r["group_id"].encode()).hexdigest()[0] != "0"]
64
+ tg = {r["group_id"] for r in train}
65
+ dev = [r for r in dev if r["group_id"] not in tg]
66
+ with open("/root/alea/data/tot_train.jsonl", "w") as f:
67
+ for r in train[:36000]:
68
+ f.write(json.dumps(r) + "\n")
69
+ with open("/root/alea/data/tot_dev.jsonl", "w") as f:
70
+ for r in dev[:3500]:
71
+ f.write(json.dumps(r) + "\n")
72
+ print({"train": min(36000, len(train)), "dev": min(3500, len(dev)), "qtypes": dict(per_qt)})