Download scripts/r5_ref/ref_label_stats.py from treoa/turkic-ocr-code: direct link, hf CLI and curl.
- Browser
- Download file 1.51 kB
-
https://huggingface.co/treoa/turkic-ocr-code/resolve/main/scripts/r5_ref/ref_label_stats.py
- Command line
-
hf download hf://treoa/turkic-ocr-code/scripts/r5_ref/ref_label_stats.py
-
curl -L -o ref_label_stats.py https://huggingface.co/treoa/turkic-ocr-code/resolve/main/scripts/r5_ref/ref_label_stats.py
1.51 kB
| """R5 REF: text-only statistics of the KOHTD train and validation manifests (never opens the test manifest, never opens an image). | |
| Usage: python ref_label_stats.py <manifest_dir> <out_json> | |
| """ | |
| import collections | |
| import csv | |
| import json | |
| import sys | |
| import unicodedata | |
| mdir, out = sys.argv[1], sys.argv[2] | |
| res = {} | |
| for name in ("kohtd_train", "kohtd_val"): | |
| rows = list(csv.DictReader(open("%s/%s.tsv" % (mdir, name), encoding="utf-8"), delimiter="\t", quoting=csv.QUOTE_NONE)) | |
| lens = [len(r["text"]) for r in rows] | |
| chars = collections.Counter(c for r in rows for c in r["text"]) | |
| res[name] = { | |
| "rows": len(rows), | |
| "forms": len({r["group"] for r in rows}), | |
| "len_le_1": sum(1 for n in lens if n <= 1), | |
| "len_gt_25": sum(1 for n in lens if n > 25), | |
| "len_ge_48": sum(1 for n in lens if n >= 48), | |
| "len_max": max(lens), | |
| "with_space": sum(1 for r in rows if " " in r["text"]), | |
| "empty": sum(1 for n in lens if n == 0), | |
| "not_nfc": sum(1 for r in rows if unicodedata.normalize("NFC", r["text"]) != r["text"]), | |
| "n_distinct_chars": len(chars), | |
| "chars": {c: n for c, n in sorted(chars.items(), key=lambda x: -x[1])}, | |
| "unique_split_values": sorted({r["split"] for r in rows}), | |
| "unique_images_ext": sorted({r["image"].rsplit(".", 1)[-1] for r in rows}), | |
| } | |
| json.dump(res, open(out, "w"), ensure_ascii=False, indent=1) | |
| for k, v in res.items(): | |
| print(k, {a: b for a, b in v.items() if a != "chars"}) | |