File size: 11,903 Bytes
a77c861
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76e9545
a77c861
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
979f3c3
a77c861
 
 
 
 
 
 
 
 
 
 
 
979f3c3
a77c861
 
 
 
 
 
 
 
979f3c3
a77c861
 
 
 
 
 
 
 
979f3c3
a77c861
 
 
 
 
 
 
 
 
 
979f3c3
a77c861
 
 
 
 
 
 
 
 
 
 
979f3c3
 
a77c861
 
 
 
 
 
 
 
 
 
 
979f3c3
a77c861
 
 
979f3c3
a77c861
 
 
979f3c3
a77c861
979f3c3
a77c861
 
 
979f3c3
a77c861
979f3c3
a77c861
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
979f3c3
a77c861
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
979f3c3
76e9545
a77c861
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Tests Sprint 51 β€” adaptation Azure Document Intelligence pour exposer
token_confidences.

Couvre :

1. ``_extract_token_confidences_from_result`` parcourt
   ``pages[].words[]`` et Γ©met ``{"token": content, "confidence": float}``
   par mot.
2. Filtrage des mots sans confidence, conf nΓ©gative, contenu vide.
3. ``expose_confidences=False`` dΓ©sactive l'extraction.
4. ``analyze_result = None`` ou structures invalides β†’ retourne ``None``.
5. ``_sdk_result_to_dict`` convertit un objet SDK proto en dict
   normalisΓ© compatible avec le chemin REST.
6. ``run()`` orchestre les deux chemins (SDK + REST) et expose les
   confidences sur l'``EngineResult``.
