diff --git a/pyproject.toml b/pyproject.toml index efb708c..af9791a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -66,7 +66,7 @@ easyocr = ["easyocr>=1.7"] # depends on easyocr itself, so this extra pulls that in too. 0.8 ships the # v6 weights and the word list the backend decodes with (brief 013); the # backend falls back to greedy decoding on older releases. -armenian = ["tetrak-easyocr-armenian>=0.8"] +armenian = ["tetrak-easyocr-armenian>=0.11"] paddle = ["paddleocr>=3.0", "paddlepaddle>=3.0"] # PaddleOCR-VL, the document vision-language model in the same package. # The class lives in paddleocr, but the pipeline it builds needs paddlex's diff --git a/src/tetrak_ocr/backends/armenian.py b/src/tetrak_ocr/backends/armenian.py index 0be1bf5..57c8164 100644 --- a/src/tetrak_ocr/backends/armenian.py +++ b/src/tetrak_ocr/backends/armenian.py @@ -21,7 +21,13 @@ twin of an Armenian character -- `h` for `հ`, a colon for the Armenian full stop `։`. `tetrak_hy.fold_script` folds those back within any token that already holds an Armenian letter. It is worth about +0.035 word -recall and costs one function call. +recall and costs one function call. Since the recogniser learnt Cyrillic +(brief 014) the fold also reads every other token in the one script, +Latin or Cyrillic, it belongs to; a token made only of glyphs the two +share (`роог` for `poor`) is decided by the text around it, so each +region is folded with the whole page as `context`. That needs +`tetrak-easyocr-armenian` 0.9 or later; the extra pins 0.11, the release +with the three-way script decision. **The word list.** Where the installed `tetrak-easyocr-armenian` has released one, `tetrak_hy.lexicon` corrects out-of-vocabulary words from @@ -109,11 +115,15 @@ def _spans(results) -> list[TextSpan]: EasyOCR gives each region as a four-point polygon; `TextSpan` wants an axis-aligned box, so the polygon's extent is taken. The fold is applied here, at the point the text enters the pipeline, so everything - downstream sees Armenian rather than a mixture of scripts. + downstream sees Armenian rather than a mixture of scripts. Every + region is folded with the whole page's text as context: a one-word + region of shared Latin/Cyrillic glyphs has nothing in itself to say + which script it is, and the page around it has. """ + page = "\n".join(text for _, text, _ in results) return [ TextSpan( - text=tetrak_hy.fold_script(text), + text=tetrak_hy.fold_script(text, context=page), bbox=polygon_extent(box), # EasyOCR reports 0-1; TextSpan documents 0-100. confidence=float(confidence) * 100, diff --git a/tests/ocr/test_armenian.py b/tests/ocr/test_armenian.py index e4f85d2..fbe1621 100644 --- a/tests/ocr/test_armenian.py +++ b/tests/ocr/test_armenian.py @@ -38,8 +38,10 @@ def stub_tetrak_hy(monkeypatch: pytest.MonkeyPatch): installed package. """ module = types.ModuleType("tetrak_hy") + module.contexts = [] - def fold_script(text: str) -> str: + def fold_script(text: str, *, context: str | None = None) -> str: + module.contexts.append(context) return " ".join( token.replace("h", "հ") if any("԰" <= c <= "֏" for c in token) else token for token in text.split() @@ -82,6 +84,13 @@ def test_the_fold_is_applied_to_every_region(self, stub_tetrak_hy) -> None: spans = armenian._spans([region("hայ", 0, 0, 10, 10)]) assert [s.text for s in spans] == ["հայ"] + def test_every_region_is_folded_with_the_whole_page_as_context(self, stub_tetrak_hy) -> None: + """A one-word region of shared Latin/Cyrillic glyphs cannot say which + script it is; the rest of the page can, so the fold gets all of it + (tetrak-easyocr-armenian 0.9, brief 014).""" + armenian._spans([region("poor", 0, 0, 10, 10), region("roads", 0, 20, 10, 30)]) + assert stub_tetrak_hy.contexts == ["poor\nroads", "poor\nroads"] + def test_a_polygon_becomes_its_axis_aligned_extent(self, stub_tetrak_hy) -> None: spans = armenian._spans([region("ա", 5, 7, 25, 27)]) assert spans[0].bbox == (5.0, 7.0, 25.0, 27.0) diff --git a/uv.lock b/uv.lock index 38f863b..8f414b4 100644 --- a/uv.lock +++ b/uv.lock @@ -1,5 +1,5 @@ version = 1 -revision = 5 +revision = 3 requires-python = ">=3.11" resolution-markers = [ "python_full_version >= '3.14' and sys_platform == 'darwin'", @@ -4663,7 +4663,7 @@ requires-dist = [ { name = "sillybos", specifier = ">=5,<6" }, { name = "sphinx", marker = "extra == 'apidocs'", specifier = ">=8.0" }, { name = "sphinx-argparse", marker = "extra == 'apidocs'", specifier = ">=0.6.1" }, - { name = "tetrak-easyocr-armenian", marker = "extra == 'armenian'", specifier = ">=0.8" }, + { name = "tetrak-easyocr-armenian", marker = "extra == 'armenian'", specifier = ">=0.11" }, { name = "torch", marker = "extra == 'all'", specifier = ">=2.0" }, { name = "torch", marker = "extra == 'ocr'", specifier = ">=2.0" }, { name = "torch", marker = "extra == 'qa'", specifier = ">=2.0" }, @@ -4675,15 +4675,15 @@ provides-extras = ["all", "apidocs", "armenian", "claude", "dev", "docs", "easyo [[package]] name = "tetrak-easyocr-armenian" -version = "0.8.0" +version = "0.11.0" source = { registry = "https://pypi.org/simple" } dependencies = [ { name = "easyocr" }, { name = "requests" }, ] -sdist = { url = "https://files.pythonhosted.org/packages/17/88/07d1276c335307ca2272886548daabb2b320ec1432a5d57c246d8568b84d/tetrak_easyocr_armenian-0.8.0.tar.gz", hash = "sha256:6fb21989743a528e26878dafb142dc2f523589613a36cae80b7ef0e8d639baac", size = 176971, upload-time = "2026-10-03T10:56:24.693Z" } +sdist = { url = "https://files.pythonhosted.org/packages/58/ef/59a88e409d2d78a879a81ddd0061d56885fb9f5200431536235340cd0bf9/tetrak_easyocr_armenian-0.11.0.tar.gz", hash = "sha256:41d0c57b54d99409507d71102923895c1e73c92039434a65615ec086d96ce1ca", size = 160487, upload-time = "2026-11-15T22:36:34.237Z" } wheels = [ - { url = "https://files.pythonhosted.org/packages/6c/87/e013f805aa925fdebeb4661923c224a9b7b89a01b5429b28467f9035f6a7/tetrak_easyocr_armenian-0.8.0-py3-none-any.whl", hash = "sha256:59dfd20ede9bb2dc7bf2c77934dd985cb478540c82d32786a9b8d0c2265c2d8d", size = 24065, upload-time = "2026-10-03T10:56:23.341Z" }, + { url = "https://files.pythonhosted.org/packages/19/f6/c06d8e6e9286ff8371c7d4ff1115ef7be0dad7ff2cd14fbbbb737d1799da/tetrak_easyocr_armenian-0.11.0-py3-none-any.whl", hash = "sha256:c068dd9f03c7d090c1c56bd89e12c24ef542a5424ea87fad671607f5ad7021c7", size = 28951, upload-time = "2026-11-15T22:36:32.891Z" }, ] [[package]]