File size: 2,904 Bytes
d816d7c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | import ast, hashlib, json, random
from collections import Counter, defaultdict
from datasets import load_dataset
random.seed(42)
MAX_PER_SOURCE = 200
MAX_ROWS = 90000
MAX_OPT = 12
def load_side(ds, rows, per_source, group_ids, fingerprints):
for r in ds:
src = r["source"]
if per_source[src] >= MAX_PER_SOURCE:
continue
try:
opts = ast.literal_eval(r["options"]) if isinstance(r["options"], str) else r["options"]
tgt = ast.literal_eval(r["target"]) if isinstance(r["target"], str) else r["target"]
except Exception:
continue
if not isinstance(opts, list) or not (2 <= len(opts) <= MAX_OPT):
continue
if not isinstance(tgt, list) or len(tgt) != len(opts):
continue
if sum(1 for t in tgt if t and t > 0) != 1:
continue
try:
tgt = [float(t) for t in tgt]
except Exception:
continue
st = r["state"] if isinstance(r["state"], str) else json.dumps(r["state"], sort_keys=True)
fp = hashlib.sha256(f"{st}|{r['question']}|{opts}".encode()).hexdigest()
if fp in fingerprints:
continue
fingerprints.add(fp)
group_ids.add(r["group_id"])
rows.append({"state": st, "id": r["id"], "kind": r["kind"],
"options": [str(o) for o in opts], "target": tgt,
"question": r["question"], "source": f"tasksource:{src}",
"group_id": r["group_id"]})
per_source[src] += 1
if len(rows) >= MAX_ROWS:
return True
return False
rows, per_source, group_ids, fps = [], Counter(), set(), set()
print("streaming train...")
done = load_side(load_dataset("tasksource/tasksource-jev-typed-decisions", split="train", streaming=True),
rows, per_source, group_ids, fps)
random.shuffle(rows)
train = [r for r in rows if not r["source"].startswith("DEVMARK")]
dev = [r for r in rows if r["source"].startswith("DEVMARK")]
train, dev = [r for r in rows if hashlib.md5(r["group_id"].encode()).hexdigest()[0] != "0"], \
[r for r in rows if hashlib.md5(r["group_id"].encode()).hexdigest()[0] == "0"]
train, dev = train[:76000], dev[:6000]
used_groups = {r["group_id"] for r in train}
dev = [r for r in dev if r["group_id"] not in used_groups]
with open("/root/alea/data/tasksource_train.jsonl", "w") as f:
for r in train:
f.write(json.dumps(r) + "\n")
with open("/root/alea/data/tasksource_dev.jsonl", "w") as f:
for r in dev:
f.write(json.dumps(r) + "\n")
rep = {"train": len(train), "dev": len(dev), "sources": len(per_source),
"kinds": Counter(r["kind"] for r in train + dev),
"top_sources": per_source.most_common(20)}
open("/root/alea/data/tasksource_conversion_report.json", "w").write(json.dumps(rep, indent=2, default=str))
print(rep)
|