7. Γ‰chec API β†’ ``error`` renseignΓ©, ``token_confidences = None``.
8. IntΓ©gration runner : ``calibration_metrics`` calculΓ©e bout-en-bout.
"""

from __future__ import annotations

from pathlib import Path
from unittest.mock import MagicMock

import pytest

from picarones.adapters.legacy_engines.azure_doc_intel import AzureDocIntelEngine


# ──────────────────────────────────────────────────────────────────────────
# Helpers
# ──────────────────────────────────────────────────────────────────────────


def _word(content: str, conf: float | None) -> dict:
    return {"content": content, "confidence": conf}


def _result(words: list[dict]) -> dict:
    return {"pages": [{"words": words}]}


# ──────────────────────────────────────────────────────────────────────────
# 1-2. Extraction depuis analyze_result
# ──────────────────────────────────────────────────────────────────────────


class TestExtractFromResult:
    def test_emits_one_entry_per_word(self) -> None:
        engine = AzureDocIntelEngine()
        result = _result([
            _word("Bonjour", 0.97),
            _word("monde", 0.93),
        ])
        out = engine._normalize_token_confidences(engine._extract_raw_confidences(result))
        assert out == [
            {"token": "Bonjour", "confidence": 0.97},
            {"token": "monde",   "confidence": 0.93},
        ]

    def test_skips_word_without_confidence(self) -> None:
        engine = AzureDocIntelEngine()
        result = _result([
            _word("ok", 0.95),
            {"content": "no_conf"},      # pas de confidence
            _word("none_conf", None),
        ])
        out = engine._normalize_token_confidences(engine._extract_raw_confidences(result))
        assert out == [{"token": "ok", "confidence": 0.95}]

    def test_skips_negative_confidence(self) -> None:
        engine = AzureDocIntelEngine()
        result = _result([
            _word("ok", 0.9),
            _word("dropped", -0.1),
        ])
        out = engine._normalize_token_confidences(engine._extract_raw_confidences(result))
        assert out == [{"token": "ok", "confidence": 0.9}]

    def test_skips_empty_content(self) -> None:
        engine = AzureDocIntelEngine()
        result = _result([
            _word("", 0.95),
            _word("ok", 0.9),
        ])
        out = engine._normalize_token_confidences(engine._extract_raw_confidences(result))
        assert out == [{"token": "ok", "confidence": 0.9}]

    def test_traverses_multiple_pages(self) -> None:
        engine = AzureDocIntelEngine()
        result = {
            "pages": [
                {"words": [_word("alpha", 0.9), _word("beta", 0.85)]},
                {"words": [_word("gamma", 0.8)]},
            ],
        }
        out = engine._normalize_token_confidences(engine._extract_raw_confidences(result))
        assert [tc["token"] for tc in (out or [])] == ["alpha", "beta", "gamma"]


# ──────────────────────────────────────────────────────────────────────────
# 3. expose_confidences=False
# ──────────────────────────────────────────────────────────────────────────


class TestExposeFlag:
    def test_disabled_returns_none(self) -> None:
        engine = AzureDocIntelEngine(config={"expose_confidences": False})
        assert engine._normalize_token_confidences(
            engine._extract_raw_confidences(_result([_word("ok", 0.9)])),
        ) is None


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


class TestDegenerateInputs:
    def test_none(self) -> None:
        engine = AzureDocIntelEngine()
        assert engine._normalize_token_confidences(engine._extract_raw_confidences(None)) is None

    def test_empty_dict(self) -> None:
        engine = AzureDocIntelEngine()
        assert engine._normalize_token_confidences(engine._extract_raw_confidences({})) is None

    def test_no_pages(self) -> None:
        engine = AzureDocIntelEngine()
        assert engine._normalize_token_confidences(engine._extract_raw_confidences(
            {"pages": []},
        )) is None

    def test_pages_without_words(self) -> None:
        engine = AzureDocIntelEngine()
        assert engine._normalize_token_confidences(engine._extract_raw_confidences(
            {"pages": [{"lines": [{"content": "no words"}]}]},
        )) is None


# ──────────────────────────────────────────────────────────────────────────
# 5. Conversion SDK β†’ dict
# ──────────────────────────────────────────────────────────────────────────


class TestSdkConversion:
    def test_sdk_to_dict(self) -> None:
        # Mock du proto SDK
        word_mock = MagicMock()
        word_mock.content = "Bonjour"
        word_mock.confidence = 0.97
        page_mock = MagicMock()
        page_mock.words = [word_mock]
        result_mock = MagicMock()
        result_mock.pages = [page_mock]

        out = AzureDocIntelEngine._sdk_result_to_dict(result_mock)
        assert "pages" in out
        assert out["pages"][0]["words"][0]["content"] == "Bonjour"
        assert out["pages"][0]["words"][0]["confidence"] == pytest.approx(0.97)

    def test_sdk_word_with_none_confidence(self) -> None:
        word_mock = MagicMock()
        word_mock.content = "ok"
        word_mock.confidence = None
        page_mock = MagicMock()
        page_mock.words = [word_mock]
        result_mock = MagicMock()
        result_mock.pages = [page_mock]

        out = AzureDocIntelEngine._sdk_result_to_dict(result_mock)
        assert out["pages"][0]["words"][0]["confidence"] is None


# ──────────────────────────────────────────────────────────────────────────
# 6-7. run() avec mock
# ──────────────────────────────────────────────────────────────────────────


def _patch_run_with_result(
    monkeypatch: pytest.MonkeyPatch,
    text: str,
    analyze_result: dict | None,
    *,
    raise_exc: Exception | None = None,
) -> AzureDocIntelEngine:
    engine = AzureDocIntelEngine()
    engine._api_key = "test-key"
    engine._endpoint = "https://test.cognitiveservices.azure.com"

    def _fake(self, image_path):
        if raise_exc is not None:
            raise raise_exc
        return text, analyze_result

    monkeypatch.setattr(
        AzureDocIntelEngine, "_run_with_native", _fake,
    )
    return engine


class TestRunOverride:
    def test_run_exposes_confidences(
        self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path,
    ) -> None:
        engine = _patch_run_with_result(
            monkeypatch,
            text="Bonjour\nmonde",
            analyze_result=_result([
                _word("Bonjour", 0.97),
                _word("monde", 0.93),
            ]),
        )
        img = tmp_path / "p.png"
        img.write_bytes(b"x")
        result = engine.run(img)
        assert result.text == "Bonjour\nmonde"
        assert result.error is None
        assert result.token_confidences is not None
        assert len(result.token_confidences) == 2

    def test_run_no_result_no_confidences(
        self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path,
    ) -> None:
        engine = _patch_run_with_result(
            monkeypatch, text="Texte", analyze_result=None,
        )
        img = tmp_path / "p.png"
        img.write_bytes(b"x")
        result = engine.run(img)
        assert result.text == "Texte"
        assert result.token_confidences is None

    def test_run_api_failure_keeps_error(
        self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path,
    ) -> None:
        engine = _patch_run_with_result(
            monkeypatch, text="", analyze_result=None,
            raise_exc=RuntimeError("Azure timeout"),
        )
        img = tmp_path / "p.png"
        img.write_bytes(b"x")
        result = engine.run(img)
        assert result.error == "Azure timeout"
        assert result.token_confidences is None


# ──────────────────────────────────────────────────────────────────────────
# 8. IntΓ©gration runner
# ──────────────────────────────────────────────────────────────────────────


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

        ocr = EngineResult(
            engine_name="azure_doc_intel",
            image_path="/tmp/x.png",
            text="alpha beta gamma",
            duration_seconds=0.1,
            token_confidences=[
                {"token": "alpha", "confidence": 0.97},
                {"token": "beta",  "confidence": 0.93},
                {"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.97 + 0.93 + 0.95) / 3,
        )