Skip to content

Commit

Permalink
Refined handling of zero. Fixes #105, fixes #106.
Browse files Browse the repository at this point in the history
  • Loading branch information
rtxm committed Mar 19, 2024
1 parent 7fea5d1 commit 510f223
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 12 deletions.
1 change: 1 addition & 0 deletions tests/test_text_to_num_en.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ def test_alpha2digit_zero(self):
self.assertEqual(alpha2digit(source, "en"), expected)

self.assertEqual(alpha2digit("zero", "en"), "0")
self.assertEqual(alpha2digit("zero love", "en"), "0 love")

def test_alpha2digit_ordinals(self):
source = (
Expand Down
2 changes: 2 additions & 0 deletions tests/test_text_to_num_fr.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ def test_alpha2digit_zero(self):
# self.assertEqual(alpha2digit(source, "fr"), source)

self.assertEqual(alpha2digit("zéro", "fr"), "0")
self.assertEqual(alpha2digit("a a un trois sept trois trois sept cinq quatre zéro c c", "fr"), "a a 1 3 7 3 3 7 5 4 0 c c")
self.assertEqual(alpha2digit("sept un zéro", "fr"), "7 1 0")

def test_alpha2digit_ordinals(self):
source = (
Expand Down
2 changes: 1 addition & 1 deletion text_to_num/lang/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def normalize(self, word: str) -> str:
return NotImplemented

def not_numeric_word(self, word: Optional[str]) -> bool:
return word is None or word != self.DECIMAL_SEP and word not in self.NUMBERS
return word is None or word != self.DECIMAL_SEP and word not in self.NUMBERS and word not in self.ZERO

def split_number_word(self, word: str) -> str: # maybe use: List[str]
"""In some languages numbers are written as one word, e.g. German
Expand Down
2 changes: 1 addition & 1 deletion text_to_num/lang/english.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class English(Language):

AND_NUMS: Set[str] = set()
AND = "and"
NEVER_IF_ALONE = {"one"}
NEVER_IF_ALONE = {"one", "o"}

# Relaxed composed numbers (two-words only)
# start => (next, target)
Expand Down
20 changes: 10 additions & 10 deletions text_to_num/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,21 +652,21 @@ def push(self, word: str, look_ahead: Optional[str] = None) -> bool:
elif (
word in self.lang.ZERO
and self.at_start_of_seq()
and (
look_ahead is None
or look_ahead in self.lang.NUMBERS
or look_ahead in self.lang.ZERO
or look_ahead in self.lang.DECIMAL_SEP
)
and look_ahead is not None
and look_ahead in self.lang.DECIMAL_SEP
):
self._value.append("0")
pass
elif (
word in self.lang.ZERO
and self.at_start_of_seq()
and look_ahead is not None
and look_ahead in self.lang.DECIMAL_SEP
# and (
# look_ahead is None
# or look_ahead in self.lang.NUMBERS
# or look_ahead in self.lang.ZERO
# or look_ahead in self.lang.DECIMAL_SEP
# )
):
pass
self._value.append("0")
elif self._push(self.lang.ord2card(word) or "", look_ahead):
self._value.append(
self.lang.num_ord(
Expand Down

0 comments on commit 510f223

Please sign in to comment.