Download text-layer/ocr-inspector/study/join_native.py from foliokestrel/folio-orient: direct link, hf CLI and curl.
- Browser
- Download file 2.35 kB
-
https://huggingface.co/foliokestrel/folio-orient/resolve/main/text-layer/ocr-inspector/study/join_native.py
- Command line
-
hf download hf://foliokestrel/folio-orient/text-layer/ocr-inspector/study/join_native.py
-
curl -L -o join_native.py https://huggingface.co/foliokestrel/folio-orient/resolve/main/text-layer/ocr-inspector/study/join_native.py
2.35 kB
| """Join frozen visual judgments to both native-page methods.""" | |
| import argparse | |
| import csv | |
| import hashlib | |
| import json | |
| from collections import Counter | |
| from pathlib import Path | |
| def records(path): | |
| return [json.loads(line) for line in path.read_text().splitlines()] | |
| def by_page(rows): | |
| result = {(r["identifier"], r["page"]): r for r in rows} | |
| assert len(result) == len(rows) | |
| return result | |
| def outcome(angle): | |
| if angle is None: | |
| return "inconclusive" | |
| return "original" if angle == 0 else "warning" | |
| def main(root, ink, contrast): | |
| reviews = [] | |
| for group in ("transfer", "catalogue"): | |
| labels = root / f"{group}-labels.tsv" | |
| digest = (root / f"{group}-labels.sha256").read_text().split()[0] | |
| assert hashlib.sha256(labels.read_bytes()).hexdigest() == digest | |
| with labels.open() as f: | |
| judgments = {r["blind"]: r for r in csv.DictReader(f, delimiter="\t")} | |
| for page in records(root / f"{group}-key.jsonl"): | |
| reviews.append(dict(**page, **{k:v for k,v in judgments.pop(page["blind"]).items() if k != "blind"})) | |
| assert not judgments | |
| reviews.extend(dict(**p, blind=None, placement="unavailable", evidence="No usable invisible alphanumeric boxes.") | |
| for p in records(root / "catalogue-no-boxes.jsonl")) | |
| joined = by_page(reviews) | |
| for method, path in (("ink", ink), ("contrast", contrast)): | |
| scores = by_page(records(path)) | |
| assert joined.keys() == scores.keys() and len(scores)==48 | |
| for key, r in scores.items(): | |
| native = r["native"] | |
| joined[key][method+"_cw"] = native["placement_cw"] | |
| joined[key]["sampled"] = native["sampled"] | |
| assert bool(native["sampled"]) == (joined[key]["placement"] != "unavailable") | |
| print(method, sorted(Counter((r["placement"],outcome(r[method+"_cw"])) for r in joined.values()).items())) | |
| (root / "native-joined.jsonl").write_text("".join(json.dumps(joined[k])+"\n" for k in sorted(joined))) | |
| for r in joined.values(): | |
| if r["placement"] in ("mixed","uncertain") or r["ink_cw"]: | |
| print(r) | |
| if __name__=="__main__": | |
| parser=argparse.ArgumentParser(description=__doc__) | |
| for name in ("root","ink","contrast"): | |
| parser.add_argument(name,type=Path) | |
| main(**vars(parser.parse_args())) | |