Spaces:
Sleeping
Sleeping
Update text2int.py
Browse files- text2int.py +88 -102
text2int.py
CHANGED
|
@@ -1,102 +1,88 @@
|
|
| 1 |
-
#
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
scale
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
lastscale = word in scales
|
| 90 |
-
lastunit = word in units
|
| 91 |
-
|
| 92 |
-
if onnumber:
|
| 93 |
-
curstring += repr(result + current)
|
| 94 |
-
|
| 95 |
-
return curstring.strip()
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
# In[ ]:
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
| 1 |
+
from isNumber import is_number # Remove or replace this if unnecessary
|
| 2 |
+
|
| 3 |
+
def text_to_int(textnum, numwords={}):
|
| 4 |
+
# Define units, tens, and scales including "lac"
|
| 5 |
+
units = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight',
|
| 6 |
+
'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen',
|
| 7 |
+
'sixteen', 'seventeen', 'eighteen', 'nineteen']
|
| 8 |
+
tens = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']
|
| 9 |
+
scales = ['hundred', 'thousand', 'lac', 'million', 'billion', 'trillion'] # "lac" added
|
| 10 |
+
ordinal_words = {'first': 1, 'second': 2, 'third': 3, 'fifth': 5, 'eighth': 8, 'ninth': 9, 'twelfth': 12}
|
| 11 |
+
ordinal_endings = [('ieth', 'y'), ('th', '')]
|
| 12 |
+
|
| 13 |
+
if not numwords:
|
| 14 |
+
numwords['and'] = (1, 0) # Handle "one hundred and twenty"
|
| 15 |
+
|
| 16 |
+
# Add units, tens, and scales to numwords
|
| 17 |
+
for idx, word in enumerate(units):
|
| 18 |
+
numwords[word] = (1, idx)
|
| 19 |
+
for idx, word in enumerate(tens):
|
| 20 |
+
numwords[word] = (1, idx * 10)
|
| 21 |
+
|
| 22 |
+
for idx, word in enumerate(scales):
|
| 23 |
+
numwords[word] = (10 ** (5 if word == 'lac' else idx * 3 or 2), 0) # Handle "lac" as 10^5
|
| 24 |
+
|
| 25 |
+
# Remove hyphens and normalize input
|
| 26 |
+
textnum = textnum.replace('-', ' ')
|
| 27 |
+
|
| 28 |
+
current = result = 0
|
| 29 |
+
curstring = ''
|
| 30 |
+
onnumber = False
|
| 31 |
+
lastunit = False
|
| 32 |
+
lastscale = False
|
| 33 |
+
|
| 34 |
+
def is_numword(x):
|
| 35 |
+
return is_number(x) or x in numwords
|
| 36 |
+
|
| 37 |
+
def from_numword(x):
|
| 38 |
+
if is_number(x):
|
| 39 |
+
return 0, int(x.replace(',', ''))
|
| 40 |
+
return numwords[x]
|
| 41 |
+
|
| 42 |
+
for word in textnum.split():
|
| 43 |
+
if word in ordinal_words:
|
| 44 |
+
scale, increment = (1, ordinal_words[word])
|
| 45 |
+
current = current * scale + increment
|
| 46 |
+
if scale > 100:
|
| 47 |
+
result += current
|
| 48 |
+
current = 0
|
| 49 |
+
onnumber = True
|
| 50 |
+
lastunit = False
|
| 51 |
+
lastscale = False
|
| 52 |
+
else:
|
| 53 |
+
for ending, replacement in ordinal_endings:
|
| 54 |
+
if word.endswith(ending):
|
| 55 |
+
word = f"{word[:-len(ending)]}{replacement}"
|
| 56 |
+
|
| 57 |
+
if not is_numword(word) or (word == 'and' and not lastscale):
|
| 58 |
+
if onnumber:
|
| 59 |
+
curstring += repr(result + current) + " "
|
| 60 |
+
curstring += word + " "
|
| 61 |
+
result = current = 0
|
| 62 |
+
onnumber = False
|
| 63 |
+
lastunit = False
|
| 64 |
+
lastscale = False
|
| 65 |
+
else:
|
| 66 |
+
scale, increment = from_numword(word)
|
| 67 |
+
onnumber = True
|
| 68 |
+
|
| 69 |
+
if lastunit and word not in scales:
|
| 70 |
+
curstring += repr(result + current) + " "
|
| 71 |
+
result = current = 0
|
| 72 |
+
|
| 73 |
+
if scale > 1:
|
| 74 |
+
current = max(1, current)
|
| 75 |
+
|
| 76 |
+
current = current * scale + increment
|
| 77 |
+
|
| 78 |
+
if scale >= 100:
|
| 79 |
+
result += current
|
| 80 |
+
current = 0
|
| 81 |
+
|
| 82 |
+
lastscale = word in scales
|
| 83 |
+
lastunit = word in units
|
| 84 |
+
|
| 85 |
+
if onnumber:
|
| 86 |
+
curstring += repr(result + current)
|
| 87 |
+
|
| 88 |
+
return curstring.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|