diff --git a/normalization/languages/spanish/number_normalizer.py b/normalization/languages/spanish/number_normalizer.py index 632a3fc..b8b4704 100644 --- a/normalization/languages/spanish/number_normalizer.py +++ b/normalization/languages/spanish/number_normalizer.py @@ -126,17 +126,25 @@ def _parse_number(self, words: list[str], i: int, n: int) -> tuple[int, int] | N end, v2 = tail return end, base + v2 return j, base - if h == 100: - sub = self._parse_0_99(words, j, n) - if sub is not None: - je, v = sub - return je, 100 + v - return j, 100 + # Hundred block optionally followed by a 0–99 remainder + # (e.g. "ciento cincuenta" → 150, "doscientos uno" → 201). + hundreds_value = h sub = self._parse_0_99(words, j, n) if sub is not None: je, v = sub - return je, h + v - return j, h + hundreds_value = h + v + j = je + # The whole hundred block may itself be a "mil" multiplier + # (e.g. "ciento cincuenta mil" → 150000). + if j < n and _fold(words[j]) == "mil": + base = hundreds_value * 1000 + j += 1 + tail = self._parse_number(words, j, n) + if tail is not None: + end, v2 = tail + return end, base + v2 + return j, base + return j, hundreds_value # --- 0–99 or leading multiplier for "mil" --- sub99 = self._parse_0_99(words, i, n) diff --git a/tests/unit/languages/spanish_number_normalizer_test.py b/tests/unit/languages/spanish_number_normalizer_test.py new file mode 100644 index 0000000..192679e --- /dev/null +++ b/tests/unit/languages/spanish_number_normalizer_test.py @@ -0,0 +1,30 @@ +import pytest + +from normalization.languages.spanish.number_normalizer import ( + SpanishNumberNormalizer, +) + + +@pytest.fixture +def normalizer(): + return SpanishNumberNormalizer() + + +@pytest.mark.parametrize( + ("text", "expected"), + [ + ("veintitres", "23"), + ("cien", "100"), + ("doscientos", "200"), + ("doscientos cincuenta", "250"), + # A hundreds-block followed by a sub-part before "mil" must keep the + # thousands multiplier (previously dropped: "doscientos cincuenta mil" + # returned "250 mil" instead of "250000"). + ("doscientos cincuenta mil", "250000"), + ("ciento cincuenta mil", "150000"), + ("novecientos noventa y nueve mil", "999000"), + ("doscientos cincuenta mil quinientos", "250500"), + ], +) +def test_spanish_number_normalizer(normalizer, text: str, expected: str): + assert normalizer(text) == expected