Download scripts/r5_linedet/gate_unittest.py from treoa/turkic-ocr-code: direct link, hf CLI and curl.
- Browser
- Download file 1.47 kB
-
https://huggingface.co/treoa/turkic-ocr-code/resolve/main/scripts/r5_linedet/gate_unittest.py
- Command line
-
hf download hf://treoa/turkic-ocr-code/scripts/r5_linedet/gate_unittest.py
-
curl -L -o gate_unittest.py https://huggingface.co/treoa/turkic-ocr-code/resolve/main/scripts/r5_linedet/gate_unittest.py
1.47 kB
| """Unit test of linedet.lm_gate on a toy folder (no real prediction file): plain outputs '' , ' ', ' ', 'a', and LM outputs with text. Expect the gated output empty for the first three.""" | |
| import csv, os, sys, tempfile | |
| sys.path.insert(0, "/path/to/TURKIC_OCR_CODE/src") | |
| import importlib | |
| gate_one = importlib.import_module("linedet." + (sys.argv[1] if len(sys.argv) > 1 else "lm_gate")).gate_one | |
| cols = ["id", "ref", "hyp", "conf", "conf_min", "hit_cap"] | |
| def w(path, rows): | |
| os.makedirs(os.path.dirname(path), exist_ok=True) | |
| with open(path, "w", newline="", encoding="utf-8") as f: | |
| f.write("\t".join(cols) + "\n") | |
| for r in rows: | |
| f.write("\t".join(str(x) for x in r) + "\n") | |
| T = tempfile.mkdtemp(dir="/path/to/TURKIC_OCR_DATA/tmp") | |
| w(T + "/X/ck/toy.tsv", [("1", "", "", 0.9, 0.9, 0), ("2", "", " ", 0.2, 0.2, 0), ("3", "", " ", 0.2, 0.2, 0), ("4", "ab", "a", 0.9, 0.9, 0), ("5", "", " ", 0.1, 0.1, 0)]) | |
| w(T + "/X/ck_lm/toy.tsv", [("1", "", "na", -3, -3, 0), ("2", "", "na", -3, -3, 0), ("3", "", "na", -3, -3, 0), ("4", "ab", "ab", -1, -1, 0), ("5", "", "na", -3, -3, 0)]) | |
| print("sets gated:", gate_one(T, "X", "ck")) | |
| rows = list(csv.DictReader(open(T + "/X/ck_lm_gated/toy.tsv", encoding="utf-8"), delimiter="\t", quoting=csv.QUOTE_NONE)) | |
| print([(r["id"], r["hyp"]) for r in rows]) | |
| assert [r["hyp"] for r in rows] == ["", "", "", "ab", ""], "gate still lets text through" | |
| print("UNIT TEST PASSED") | |
| import shutil; shutil.rmtree(T) | |