File size: 3,159 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 | #!/usr/bin/env python3
"""Validate the PRINT manifests with the same rules as stage DATA's r5_validate_manifests.py (header, allowed
values, NFC, no tab or line break, absolute existing image path, ids unique), plus: ids unique against every
other manifest of the folder, and the 'paragraph' unit reported separately (DATA's list is word, line, page).
Writes the result to the path given as argument 1 (not to DATA's own validation file)."""
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"}
MINE = ["print_real_train", "print_real_val", "print_real_test", "more_real_print"]
res = {}
my_ids = {}
for name in MINE:
bad = collections.Counter()
n = 0
with open(MAN / (name + ".tsv"), 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 text == "":
bad["empty_text"] += 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_in_DATA_list:" + 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")):
if name.endswith(sfx) and split != sp:
bad["split_value_vs_file_name"] += 1
if id_ in my_ids:
bad["duplicate_id"] += 1
my_ids[id_] = name
res[name] = {"rows": n, "problems": dict(bad)}
# ids against the other manifests
others = collections.Counter()
for p in sorted(MAN.glob("*.tsv")):
if p.stem in MINE:
continue
with open(p, encoding="utf-8", newline="") as f:
next(f)
for line in f:
i = line.split("\t", 1)[0]
if i in my_ids:
others[p.stem] += 1
res["id_collisions_with_other_manifests"] = dict(others)
json.dump(res, open(sys.argv[1], "w", encoding="utf-8"), indent=1, ensure_ascii=False)
print(json.dumps(res, ensure_ascii=False))
|