Download scripts/r5/r5_p2_slice.py from treoa/turkic-ocr-code: direct link, hf CLI and curl.
- Browser
- Download file 3.42 kB
-
https://huggingface.co/treoa/turkic-ocr-code/resolve/main/scripts/r5/r5_p2_slice.py
- Command line
-
hf download hf://treoa/turkic-ocr-code/scripts/r5/r5_p2_slice.py
-
curl -L -o r5_p2_slice.py https://huggingface.co/treoa/turkic-ocr-code/resolve/main/scripts/r5/r5_p2_slice.py
3.42 kB
| """R5 DATA: the unseen-word slice P2 (and P2b) of the KOHTD test, by version 3 section 1. | |
| P2 = KOHTD test crops whose normalised label does not occur among the normalised labels of any handwriting training crop | |
| (real handwriting train manifests plus hand_synth train manifests when they exist). | |
| P2b = P2 crops whose normalised label also equals no whitespace token of any training label of any kind | |
| (typed_synth, hand_synth, clutter excluded because empty). Reported only. | |
| Normalisation: NFC, lower case, keep only letters and digits (Unicode categories L* and N*). | |
| A test crop whose normalised label is empty (for example "-" or ".") belongs to neither slice. | |
| Reads only label text and manifests; opens no image. Writes kohtd_test_p2.tsv, kohtd_test_p2b.tsv and p2_slice_info.json. | |
| Run again when GEN's synthetic train manifests are frozen. Usage: python -I r5_p2_slice.py | |
| """ | |
| import json | |
| import time | |
| import unicodedata | |
| from pathlib import Path | |
| MAN = Path("/path/to/TURKIC_OCR_DATA/data/processed/r5/manifests") | |
| INFO = MAN.parent / "p2_slice_info.json" | |
| HDR = ["id", "image", "text", "lang", "script", "kind", "source", "split", "group", "unit"] | |
| HAND_TRAIN = ["kohtd_train", "hkr_train", "chd_train", "school_train", "synth_hand_train"] | |
| OTHER_TRAIN = ["synth_typed_train"] | |
| def norm(s): | |
| s = unicodedata.normalize("NFC", s).lower() | |
| return "".join(c for c in s if unicodedata.category(c)[0] in ("L", "N")) | |
| def read(p): | |
| rows = [] | |
| with open(p, encoding="utf-8", newline="") as f: | |
| assert f.readline().rstrip("\n").split("\t") == HDR | |
| for line in f: | |
| rows.append(line.rstrip("\n").split("\t")) | |
| return rows | |
| def write(p, rows): | |
| with open(p, "w", encoding="utf-8", newline="") as f: | |
| f.write("\t".join(HDR) + "\n") | |
| for r in rows: | |
| f.write("\t".join(r) + "\n") | |
| used, missing = {}, [] | |
| hand_labels = set() | |
| tokens = set() | |
| for name in HAND_TRAIN + OTHER_TRAIN: | |
| p = MAN / (name + ".tsv") | |
| if not p.exists(): | |
| missing.append(name) | |
| continue | |
| rows = read(p) | |
| used[name] = len(rows) | |
| for r in rows: | |
| n = norm(r[2]) | |
| if name in HAND_TRAIN and n: | |
| hand_labels.add(n) | |
| for tok in r[2].split(): | |
| t = norm(tok) | |
| if t: | |
| tokens.add(t) | |
| if n: | |
| tokens.add(n) | |
| test = read(MAN / "kohtd_test.tsv") | |
| p2, p2b, empty = [], [], 0 | |
| for r in test: | |
| n = norm(r[2]) | |
| if not n: | |
| empty += 1 | |
| continue | |
| if n not in hand_labels: | |
| p2.append(r) | |
| if n not in tokens: | |
| p2b.append(r) | |
| write(MAN / "kohtd_test_p2.tsv", p2) | |
| write(MAN / "kohtd_test_p2b.tsv", p2b) | |
| info = { | |
| "generated_utc": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()), | |
| "test_crops": len(test), "test_crops_with_empty_normalised_label": empty, | |
| "p2_crops": len(p2), "p2b_crops": len(p2b), | |
| "p2_forms": len({r[8] for r in p2}), "p2b_forms": len({r[8] for r in p2b}), | |
| "train_manifests_used_rows": used, "train_manifests_missing_at_run_time": missing, | |
| "distinct_normalised_handwriting_train_labels": len(hand_labels), | |
| "final": len(missing) == 0, | |
| "note": "P2 is provisional while any train manifest is missing: rerun after GEN's synth_hand_train.tsv and synth_typed_train.tsv exist.", | |
| } | |
| json.dump(info, open(INFO, "w", encoding="utf-8"), ensure_ascii=False, indent=1) | |
| print(json.dumps(info, ensure_ascii=False)) | |