File size: 12,669 Bytes
49ee0be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76e9545
 
49ee0be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
979f3c3
49ee0be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
979f3c3
49ee0be
 
 
 
 
 
 
 
 
 
 
 
 
 
979f3c3
49ee0be
 
 
 
 
 
 
 
 
 
979f3c3
49ee0be
 
 
 
 
 
 
 
 
 
979f3c3
49ee0be
 
 
 
 
 
 
 
 
 
 
 
 
 
979f3c3
49ee0be
 
 
 
 
 
 
 
 
 
979f3c3
49ee0be
 
 
 
979f3c3
49ee0be
 
 
 
 
 
 
 
 
979f3c3
49ee0be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
979f3c3
76e9545
49ee0be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
"""Tests Sprint 48 β€” adaptation Pero OCR pour exposer token_confidences.

Couvre :

1. ``_extract_token_confidences_from_layout`` parcourt regions/lines
   et Γ©met un dict ``{"token": str, "confidence": float}`` par mot,
   en utilisant ``line.transcription_confidence`` propagΓ©e Γ  tous les
   mots de la ligne.
2. Les lignes sans ``transcription`` ou sans
   ``transcription_confidence`` sont sautΓ©es.
3. Une transcription multi-mots produit autant d'entrΓ©es que de mots.
4. ``expose_confidences=False`` dΓ©sactive l'extraction.
5. ``page_layout = None`` ou vide β†’ retourne ``None`` sans crash.
6. ``run()`` appelle ``_run_pero_pipeline`` **une seule fois** (pas
   de double coΓ»t comme Tesseract) et expose ``token_confidences``
   sur l'``EngineResult``.
7. Si le pipeline lève, ``error`` est renseigné, ``text=""``, et
   ``token_confidences = None``.
8. IntΓ©gration bout-en-bout avec ``_compute_document_result``.
"""

from __future__ import annotations

from pathlib import Path
from unittest.mock import MagicMock

import pytest

import picarones.adapters.legacy_engines.pero_ocr as pero_module
from picarones.adapters.legacy_engines.pero_ocr import PeroOCREngine


# ──────────────────────────────────────────────────────────────────────────
# Helpers : mock d'un page_layout Pero OCR
# ──────────────────────────────────────────────────────────────────────────


def _mock_line(transcription: str, conf: float | None) -> MagicMock:
    line = MagicMock()
    line.transcription = transcription
    line.transcription_confidence = conf
    return line


def _mock_region(lines: list) -> MagicMock:
    region = MagicMock()
    region.lines = lines
    return region


def _mock_layout(regions: list) -> MagicMock:
    layout = MagicMock()
    layout.regions = regions
    return layout


# ──────────────────────────────────────────────────────────────────────────
# 1-3. Extraction depuis page_layout
# ──────────────────────────────────────────────────────────────────────────


class TestExtractFromLayout:
    def test_one_word_per_token(self) -> None:
        engine = PeroOCREngine()
        layout = _mock_layout([
            _mock_region([
                _mock_line("Bonjour le monde", 0.92),
            ]),
        ])
        out = engine._normalize_token_confidences(engine._extract_raw_confidences(layout))
        assert out is not None
        assert out == [
            {"token": "Bonjour", "confidence": 0.92},
            {"token": "le", "confidence": 0.92},
            {"token": "monde", "confidence": 0.92},
        ]

    def test_multiple_lines_concatenated(self) -> None:
        engine = PeroOCREngine()
        layout = _mock_layout([
            _mock_region([
                _mock_line("Première ligne", 0.95),
                _mock_line("Deuxième ligne", 0.80),
            ]),
        ])
        out = engine._normalize_token_confidences(engine._extract_raw_confidences(layout))
        assert out is not None
        # Chaque mot porte la confidence de SA ligne
        assert {"token": "Première", "confidence": 0.95} in out
        assert {"token": "Deuxième", "confidence": 0.80} in out

    def test_skips_empty_transcription(self) -> None:
        engine = PeroOCREngine()
        layout = _mock_layout([
            _mock_region([
                _mock_line("", 0.95),       # transcription vide
                _mock_line(None, 0.95),     # transcription None
                _mock_line("ok", 0.95),     # ok
            ]),
        ])
        out = engine._normalize_token_confidences(engine._extract_raw_confidences(layout))
        assert out == [{"token": "ok", "confidence": 0.95}]

    def test_skips_none_confidence(self) -> None:
        engine = PeroOCREngine()
        layout = _mock_layout([
            _mock_region([
                _mock_line("avec_conf", 0.85),
                _mock_line("sans_conf", None),
            ]),
        ])
        out = engine._normalize_token_confidences(engine._extract_raw_confidences(layout))
        assert out == [{"token": "avec_conf", "confidence": 0.85}]

    def test_skips_negative_confidence(self) -> None:
        engine = PeroOCREngine()
        layout = _mock_layout([
            _mock_region([
                _mock_line("ok", 0.9),
                _mock_line("dropped", -0.1),
            ]),
        ])
        out = engine._normalize_token_confidences(engine._extract_raw_confidences(layout))
        assert out == [{"token": "ok", "confidence": 0.9}]


# ──────────────────────────────────────────────────────────────────────────
# 4. expose_confidences=False
# ──────────────────────────────────────────────────────────────────────────


class TestExposeFlag:
    def test_disabled_returns_none(self) -> None:
        engine = PeroOCREngine(config={"expose_confidences": False})
        layout = _mock_layout([
            _mock_region([_mock_line("hello", 0.9)]),
        ])
        assert engine._normalize_token_confidences(engine._extract_raw_confidences(layout)) is None


