Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 13 additions & 3 deletions src/tetrak_ocr/backends/armenian.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 10 additions & 1 deletion tests/ocr/test_armenian.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading