"""The ink corpus's one line format, shared by every `corpus-<dataset>.py`.A line is one letter or digit written by one person, as the ink harness reads it: {"strokes": [[[x, y, t], ...], ...], "label": "h", "case": "upper", "writer": "<id>", "source": "<dataset>", "input": "stylus"}Points are in pt, y down, the letter's box moved to the origin and scaled soits longer side is `SIDE` (what a thumb writes on a phone card); t is ms fromthe group's first touch. A letter's label is lower case (search is case-free)and `case` keeps the case written, `lower` or `upper`; a digit's label is thedigit and its `case` is `digit`. Repeated points (a tablet reporting the sameplace twice) are dropped. A symbol with no extent (a lone tap) is noneand is counted as skipped, never written."""import jsonimport osfrom collections importCounterSIDE = 120.0# Where a dataset has no timing: a point every 8 ms, 450 ms between strokes.POINT_MS = 8GAP_MS = 450def deduped(points): out = []for p in points:ifnot out or (p[0], p[1]) != (out[-1][0], out[-1][1]): out.append(p)return outdef case_of(symbol):"""`digit`, `upper` or `lower`: the case a symbol was written in."""return"digit"if symbol.isdigit() else"upper"if symbol.isupper() else"lower"def line(strokes, letter, writer, source, input_kind):"""One corpus line from `strokes` ([[x, y, t_or_None], ...] each, in the dataset's units, y down), or None when it has no extent. Timing is the dataset's when every point has one, else synthesised.""" strokes = [deduped(s) for s in strokes] strokes = [s for s in strokes if s] xs = [p[0] for s in strokes for p in s] ys = [p[1] for s in strokes for p in s]ifnot xs:returnNone left, top = min(xs), min(ys) longer = max(max(xs) - left, max(ys) - top)if longer <= 0:returnNone scale = SIDE / longer timed = all(p[2] isnotNonefor s in strokes for p in s) first = strokes[0][0][2] if timed else0 out, clock = [], -GAP_MSfor s in strokes: clock += GAP_MS - POINT_MS stroke = []for p in s: clock += POINT_MS t = p[2] - first if timed else clock stroke.append([round((p[0] - left) * scale, 1), round((p[1] - top) * scale, 1), int(t)]) out.append(stroke)return {"strokes": out,"label": letter.lower(),"case": case_of(letter),"writer": writer,"source": source,"input": input_kind, }def write(rows, skipped, out_dir, dataset, timing):"""`<out_dir>/<dataset>.jsonl`, `<dataset>.labels.json` (the labels in line order) and `<dataset>.stats.json` (counts, for the README).""" os.makedirs(out_dir, exist_ok=True)with open(os.path.join(out_dir, f"{dataset}.jsonl"), "w") as f:for row in rows: f.write(json.dumps(row, separators=(",", ":")) + "\n")with open(os.path.join(out_dir, f"{dataset}.labels.json"), "w") as f: json.dump([row["label"] for row in rows], f) per = Counter((row["label"], row["case"]) for row in rows) cases = {label: ("digit",) if label.isdigit() else ("lower", "upper") for label in {row["label"] for row in rows}} stats = {"dataset": dataset,"lines": len(rows),"skipped": skipped,"timing": timing,"writers": len({row["writer"] for row in rows}),"inputs": dict(Counter(row["input"] for row in rows)),"per_symbol": {label: {case: per[(label, case)] for case in cases[label]} for label in sorted(cases)}, }with open(os.path.join(out_dir, f"{dataset}.stats.json"), "w") as f: json.dump(stats, f, indent=1)return stats