Spaces:
Sleeping
Sleeping
File size: 16,810 Bytes
fe4b9ac 979f3c3 fe4b9ac 979f3c3 fe4b9ac | 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 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 | """Tests Sprint 60 β numΓ©raux romains transversaux.
Couvre :
1. ``roman_to_int`` / ``int_to_roman`` : conversion bidirectionnelle,
tolΓ©rance casse + ``j`` mΓ©diΓ©val, validation stricte des formes
absurdes.
2. ``detect_roman_numerals`` : dΓ©tection par regex, ``min_length``,
tolérance casse, frontière de mot.
3. ``compute_roman_numeral_metrics`` : 5 statuts (strict_preserved,
case_changed, j_dropped, converted_to_arabic, lost) discriminΓ©s
sur scΓ©narios standards.
4. Cas rΓ©alistes : manuscrit mΓ©diΓ©val (mcclxxxij), imprimΓ© ancien
(Tome IV, chap. VII), moderne (Louis XIV, MCMXIV).
5. Comptage exhaustif : la somme des per_status Γ©gale n_total.
6. Cas dΓ©gΓ©nΓ©rΓ©s.
7. IntΓ©gration registre typΓ©.
"""
from __future__ import annotations
import pytest
from picarones.core.metric_registry import compute_at_junction, select_metrics
from picarones.core.modules import ArtifactType
from picarones.measurements.roman_numerals import (
ALL_STATUSES,
STATUS_CASE_CHANGED,
STATUS_CONVERTED_TO_ARABIC,
STATUS_J_DROPPED,
STATUS_LOST,
STATUS_STRICT_PRESERVED,
compute_roman_numeral_metrics,
detect_roman_numerals,
int_to_roman,
roman_numeral_strict_score,
roman_numeral_value_score,
roman_to_int,
)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 1. roman_to_int / int_to_roman
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class TestRomanToInt:
@pytest.mark.parametrize(
"s,expected",
[
("I", 1), ("II", 2), ("III", 3),
("IV", 4), ("V", 5), ("IX", 9),
("X", 10), ("XIV", 14), ("XL", 40), ("XLIV", 44),
("L", 50), ("XC", 90), ("XCIX", 99),
("C", 100), ("CD", 400), ("D", 500), ("CM", 900),
("M", 1000), ("MCMXIV", 1914), ("MDCCLXXXIX", 1789),
],
)
def test_standard_uppercase(self, s: str, expected: int) -> None:
assert roman_to_int(s) == expected
@pytest.mark.parametrize(
"s,expected",
[
("i", 1), ("iv", 4), ("xiv", 14),
("mcmxiv", 1914), ("mdcclxxxix", 1789),
],
)
def test_lowercase_accepted(self, s: str, expected: int) -> None:
assert roman_to_int(s) == expected
@pytest.mark.parametrize(
"s,expected",
[
("ij", 2), ("iij", 3), ("iiij", 4),
("vij", 7), ("viij", 8),
("xij", 12), ("xiij", 13),
("mcclxxxij", 1282),
],
)
def test_medieval_j_final(self, s: str, expected: int) -> None:
assert roman_to_int(s) == expected
@pytest.mark.parametrize(
"s",
[
"", "ABC", "XYZ", "VV", "LL", "DD",
"IIIII", "XXXXX", "VVVV",
],
)
def test_invalid_returns_none(self, s: str) -> None:
assert roman_to_int(s) is None
def test_none_returns_none(self) -> None:
assert roman_to_int(None) is None
def test_medieval_relaxed_iiii(self) -> None:
# IIII (forme mΓ©diΓ©vale) acceptΓ©
assert roman_to_int("IIII") == 4
assert roman_to_int("XXXX") == 40
class TestIntToRoman:
@pytest.mark.parametrize(
"n,expected",
[
(1, "I"), (4, "IV"), (9, "IX"), (14, "XIV"),
(40, "XL"), (90, "XC"), (99, "XCIX"),
(400, "CD"), (900, "CM"),
(1789, "MDCCLXXXIX"), (1914, "MCMXIV"), (2024, "MMXXIV"),
],
)
def test_standard(self, n: int, expected: str) -> None:
assert int_to_roman(n) == expected
def test_round_trip(self) -> None:
for n in (1, 7, 49, 99, 444, 1066, 1789, 1914, 2024):
assert roman_to_int(int_to_roman(n)) == n
def test_invalid_input(self) -> None:
with pytest.raises(ValueError):
int_to_roman(0)
with pytest.raises(ValueError):
int_to_roman(-5)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 2. detect_roman_numerals
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class TestDetectRomanNumerals:
def test_detects_uppercase(self) -> None:
text = "Louis XIV mourut en MDCCXV"
out = detect_roman_numerals(text)
nums = [(n, v) for _i, n, v in out]
assert ("XIV", 14) in nums
assert ("MDCCXV", 1715) in nums
def test_detects_medieval_lowercase_with_j(self) -> None:
text = "fait en lan mcclxxxij chap iij"
out = detect_roman_numerals(text)
nums = [(n, v) for _i, n, v in out]
assert ("mcclxxxij", 1282) in nums
assert ("iij", 3) in nums
def test_returns_indices(self) -> None:
text = "abc XIV def"
out = detect_roman_numerals(text)
assert out == [(4, "XIV", 14)]
def test_min_length_filters_single_letter(self) -> None:
# Β« I Β» seul ambigu (pronom anglais)
text = "I am here in chapter II"
out_default = detect_roman_numerals(text)
nums_default = [n for _i, n, _v in out_default]
assert "I" in nums_default
assert "II" in nums_default
out_filtered = detect_roman_numerals(text, min_length=2)
nums_filtered = [n for _i, n, _v in out_filtered]
assert "I" not in nums_filtered
assert "II" in nums_filtered
def test_word_boundary_blocks_embedded(self) -> None:
# « VIVE » contient « V " et « IV " mais pas en frontière
# de mot β pas de match.
text = "VIVE le roi"
out = detect_roman_numerals(text)
nums = [n for _i, n, _v in out]
assert "V" not in nums
assert "IV" not in nums
def test_invalid_chars_skipped(self) -> None:
# Β« VV " est ramassΓ© par la regex puis rejetΓ© par la
# validation β pas dans le rΓ©sultat.
text = "essai VV ici"
out = detect_roman_numerals(text)
assert out == []
def test_empty_input(self) -> None:
assert detect_roman_numerals("") == []
assert detect_roman_numerals(None) == []
def test_text_without_numerals(self) -> None:
assert detect_roman_numerals("hello world without any") == []
def test_preserves_order(self) -> None:
text = "chap II puis chap V puis chap XIV"
out = detect_roman_numerals(text)
nums = [n for _i, n, _v in out]
assert nums == ["II", "V", "XIV"]
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 3. compute_roman_numeral_metrics β 5 statuts
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class TestClassificationStatuses:
def test_strict_preserved(self) -> None:
m = compute_roman_numeral_metrics("Louis XIV", "Louis XIV")
assert m["per_status"][STATUS_STRICT_PRESERVED] == 1
assert m["global_strict_score"] == pytest.approx(1.0)
def test_case_changed(self) -> None:
# GT en minuscule, hyp en majuscule (modernisation typo)
m = compute_roman_numeral_metrics("Louis xiv", "Louis XIV")
assert m["per_status"][STATUS_CASE_CHANGED] == 1
assert m["per_status"][STATUS_STRICT_PRESERVED] == 0
assert m["global_strict_score"] == 0.0
assert m["global_value_score"] == pytest.approx(1.0)
def test_j_dropped(self) -> None:
# GT mΓ©diΓ©vale avec j final, hyp normalise en i
m = compute_roman_numeral_metrics("chap iij", "chap iii")
assert m["per_status"][STATUS_J_DROPPED] == 1
assert m["global_value_score"] == pytest.approx(1.0)
def test_converted_to_arabic(self) -> None:
m = compute_roman_numeral_metrics("Louis XIV", "Louis 14")
assert m["per_status"][STATUS_CONVERTED_TO_ARABIC] == 1
assert m["global_value_score"] == pytest.approx(1.0)
def test_lost(self) -> None:
m = compute_roman_numeral_metrics("Louis XIV", "Louis le quatorzième")
assert m["per_status"][STATUS_LOST] == 1
assert m["global_value_score"] == 0.0
def test_priority_strict_over_arabic(self) -> None:
# Si l'hyp contient Γ la fois la forme stricte ET le chiffre
# arabe, on classe strict (prioritΓ©).
m = compute_roman_numeral_metrics(
"Louis XIV", "Louis XIV (14 du nom)",
)
assert m["per_status"][STATUS_STRICT_PRESERVED] == 1
assert m["per_status"][STATUS_CONVERTED_TO_ARABIC] == 0
class TestPerStatusBreakdown:
def test_breakdown_sums_to_total(self) -> None:
gt = "lan mcclxxxij chap iij Louis XIV en MCMXIV"
hyp = "lan MCCLXXXIJ chap iii Louis 14 en MCMXIV"
m = compute_roman_numeral_metrics(gt, hyp)
assert sum(m["per_status"].values()) == m["n_numerals_reference"]
def test_per_status_initializes_all(self) -> None:
# MΓͺme sans aucun numΓ©ral, per_status doit lister tous les
# statuts Γ 0.
m = compute_roman_numeral_metrics("hello", "hello")
assert set(m["per_status"].keys()) == set(ALL_STATUSES)
def test_per_numeral_entries(self) -> None:
m = compute_roman_numeral_metrics("XIV puis V", "XIV puis 5")
per_num = m["per_numeral"]
assert len(per_num) == 2
for entry in per_num:
assert "index" in entry
assert "numeral" in entry
assert "value" in entry
assert "status" in entry
def test_lost_numerals_listed(self) -> None:
m = compute_roman_numeral_metrics("XIV V XX", "neant")
lost = m["lost_numerals"]
assert len(lost) == 3
values = sorted(e["value"] for e in lost)
assert values == [5, 14, 20]
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 4. Cas rΓ©alistes par pΓ©riode patrimoniale
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class TestRealisticMedievalCharter:
"""Charte mΓ©diΓ©vale en minuscule avec ``j`` final."""
@pytest.fixture
def gt(self) -> str:
return "fait en lan de grace mcclxxxij le iij iour de mai"
def test_diplomatic(self, gt: str) -> None:
m = compute_roman_numeral_metrics(gt, gt)
assert m["global_strict_score"] == pytest.approx(1.0)
def test_modernized_to_uppercase_no_j(self, gt: str) -> None:
# Modernisation typo + orthographique : majuscule + jβi
hyp = "fait en lan de grace MCCLXXXII le III iour de mai"
m = compute_roman_numeral_metrics(gt, hyp)
assert m["global_strict_score"] == 0.0
assert m["global_value_score"] == pytest.approx(1.0)
# Au moins un j_dropped (sur iij β III)
assert m["per_status"][STATUS_J_DROPPED] >= 1
def test_arabic_conversion(self, gt: str) -> None:
hyp = "fait en lan de grace 1282 le 3 iour de mai"
m = compute_roman_numeral_metrics(gt, hyp)
assert m["per_status"][STATUS_CONVERTED_TO_ARABIC] == 2
assert m["global_value_score"] == pytest.approx(1.0)
class TestRealisticEarlyModernPrint:
"""Tome IV, chap. VII (imprimΓ© ancien)."""
def test_diplomatic(self) -> None:
gt = "Tome IV chap VII p XXIII"
m = compute_roman_numeral_metrics(gt, gt)
assert m["global_strict_score"] == pytest.approx(1.0)
def test_arabic_conversion(self) -> None:
gt = "Tome IV chap VII p XXIII"
hyp = "Tome 4 chap 7 p 23"
m = compute_roman_numeral_metrics(gt, hyp)
assert m["per_status"][STATUS_CONVERTED_TO_ARABIC] == 3
assert m["global_value_score"] == pytest.approx(1.0)
class TestRealisticModernSovereign:
"""Louis XIV, MCMXIV β moderne."""
def test_diplomatic(self) -> None:
gt = "Louis XIV mourut en MDCCXV ; la guerre en MCMXIV"
m = compute_roman_numeral_metrics(gt, gt)
assert m["global_strict_score"] == pytest.approx(1.0)
def test_partial_arabic(self) -> None:
# Le souverain reste en romain, la date passe en arabe
gt = "Louis XIV mourut en MDCCXV"
hyp = "Louis XIV mourut en 1715"
m = compute_roman_numeral_metrics(gt, hyp)
assert m["per_status"][STATUS_STRICT_PRESERVED] == 1
assert m["per_status"][STATUS_CONVERTED_TO_ARABIC] == 1
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 5. Cas dΓ©gΓ©nΓ©rΓ©s
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class TestDegenerateCases:
def test_gt_without_numerals(self) -> None:
m = compute_roman_numeral_metrics("hello world", "hello world")
assert m["n_numerals_reference"] == 0
assert m["global_strict_score"] == 0.0
assert m["global_value_score"] == 0.0
assert m["per_numeral"] == []
def test_empty_gt(self) -> None:
m = compute_roman_numeral_metrics("", "anything")
assert m["n_numerals_reference"] == 0
def test_none_inputs(self) -> None:
m = compute_roman_numeral_metrics(None, None)
assert m["n_numerals_reference"] == 0
def test_empty_hyp_with_numerals(self) -> None:
m = compute_roman_numeral_metrics("XIV V", "")
assert m["n_numerals_reference"] == 2
assert m["per_status"][STATUS_LOST] == 2
assert m["global_value_score"] == 0.0
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 6. Raccourcis + registre typΓ©
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class TestShortcuts:
def test_strict_shortcut_matches_full_call(self) -> None:
gt = "Louis XIV"
hyp = "Louis XIV"
full = compute_roman_numeral_metrics(gt, hyp)
assert roman_numeral_strict_score(gt, hyp) == pytest.approx(
full["global_strict_score"],
)
def test_value_shortcut_matches_full_call(self) -> None:
gt = "Louis XIV"
hyp = "Louis 14"
full = compute_roman_numeral_metrics(gt, hyp)
assert roman_numeral_value_score(gt, hyp) == pytest.approx(
full["global_value_score"],
)
class TestRegistryIntegration:
def test_metrics_registered(self) -> None:
import picarones.measurements.roman_numerals # noqa: F401
selected = select_metrics(
(ArtifactType.TEXT, ArtifactType.TEXT),
)
names = {spec.name for spec in selected}
assert "roman_numeral_strict_score" in names
assert "roman_numeral_value_score" in names
def test_compute_at_junction_strict(self) -> None:
out = compute_at_junction(
"Louis XIV", "Louis XIV",
(ArtifactType.TEXT, ArtifactType.TEXT),
)
assert out["roman_numeral_strict_score"] == pytest.approx(1.0)
def test_compute_at_junction_value(self) -> None:
out = compute_at_junction(
"Louis XIV", "Louis 14",
(ArtifactType.TEXT, ArtifactType.TEXT),
)
assert out["roman_numeral_strict_score"] == pytest.approx(0.0)
assert out["roman_numeral_value_score"] == pytest.approx(1.0)
|