Download ocr-tech/tests/test_audit_ctc_label_feasibility.py from prosperious/ocr-projects-archive: direct link, hf CLI and curl.
- Browser
- Download file 2.38 kB
-
https://huggingface.co/prosperious/ocr-projects-archive/resolve/main/ocr-tech/tests/test_audit_ctc_label_feasibility.py
- Command line
-
hf download hf://prosperious/ocr-projects-archive/ocr-tech/tests/test_audit_ctc_label_feasibility.py
-
curl -L -o test_audit_ctc_label_feasibility.py https://huggingface.co/prosperious/ocr-projects-archive/resolve/main/ocr-tech/tests/test_audit_ctc_label_feasibility.py
2.38 kB
| from __future__ import annotations | |
| import importlib.util | |
| from pathlib import Path | |
| import pytest | |
| ROOT = Path(__file__).resolve().parents[1] | |
| SPEC = importlib.util.spec_from_file_location( | |
| "audit_ctc_label_feasibility", | |
| ROOT / "tools" / "audit_ctc_label_feasibility.py", | |
| ) | |
| assert SPEC and SPEC.loader | |
| MODULE = importlib.util.module_from_spec(SPEC) | |
| SPEC.loader.exec_module(MODULE) | |
| def test_ctc_min_timesteps_counts_adjacent_equal_tokens() -> None: | |
| assert MODULE.ctc_min_timesteps("πππ«") == 4 | |
| assert MODULE.ctc_min_timesteps("ππ«π") == 3 | |
| assert MODULE.ctc_min_timesteps("πππ") == 5 | |
| def test_report_fails_impossible_label_and_passes_wider_pool(tmp_path: Path) -> None: | |
| dictionary = tmp_path / "dict.txt" | |
| dictionary.write_text("π\nπ«\n", encoding="utf-8") | |
| labels = tmp_path / "labels.txt" | |
| labels.write_text("a.png\tπππ«\nb.png\tππ«\n", encoding="utf-8") | |
| failed = MODULE.build_report( | |
| dictionary=dictionary, | |
| label_files=[("train", labels)], | |
| pool_width=3, | |
| max_text_length=2, | |
| use_space_char=False, | |
| ) | |
| assert failed["status"] == "FAIL" | |
| assert failed["splits"][0]["over_pool_width"] == 1 | |
| assert failed["splits"][0]["over_max_text_length"] == 1 | |
| passed = MODULE.build_report( | |
| dictionary=dictionary, | |
| label_files=[("train", labels)], | |
| pool_width=4, | |
| max_text_length=3, | |
| use_space_char=False, | |
| ) | |
| assert passed["status"] == "PASS" | |
| def test_multicodepoint_dictionary_is_rejected(tmp_path: Path) -> None: | |
| dictionary = tmp_path / "dict.txt" | |
| dictionary.write_text("ππ\n", encoding="utf-8") | |
| with pytest.raises(ValueError, match="exactly one codepoint"): | |
| MODULE.audit_dictionary(dictionary, use_space_char=False) | |
| def test_unknown_label_codepoint_is_a_failure(tmp_path: Path) -> None: | |
| dictionary = tmp_path / "dict.txt" | |
| dictionary.write_text("π\n", encoding="utf-8") | |
| labels = tmp_path / "labels.txt" | |
| labels.write_text("a.png\tπ\u00a0π\n", encoding="utf-8") | |
| report = MODULE.build_report( | |
| dictionary=dictionary, | |
| label_files=[("train", labels)], | |
| pool_width=10, | |
| max_text_length=10, | |
| use_space_char=True, | |
| ) | |
| assert report["status"] == "FAIL" | |
| assert report["splits"][0]["unknown_codepoints"] == ["U+00A0"] | |