Skip to content
Open
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
24 changes: 16 additions & 8 deletions normalization/languages/spanish/number_normalizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 30 additions & 0 deletions tests/unit/languages/spanish_number_normalizer_test.py
Original file line number Diff line number Diff line change
@@ -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