File size: 3,753 Bytes
24aaa76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""R5 DATA: check every manifest in DATA/data/processed/r5/manifests against the shared format.
Checks: header; allowed values of lang, script, kind, split, unit; text is NFC and has no tab or line break; ids unique across all manifests of the folder
(except the named subsets V1 and lmtune, which repeat validation ids on purpose); image path is absolute and exists; train/val/test value agrees with the file name.
Writes DATA/outputs/r5/manifest_validation.json. Usage: python -I r5_validate_manifests.py [manifest names to check only; then the output file is manifest_validation_partial.json]
"""
import collections
import json
import os
import sys
import unicodedata
from pathlib import Path

MAN = Path("/path/to/TURKIC_OCR_DATA/data/processed/r5/manifests")
HDR = ["id", "image", "text", "lang", "script", "kind", "source", "split", "group", "unit"]
LANG = {"kk", "ky", "uz_cyrl", "uz_latn", "tr", "az", "tt", "ba", "tk", "sah", "ru", "other"}
SCRIPT = {"Cyrl", "Latn"}
KIND = {"hand_real", "hand_synth", "typed_real", "typed_synth", "clutter"}
SPLIT = {"train", "val", "test"}
UNIT = {"word", "line", "page"}
SUBSETS = {"kohtd_screen_v1", "kohtd_lmtune", "kohtd_test_p2", "kohtd_test_p2b", "hkr_val_s2000", "chd_val_s2000", "school_val_s2000"}
res = {}
seen = {}
dups = collections.Counter()
ONLY = set(sys.argv[1:])
for p in sorted(MAN.glob("*.tsv")):
    if p.name.endswith(".meta.tsv"):  # FREEZE 21 Nov: GEN's per-row sidecars are not manifests (extra columns)
        continue
    name = p.stem
    if ONLY and name not in ONLY:
        continue
    bad = collections.Counter()
    n = 0
    with open(p, encoding="utf-8", newline="") as f:
        if f.readline().rstrip("\n").split("\t") != HDR:
            bad["bad_header"] += 1
        for line in f:
            parts = line.rstrip("\n").split("\t")
            n += 1
            if len(parts) != 10:
                bad["wrong_column_count"] += 1
                continue
            id_, img, text, lang, script, kind, source, split, group, unit = parts
            if unicodedata.normalize("NFC", text) != text:
                bad["text_not_nfc"] += 1
            if lang not in LANG:
                bad["bad_lang"] += 1
            if script not in SCRIPT:
                bad["bad_script"] += 1
            if kind not in KIND:
                bad["bad_kind"] += 1
            if split not in SPLIT:
                bad["bad_split"] += 1
            if unit not in UNIT:
                bad["bad_unit"] += 1
            if not img.startswith("/"):
                bad["image_not_absolute"] += 1
            elif not os.path.exists(img):
                bad["image_missing"] += 1
            for sfx, sp in (("_train", "train"), ("_val", "val"), ("_test", "test"), ("_test1", "test"), ("_test2", "test")):
                if name.endswith(sfx) and split != sp:
                    bad["split_value_vs_file_name"] += 1
            if name in ("kohtd_screen_v1", "kohtd_lmtune", "hkr_val_s2000", "chd_val_s2000", "school_val_s2000"):
                if split != "val":
                    bad["subset_not_val"] += 1
            if name not in SUBSETS:
                if id_ in seen:
                    dups[(name, seen[id_])] += 1
                else:
                    seen[id_] = name
    res[name] = {"rows": n, "problems": dict(bad)}
out = {"manifests": res, "duplicate_ids_across_manifests": {f"{a} ~ {b}": v for (a, b), v in dups.items()},
       "all_clean": all(not v["problems"] for v in res.values()) and not dups}
Path("/path/to/TURKIC_OCR_DATA/outputs/r5/" + ("manifest_validation_partial.json" if ONLY else "manifest_validation.json")).write_text(json.dumps(out, ensure_ascii=False, indent=1), encoding="utf-8")
print(json.dumps(out, ensure_ascii=False)[:3000])