# ──────────────────────────────────────────────────────────────────────────
# 5. Cas dΓ©gΓ©nΓ©rΓ©s
# ──────────────────────────────────────────────────────────────────────────


class TestDegenerateLayouts:
    def test_none_layout(self) -> None:
        engine = PeroOCREngine()
        assert engine._normalize_token_confidences(engine._extract_raw_confidences(None)) is None

    def test_empty_regions(self) -> None:
        engine = PeroOCREngine()
        layout = _mock_layout([])
        assert engine._normalize_token_confidences(engine._extract_raw_confidences(layout)) is None

    def test_only_lines_without_conf_returns_none(self) -> None:
        engine = PeroOCREngine()
        layout = _mock_layout([
            _mock_region([
                _mock_line("ok", None),
                _mock_line("ok2", None),
            ]),
        ])
        assert engine._normalize_token_confidences(engine._extract_raw_confidences(layout)) is None


# ──────────────────────────────────────────────────────────────────────────
# 6-7. run() avec mock du pipeline complet
# ──────────────────────────────────────────────────────────────────────────


def _make_engine_with_mock_pipeline(
    monkeypatch: pytest.MonkeyPatch,
    *,
    text: str = "Bonjour le monde",
    layout_regions: list | None = None,
    raise_on_pipeline: bool = False,
) -> PeroOCREngine:
    """Mocke ``_run_pero_pipeline`` pour ne pas dΓ©pendre de pero-ocr."""
    engine = PeroOCREngine()

    if layout_regions is None:
        layout_regions = [
            _mock_region([_mock_line(text, 0.92)]),
        ]
    layout = _mock_layout(layout_regions)

    def _fake_pipeline(self, image_path):
        if raise_on_pipeline:
            raise RuntimeError("simulated pipeline failure")
        return text, layout

    monkeypatch.setattr(
        PeroOCREngine, "_run_pero_pipeline", _fake_pipeline,
    )
    return engine


class TestRunPipeline:
    def test_run_exposes_confidences(
        self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path,
    ) -> None:
        engine = _make_engine_with_mock_pipeline(monkeypatch)
        img = tmp_path / "p.png"
        img.write_bytes(b"x")

        result = engine.run(img)
        assert result.text == "Bonjour le monde"
        assert result.error is None
        assert result.token_confidences is not None
        assert len(result.token_confidences) == 3

    def test_run_text_preserved_octet_for_octet(
        self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path,
    ) -> None:
        engine = _make_engine_with_mock_pipeline(
            monkeypatch, text="Texte avec\nplusieurs lignes",
        )
        img = tmp_path / "p.png"
        img.write_bytes(b"x")
        result = engine.run(img)
        assert result.text == "Texte avec\nplusieurs lignes"

    def test_pipeline_failure_keeps_error(
        self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path,
    ) -> None:
        engine = _make_engine_with_mock_pipeline(
            monkeypatch, raise_on_pipeline=True,
        )
        img = tmp_path / "p.png"
        img.write_bytes(b"x")
        result = engine.run(img)
        assert result.error == "simulated pipeline failure"
        assert result.text == ""
        assert result.token_confidences is None


# ──────────────────────────────────────────────────────────────────────────
# 8. IntΓ©gration bout-en-bout avec le runner
# ──────────────────────────────────────────────────────────────────────────


class TestEndToEndWithRunner:
    def test_runner_picks_up_confidences(self) -> None:
        from picarones.measurements.runner import _compute_document_result
        from picarones.adapters.legacy_engines.base import EngineResult

        ocr = EngineResult(
            engine_name="pero",
            image_path="/tmp/x.png",
            text="alpha beta gamma",
            duration_seconds=0.1,
            # Confidence ∈ [0, 1] cΓ΄tΓ© Pero (vs [0, 100] Tesseract) β€”
            # le runner Sprint 42 normalise via le helper bag-of-words.
            token_confidences=[
                {"token": "alpha", "confidence": 0.95},
                {"token": "beta",  "confidence": 0.95},
                {"token": "gamma", "confidence": 0.95},
            ],
        )
        dr = _compute_document_result(
            doc_id="d1", image_path="/tmp/x.png",
            ground_truth="alpha beta gamma",
            ocr_result=ocr, char_exclude=None,
        )
        assert dr.calibration_metrics is not None
        assert dr.calibration_metrics["overall_accuracy"] == 1.0
        assert dr.calibration_metrics["overall_confidence"] == pytest.approx(0.95)


# ──────────────────────────────────────────────────────────────────────────
# 9. Pero absent β€” fallback gracieux cΓ΄tΓ© pipeline rΓ©el
# ──────────────────────────────────────────────────────────────────────────


class TestPeroAbsent:
    def test_pipeline_missing_pero_raises(
        self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path,
    ) -> None:
        """Si pero-ocr n'est pas installé, ``_run_pero_pipeline`` lève
        Γ  travers ``_get_parser()``.  ``run()`` capture l'exception et
        retourne ``EngineResult.error``."""
        monkeypatch.setattr(pero_module, "_PERO_AVAILABLE", False)
        engine = PeroOCREngine(config={"config": "/no/such/file.ini"})
        img = tmp_path / "p.png"
        img.write_bytes(b"x")
        result = engine.run(img)
        assert result.error is not None
        assert "pero" in result.error.lower() or "Pillow" in result.error
        assert result.token_confidences is None