#!/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))