Skip to content

Commit faa24f8

Browse files
committed
Handle hyphenated numbers
1 parent c97d980 commit faa24f8

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

‎mathparse/mathparse.py‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ def replace_word_tokens(string: str, language: str) -> str:
107107
return the string with the words replaced with
108108
an operational equivalent.
109109
"""
110-
import re
111110
words = mathwords.word_groups_for_language(language)
112111

113112
# Replace binary operator words with numeric operators
@@ -149,6 +148,15 @@ def replace_word_tokens(string: str, language: str) -> str:
149148
1, 2, 3, 4, 5, 6, 7, 8, 9
150149
]}
151150

151+
# Preprocess hyphenated compound numbers
152+
# (e.g., "fifty-four" -> "fifty four")
153+
for tens_word in tens_words.keys():
154+
for units_word in units_words.keys():
155+
hyphenated_pattern = tens_word + r'-' + units_word
156+
space_separated = tens_word + ' ' + units_word
157+
if re.search(hyphenated_pattern, string):
158+
string = re.sub(hyphenated_pattern, space_separated, string)
159+
152160
# Replace compound numbers first (before individual number replacement)
153161
for tens_word, tens_value in tens_words.items():
154162
for units_word, units_value in units_words.items():

‎tests/test_replace_word_tokens.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,9 @@ def test_tens_and_hundreds_with_spaces(self):
5555
'one hundred times fifty four', language='ENG'
5656
)
5757
self.assertEqual(result, '(1 * 100) * (50 + 4)')
58+
59+
def test_hyphenated_numbers(self):
60+
result = mathparse.replace_word_tokens(
61+
'one hundred times fifty-four', language='ENG'
62+
)
63+
self.assertEqual(result, '(1 * 100) * (50 + 4)')

0 commit comments

Comments
 (0)