Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions tests/test_text_to_num_en.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,10 @@ def test_text2num(self):
self.assertEqual(text2num("thousand nine hundred twenty", "en"), 1920)
self.assertEqual(text2num("one billion twenty-five millions", "en"), 1_025_000_000)

def test_text2num_centuries(self):
self.assertEqual(text2num("nineteen hundred seventy-three", "en"), 1973)

def test_text2num_exc(self):
self.assertRaises(ValueError, text2num, "thousand thousand two hundreds", "en")
self.assertRaises(ValueError, text2num, "sixty fifteen", "en")
self.assertRaises(ValueError, text2num, "sixty hundred", "en")
self.assertRaises(ValueError, text2num, "hundred hundreds", "en")

def test_text2num_zeroes(self):
self.assertEqual(text2num("zero", "en"), 0)
Expand All @@ -67,6 +64,13 @@ def test_text2num_zeroes(self):
self.assertRaises(ValueError, text2num, "fifty zero three", "en")
self.assertRaises(ValueError, text2num, "fifty three zero", "en")

def test_text2num_hundreds(self):
source = "forty five hundred thirty eight"
expected = 4538
self.assertEqual(text2num(source, "en"), expected)
self.assertEqual(text2num("nineteen hundred seventy-three", "en"), 1973)
self.assertEqual(text2num("sixty hundred", "en"), 6000)

def test_alpha2digit_integers(self):
source = "twenty-five cows, twelve chickens and one hundred twenty five kg of potatoes."
expected = "25 cows, 12 chickens and 125 kg of potatoes."
Expand Down Expand Up @@ -97,6 +101,11 @@ def test_relaxed(self):
expected = "34 = 34"
self.assertEqual(alpha2digit(source, "en", relaxed=True), expected)

def test_alpha2digit_hundreds(self):
source = "forty five hundred thirty eight dollars and eighteen cents"
expected = "4538 dollars and 18 cents"
self.assertEqual(alpha2digit(source, "en"), expected)

def test_alpha2digit_formal(self):
source = "plus thirty-three nine sixty zero six twelve twenty-one"
expected = "+33 9 60 06 12 21"
Expand Down
2 changes: 2 additions & 0 deletions text_to_num/lang/english.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
# Those words multiplies lesser numbers (see Rules)
# Special case: "hundred" is processed apart.
MULTIPLIERS = {
"hundred": 100,
"hundreds": 100,
"thousand": 1_000,
"thousands": 1_000,
"million": 1_000_000,
Expand Down
10 changes: 7 additions & 3 deletions text_to_num/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ def is_coef_appliable(self, coef: int) -> bool:
return coef != self.value

"""Is this multiplier expected?"""
if coef > self.value and (self.value > 0 or coef >= 1000):
if coef > self.value and (self.value > 0 or coef >= 100):
# a multiplier can be applied to anything lesser than itself,
# as long as it not zero (special case for 1000 which then implies 1)
return True
if coef * 1000 <= self.n000_val:
if coef * 1000 <= self.n000_val or coef == 100 and 100 > self.grp_val:
# a multiplier can not be applied to a value bigger than itself,
# so it must be applied to the current group only.
# ex. for "mille": "deux millions cent cinquante mille"
Expand All @@ -133,7 +133,7 @@ def is_coef_appliable(self, coef: int) -> bool:
# we test the 1000 × ``coef`` (as the multipliers above 100,
# are a geometric progression of ratio 1000)
return (
self.grp_val > 0 or coef == 1000
self.grp_val > 0 or coef == 1000 or coef == 100
) # "mille" without unit is additive
# TODO: There is one exception to the above rule: "de milliard"
# ex. : "mille milliards de milliards"
Expand Down Expand Up @@ -175,6 +175,10 @@ def push(self, word: str, look_ahead: Optional[str] = None) -> bool:
return False
# a multiplier can not be applied to a value bigger than itself,
# so it must be applied to the current group
if coef < 1000:
self.grp_val = (self.grp_val or 1) * coef
self.last_word = None
return True
if coef < self.n000_val:
self.n000_val = self.n000_val + coef * (
self.grp_val or 1
Expand Down