turkic-ocr-code / scripts /r5_ref /ref_label_stats.py
treoa's picture
Release 1
24aaa76
Raw History Blame Contribute Delete
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"})