# K1 (runs on the cluster): build the KOHTD form-disjoint split exactly as OUT/split_spec_v1.txt says. # usage: python k1_build_split.py # scan_map.tsv: header, then "stemsheet" for scan-named crops shown to be cuts of a sheet (their split = that sheet's form's split) # Text only: no image is read. import collections import hashlib import os import re import sys labels_path, out_dir, chk_path, key_mode, scan_map_path = sys.argv[1:6] PREFIX = "KOHTD-split-v1-2026-11-20:" KAZ = "әғқңөұүһі" KAZ_UP = "ӘҒҚҢӨҰҮҺІ" def bucket(key): h = hashlib.sha256((PREFIX + str(key)).encode("utf-8")).hexdigest()[:8] v = int(h, 16) % 100 return "train" if v < 70 else ("validation" if v < 85 else "test") num = re.compile(r"^(\d+)_(\d+)_(\d+)$") scan = re.compile(r"^word_croped_(.+)_(-?\d+)_(\d+)$") rows = [] with open(labels_path, encoding="utf-8") as f: next(f) for line in f: stem, _, lab = line.rstrip("\n").partition("\t") rows.append((stem, lab)) forms = set() for stem, lab in rows: m = num.match(stem) if m: forms.add(int(m.group(1))) scan_map = {} if scan_map_path != "none": with open(scan_map_path, encoding="utf-8") as f: next(f) for line in f: a = line.rstrip("\n").split("\t") scan_map[a[0]] = int(a[1]) def split_of_form(fid): key = fid if key_mode == "form" else fid // 10 return bucket(key) os.makedirs(out_dir, exist_ok=True) outs = {s: open(os.path.join(out_dir, s + ".tsv"), "w", encoding="utf-8") for s in ("train", "validation", "test")} for o in outs.values(): o.write("crop_file\tform_id\tlabel\tkey_type\n") crops = collections.Counter() form_set = collections.defaultdict(set) chars = collections.defaultdict(collections.Counter) nchars = collections.Counter() scan_info = collections.Counter() for stem, lab in rows: m = num.match(stem) if m: fid = int(m.group(1)) sp = split_of_form(fid) outs[sp].write("%s.jpg\t%d\t%s\tnumbered\n" % (stem, fid, lab)) form_set[sp].add(fid) key_type = "numbered" else: m2 = scan.match(stem) if not m2: continue if stem in scan_map and scan_map[stem] in forms: fid = scan_map[stem] sp = split_of_form(fid) outs[sp].write("%s.jpg\t%d\t%s\tscan_named_cut_of_sheet\n" % (stem, fid, lab)) scan_info["scan crops placed with their sheet's form"] += 1 else: sp = "train" outs[sp].write("%s.jpg\tNA\t%s\tscan_named_train_only\n" % (stem, lab)) scan_info["scan crops train only"] += 1 key_type = "scan" crops[(sp, key_type)] += 1 for ch in lab: if ch in KAZ or ch in KAZ_UP: chars[sp][ch] += 1 nchars[sp] += len(lab) for o in outs.values(): o.close() # excluded sheets: sheet ids 0..1925 without numbered crops with open(os.path.join(out_dir, "excluded_sheets.tsv"), "w", encoding="utf-8") as f: f.write("sheet\tsheet_file\treason\n") for i in range(1926): if i not in forms: f.write("%d\t%d.jpg\tno numbered crops (unlabelled page)\n" % (i, i)) with open(os.path.join(out_dir, "split_stats.tsv"), "w", encoding="utf-8") as f: f.write("split\tforms\tnumbered_crops\tscan_named_crops\ttotal_crops\tlabel_characters\n") tot_num = sum(v for (s, k), v in crops.items() if k == "numbered") for sp in ("train", "validation", "test"): n_num = crops[(sp, "numbered")] n_scan = crops[(sp, "scan")] f.write("%s\t%d\t%d\t%d\t%d\t%d\n" % (sp, len(form_set[sp]), n_num, n_scan, n_num + n_scan, nchars[sp])) f.write("\nlanguage-specific letters (counts in the labels of each split)\nletter\ttrain\tvalidation\ttest\n") for ch in KAZ + KAZ_UP: f.write("%s\t%d\t%d\t%d\n" % (ch, chars["train"][ch], chars["validation"][ch], chars["test"][ch])) f.write("all_9_letters_lower\t%d\t%d\t%d\n" % tuple(sum(chars[sp][c] for c in KAZ) for sp in ("train", "validation", "test"))) f.write("all_9_letters_upper\t%d\t%d\t%d\n" % tuple(sum(chars[sp][c] for c in KAZ_UP) for sp in ("train", "validation", "test"))) f.write("all_18\t%d\t%d\t%d\n" % tuple(sum(chars[sp][c] for c in KAZ + KAZ_UP) for sp in ("train", "validation", "test"))) f.write("\nscan-named crops: %s\n" % dict(scan_info)) f.write("key mode: %s; forms with numbered crops: %d; excluded sheets: %d\n" % (key_mode, len(forms), 1926 - len(forms))) # checksums with open(chk_path, "w") as c: for name in ("train.tsv", "validation.tsv", "test.tsv", "excluded_sheets.tsv", "split_stats.tsv", "split_spec_v1.txt"): p = os.path.join(out_dir, name) if not os.path.exists(p): continue h = hashlib.sha256() with open(p, "rb") as g: for chunk in iter(lambda: g.read(1 << 20), b""): h.update(chunk) c.write("%s %s\n" % (h.hexdigest(), p)) print(open(os.path.join(out_dir, "split_stats.tsv"), encoding="utf-8").read())