Download scripts/release2/rel2_sakha_check.py from treoa/turkic-ocr-code: direct link, hf CLI and curl.
- Browser
- Download file 4.51 kB
-
https://huggingface.co/treoa/turkic-ocr-code/resolve/main/scripts/release2/rel2_sakha_check.py
- Command line
-
hf download hf://treoa/turkic-ocr-code/scripts/release2/rel2_sakha_check.py
-
curl -L -o rel2_sakha_check.py https://huggingface.co/treoa/turkic-ocr-code/resolve/main/scripts/release2/rel2_sakha_check.py
4.51 kB
| """RELEASE2 check: can a released Sakha item still carry newspaper (ailab) text that its line attribution missed? | |
| The release gate removed items whose source_doc names an ailab document. Attribution is a substring heuristic: a line marked | |
| 'none' (no corpus match) or 'split' (first match per half only) could still come from a newspaper sentence. For every released | |
| sah item with confidence none or split and at least 2 words, and for every line of every released sah page, test the label | |
| against every ailab sentence drawn for that page (over-inclusive: the bank also records sentences drawn only to test fonts): | |
| exact = the label (trailing hyphen removed) is a substring of an ailab sentence | |
| window = some 3 consecutive words of the label occur in an ailab sentence | |
| Output: DATA/tmp/release2/sakha_check.json (counts per kind and split, ids with hits). Reads only released labels and the | |
| generator's sentence lists; no real-data test label. | |
| usage: CODE/.venv_gen/bin/python -I rel2_sakha_check.py""" | |
| import csv | |
| import json | |
| import os | |
| import re | |
| from collections import Counter, defaultdict | |
| csv.field_size_limit(10 ** 9) | |
| D = "/path/to/TURKIC_OCR_DATA" | |
| PKG = D + "/release/rc1_data" | |
| TEXT = D + "/data/processed/r5/pagesynth_text_split" | |
| REGEN = D + "/tmp/release2/regen/pages.jsonl" | |
| RX_LINE = re.compile(r"^(hs|ts|cl)_(train|val|test)_(.+)_(\d{6})_(\d{2})$") | |
| RX_PAGE = re.compile(r"^pg_(train|val|test)_[a-z]+_(.+)_[lmh]_(\d{6})$") | |
| drawn = {} | |
| with open(REGEN, encoding="utf-8") as f: | |
| for line in f: | |
| if '"lang": "sah"' not in line: | |
| continue | |
| r = json.loads(line) | |
| drawn[(r["split"], r["pop"], r["idx"])] = [d for d in (r.get("drawn_docs") or []) if d.startswith("ailab:")] | |
| todo = [] # (kind, split, item id, page key, list of label strings) | |
| for kind in ("hand", "typed", "pages"): | |
| for sp in ("train", "val", "test"): | |
| p = os.path.join(PKG, "labels", "%s_%s.jsonl" % (kind, sp)) | |
| with open(p, encoding="utf-8") as f: | |
| for line in f: | |
| r = json.loads(line) | |
| if r["language"] != "sah": | |
| continue | |
| if kind == "pages": | |
| m = RX_PAGE.match(r["id"]) | |
| key = (sp, "H", int(m.group(3))) | |
| pj = json.load(open(os.path.join(PKG, r["page_json"]), encoding="utf-8")) | |
| labels = [ln["text"] for ln in pj.get("lines", []) if len(ln.get("text", "").split()) >= 2] | |
| todo.append((kind, sp, r["id"], key, labels)) | |
| elif r.get("text_source_confidence") in ("none", "split") and len(r["text"].split()) >= 2: | |
| m = RX_LINE.match(r["id"]) | |
| key = (sp, "T" if m.group(1) == "ts" else "H", int(m.group(4))) | |
| todo.append((kind, sp, r["id"], key, [r["text"]])) | |
| need = defaultdict(set) | |
| for kind, sp, iid, key, labels in todo: | |
| for d in drawn.get(key, []): | |
| need[sp].add(d) | |
| sent = defaultdict(list) | |
| for sp, ids in need.items(): | |
| with open(os.path.join(TEXT, sp, "sah", "sentences.tsv"), encoding="utf-8") as f: | |
| for line in f: | |
| src, _, s = line.rstrip("\n").partition("\t") | |
| if src in ids: | |
| sent[(sp, src)].append(s) | |
| counts = defaultdict(Counter) | |
| hits = [] | |
| for kind, sp, iid, key, labels in todo: | |
| ss = [s for d in drawn.get(key, []) for s in sent.get((key[0], d), [])] | |
| counts["%s|%s" % (kind, sp)]["checked_items"] += 1 | |
| counts["%s|%s" % (kind, sp)]["checked_labels"] += len(labels) | |
| ex = win = False | |
| for lab in labels: | |
| t = lab.rstrip("-").strip() | |
| if any(t in s for s in ss): | |
| ex = True | |
| ws = t.split() | |
| for i in range(len(ws) - 2): | |
| w3 = " ".join(ws[i:i + 3]) | |
| if any(w3 in s for s in ss): | |
| win = True | |
| break | |
| if ex: | |
| counts["%s|%s" % (kind, sp)]["exact_hit_items"] += 1 | |
| if win: | |
| counts["%s|%s" % (kind, sp)]["window_hit_items"] += 1 | |
| if ex or win: | |
| hits.append(dict(id=iid, kind=kind, split=sp, exact=ex, window=win)) | |
| out = dict(counts={k: dict(v) for k, v in counts.items()}, n_hits=len(hits), hits=hits, | |
| pages_with_ailab_candidates=sum(1 for v in drawn.values() if v), ailab_sentences_loaded=sum(len(v) for v in sent.values())) | |
| json.dump(out, open(D + "/tmp/release2/sakha_check.json", "w", encoding="utf-8"), ensure_ascii=False, indent=1) | |
| print(json.dumps(dict(counts=out["counts"], n_hits=len(hits)), ensure_ascii=False)) | |