Upload 5 files
Browse files- convert2list.py +96 -0
- isNumber.py +16 -0
- processDoubles.py +25 -0
- replaceWords.py +159 -0
- text2int.py +199 -0
convert2list.py
ADDED
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# coding: utf-8
|
3 |
+
|
4 |
+
# In[30]:
|
5 |
+
|
6 |
+
|
7 |
+
# import nbimporter
|
8 |
+
import nbimporter
|
9 |
+
from Text2List import text_to_list
|
10 |
+
def convert_to_list(text, text_list):
|
11 |
+
matched_words = []
|
12 |
+
unmatched_text = '' # To accumulate unmatched characters
|
13 |
+
|
14 |
+
# Sort text_list by length in descending order to prioritize longest matches first
|
15 |
+
text_list_sorted = sorted(text_list, key=len, reverse=True)
|
16 |
+
|
17 |
+
while text:
|
18 |
+
matched = False
|
19 |
+
for word in text_list_sorted:
|
20 |
+
if text.startswith(word):
|
21 |
+
# Add any accumulated unmatched text before appending the matched word
|
22 |
+
if unmatched_text:
|
23 |
+
matched_words.append(unmatched_text)
|
24 |
+
unmatched_text = '' # Reset unmatched text accumulator
|
25 |
+
|
26 |
+
matched_words.append(word)
|
27 |
+
text = text[len(word):] # Remove the matched part from text
|
28 |
+
matched = True
|
29 |
+
break
|
30 |
+
|
31 |
+
if not matched:
|
32 |
+
# Accumulate unmatched characters
|
33 |
+
unmatched_text += text[0]
|
34 |
+
text = text[1:]
|
35 |
+
|
36 |
+
# If there's any remaining unmatched text, add it to the result
|
37 |
+
if unmatched_text:
|
38 |
+
matched_words.append(unmatched_text)
|
39 |
+
|
40 |
+
# Join matched words and unmatched text with a space
|
41 |
+
result = ' '.join(matched_words)
|
42 |
+
return result
|
43 |
+
|
44 |
+
text = "जीरोएकदोतीनचारपांचछहसातआठनौदसजीरोएकदोतीनचारपांच"
|
45 |
+
|
46 |
+
if __name__=="__main__":
|
47 |
+
converted=convert_to_list(text, text_to_list())
|
48 |
+
print(converted)
|
49 |
+
|
50 |
+
|
51 |
+
# In[33]:
|
52 |
+
|
53 |
+
|
54 |
+
# # import nbimporter
|
55 |
+
# import nbimporter
|
56 |
+
# from Text2List import text_to_list
|
57 |
+
# def convert_to_list(text, text_list):
|
58 |
+
# matched_words = []
|
59 |
+
# unmatched_text = '' # To accumulate unmatched characters
|
60 |
+
|
61 |
+
# # Sort text_list by length in descending order to prioritize longest matches first
|
62 |
+
# text_list_sorted = sorted(text_list, key=len, reverse=True)
|
63 |
+
|
64 |
+
# while text:
|
65 |
+
# matched = False
|
66 |
+
# for word in text_list_sorted:
|
67 |
+
# if word in text:
|
68 |
+
# # Add any accumulated unmatched text before appending the matched word
|
69 |
+
# if unmatched_text:
|
70 |
+
# matched_words.append(unmatched_text)
|
71 |
+
# unmatched_text = '' # Reset unmatched text accumulator
|
72 |
+
|
73 |
+
# matched_words.append(word)
|
74 |
+
# text = text[len(word):] # Remove the matched part from text
|
75 |
+
# matched = True
|
76 |
+
# break
|
77 |
+
|
78 |
+
# if not matched:
|
79 |
+
# # Accumulate unmatched characters
|
80 |
+
# unmatched_text += text[0]
|
81 |
+
# text = text[1:]
|
82 |
+
|
83 |
+
# # If there's any remaining unmatched text, add it to the result
|
84 |
+
# if unmatched_text:
|
85 |
+
# matched_words.append(unmatched_text)
|
86 |
+
|
87 |
+
# # Join matched words and unmatched text with a space
|
88 |
+
# result = ' '.join(matched_words)
|
89 |
+
# return result
|
90 |
+
|
91 |
+
# text = "जीरोएकदोतीनचार"
|
92 |
+
|
93 |
+
# if __name__=="__main__":
|
94 |
+
# converted=convert_to_list(text, text_to_list())
|
95 |
+
# print(converted)
|
96 |
+
|
isNumber.py
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# coding: utf-8
|
3 |
+
|
4 |
+
# In[ ]:
|
5 |
+
|
6 |
+
|
7 |
+
# Function to check if the string is a number
|
8 |
+
def is_number(x):
|
9 |
+
if type(x) == str:
|
10 |
+
x = x.replace(',', '')
|
11 |
+
try:
|
12 |
+
float(x)
|
13 |
+
except:
|
14 |
+
return False
|
15 |
+
return True
|
16 |
+
|
processDoubles.py
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# coding: utf-8
|
3 |
+
|
4 |
+
# In[ ]:
|
5 |
+
|
6 |
+
|
7 |
+
# Function to process "double" followed by a number
|
8 |
+
def process_doubles(sentence):
|
9 |
+
tokens = sentence.split()
|
10 |
+
result = []
|
11 |
+
i = 0
|
12 |
+
while i < len(tokens):
|
13 |
+
if tokens[i] == "डबल":
|
14 |
+
if i + 1 < len(tokens):
|
15 |
+
result.append(tokens[i + 1])
|
16 |
+
result.append(tokens[i + 1])
|
17 |
+
i += 2
|
18 |
+
else:
|
19 |
+
result.append(tokens[i])
|
20 |
+
i += 1
|
21 |
+
else:
|
22 |
+
result.append(tokens[i])
|
23 |
+
i += 1
|
24 |
+
return ' '.join(result)
|
25 |
+
|
replaceWords.py
ADDED
@@ -0,0 +1,159 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# coding: utf-8
|
3 |
+
|
4 |
+
# In[7]:
|
5 |
+
|
6 |
+
|
7 |
+
import re
|
8 |
+
|
9 |
+
def replace_words(sentence):
|
10 |
+
# Define a dictionary mapping a single word to a list of words or phrases
|
11 |
+
replacement_map = {
|
12 |
+
# Multiples of ten
|
13 |
+
'twenty': ['ट्वेंटी', 'बीस'],
|
14 |
+
'thirty': ['थर्टी', 'तीस'],
|
15 |
+
'forty': ['फोर्टी', 'चालीस'],
|
16 |
+
'fifty': ['फिफ्टी', 'पचास'],
|
17 |
+
'sixty': ['सिक्स्टी', 'साठ'],
|
18 |
+
'seventy': ['सेवंटी', 'सत्तर','सेवनटी','सेवेनटी','सेवांटी'],
|
19 |
+
'eighty': ['एटी', 'अस्सी'],
|
20 |
+
'ninety': ['नाइंटी', 'नब्बे'],
|
21 |
+
|
22 |
+
# Numbers from 11 to 19
|
23 |
+
'eleven': ['इलेवन', 'ग्यारह','इगारा'],
|
24 |
+
'twelve': ['ट्वेल्व', 'बारह'],
|
25 |
+
'thirteen': ['थर्टीन', 'तेरह','तेरा'],
|
26 |
+
'fourteen': ['फोर्टीन', 'चौदह'],
|
27 |
+
'fifteen': ['फिफ्टीन', 'पंद्रह','पंद्रा'],
|
28 |
+
'sixteen': ['सिक्स्टीन', 'सोलह','सोल्ला'],
|
29 |
+
'seventeen': ['सेवंटीन', 'सत्रह''सतरा'],
|
30 |
+
'eighteen': ['एटीन', 'अठारह''अठारा'],
|
31 |
+
'nineteen': ['नाइनटीन', 'उन्नीस','उन्नईस','उनाइस'],
|
32 |
+
|
33 |
+
# Numbers from 21 to 29
|
34 |
+
'twenty one': ['ट्वेंटी वन', 'इक्कीस'],
|
35 |
+
'twenty two': ['ट्वेंटी टू', 'बाईस'],
|
36 |
+
'twenty three': ['ट्वेंटी थ्री', 'तेईस'],
|
37 |
+
'twenty four': ['ट्वेंटी फोर', 'चौबीस'],
|
38 |
+
'twenty five': ['ट्वेंटी फाइव', 'पच्चीस'],
|
39 |
+
'twenty six': ['ट्वेंटी सिक्स', 'छब्बीस'],
|
40 |
+
'twenty seven': ['ट्वेंटी सेवन', 'सत्ताईस','सताईस'],
|
41 |
+
'twenty eight': ['ट्वेंटी एट', 'अट्ठाईस','अठ्ठाइस','अठ्ठाईस'],
|
42 |
+
'twenty nine': ['ट्वेंटी नाइन', 'उनतीस'],
|
43 |
+
|
44 |
+
# Numbers from 31 to 39
|
45 |
+
'thirty one': ['थर्टी वन', 'इकतीस'],
|
46 |
+
'thirty two': ['थर्टी टू', 'बत्तीस'],
|
47 |
+
'thirty three': ['थर्टी थ्री', 'तेतीस'],
|
48 |
+
'thirty four': ['थर्टी फोर', 'चौंतीस'],
|
49 |
+
'thirty five': ['थर्टी फाइव', 'पैंतीस'],
|
50 |
+
'thirty six': ['थर्टी सिक्स', 'छत्तीस'],
|
51 |
+
'thirty seven': ['थर्टी सेवन', 'सैंतीस'],
|
52 |
+
'thirty eight': ['थर्टी एट', 'अड़तीस'],
|
53 |
+
'thirty nine': ['थर्टी नाइन', 'उनतालीस'],
|
54 |
+
|
55 |
+
# Numbers from 41 to 49
|
56 |
+
'forty one': ['फोर्टी वन', 'इकतालीस'],
|
57 |
+
'forty two': ['फोर्टी टू', 'बयालीस'],
|
58 |
+
'forty three': ['फोर्टी थ्री', 'तैंतालीस'],
|
59 |
+
'forty four': ['फोर्टी फोर', 'चौंतालीस'],
|
60 |
+
'forty five': ['फोर्टी फाइव', 'पैंतालीस'],
|
61 |
+
'forty six': ['फोर्टी सिक्स', 'छयालिस'],
|
62 |
+
'forty seven': ['फोर्टी सेवन', 'सैंतालीस'],
|
63 |
+
'forty eight': ['फोर्टी एट', 'अड़तालीस'],
|
64 |
+
'forty nine': ['फोर्टी नाइन', 'उनचास'],
|
65 |
+
|
66 |
+
# Numbers from 51 to 59
|
67 |
+
'fifty one': ['फिफ्टी वन', 'इक्यावन'],
|
68 |
+
'fifty two': ['फिफ्टी टू', 'बावन'],
|
69 |
+
'fifty three': ['फिफ्टी थ्री', 'तिरेपन','तिरपन','तीरपन'],
|
70 |
+
'fifty four': ['फिफ्टी फोर', 'चौवन'],
|
71 |
+
'fifty five': ['फिफ्टी फाइव', 'पचपन'],
|
72 |
+
'fifty six': ['फिफ्टी सिक्स', 'छप्पन','छपपन'],
|
73 |
+
'fifty seven': ['फिफ्टी सेवन', 'सत्तावन','संताबन','संतावन'],
|
74 |
+
'fifty eight': ['फिफ्टी एट', 'अट्ठावन','अंठावन'],
|
75 |
+
'fifty nine': ['फिफ्टी नाइन', 'उनसठ','उंसट','उंसठ'],
|
76 |
+
|
77 |
+
# Numbers from 61 to 69
|
78 |
+
'sixty one': ['सिक्स्टी वन', 'इकसठ'],
|
79 |
+
'sixty two': ['सिक्स्टी टू', 'बासठ'],
|
80 |
+
'sixty three': ['सिक्स्टी थ्री', 'तिरसठ'],
|
81 |
+
'sixty four': ['सिक्स्टी फोर', 'चौंसठ'],
|
82 |
+
'sixty five': ['सिक्स्टी फाइव', 'पैंसठ'],
|
83 |
+
'sixty six': ['सिक्स्टी सिक्स', 'छियासठ'],
|
84 |
+
'sixty seven': ['सिक्स्टी सेवन', 'सड़सठ'],
|
85 |
+
'sixty eight': ['सिक्स्टी एट', 'अड़सठ'],
|
86 |
+
'sixty nine': ['सिक्स्टी नाइन', 'उनहत्तर'],
|
87 |
+
|
88 |
+
# Numbers from 71 to 79
|
89 |
+
'seventy one': ['सेवंटी वन', 'इकहत्तर','इखत्तर','इकत्तर'],
|
90 |
+
'seventy two': ['सेवंटी टू', 'बहत्तर'],
|
91 |
+
'seventy three': ['सेवंटी थ्री', 'तिहत्तर','तियत्र','तियत्तर','तीहत्तर','तिहत्थर'],
|
92 |
+
'seventy four': ['सेवंटी फोर', 'चौहत्तर',],
|
93 |
+
'seventy five': ['सेवंटी फाइव', 'पचहत्तर','पछत्तर','पिछत्तर','पचहत्तर','पचत्तर'],
|
94 |
+
'seventy six': ['सेवंटी सिक्स', 'छिहत्तर','छीहत्तर'],
|
95 |
+
'seventy seven': ['सेवंटी सेवन', 'सतहत्तर','सतात्तर','सतत्तर','सतहत्थर'],
|
96 |
+
'seventy eight': ['सेवंटी एट', 'अठहत्तर','अठत्तर'],
|
97 |
+
'seventy nine': ['सेवंटी नाइन', 'उन्यासी','उनासी'],
|
98 |
+
|
99 |
+
# Numbers from 81 to 89
|
100 |
+
'eighty one': ['एटी वन', 'इक्यासी'],
|
101 |
+
'eighty two': ['एटी टू', 'बयासी'],
|
102 |
+
'eighty three': ['एटी थ्री', 'तिरासी'],
|
103 |
+
'eighty four': ['एटी फोर', 'चौरासी'],
|
104 |
+
'eighty five': ['एटी फाइव', 'पचासी','पिचासी'],
|
105 |
+
'eighty six': ['एटी सिक्स', 'छियासी'],
|
106 |
+
'eighty seven': ['एटी सेवन', 'सतासी'],
|
107 |
+
'eighty eight': ['एटी एट', 'अठासी'],
|
108 |
+
'eighty nine': ['एटी नाइन', 'नवासी'],
|
109 |
+
|
110 |
+
# Numbers from 91 to 99
|
111 |
+
'ninety one': ['नाइंटी वन', 'इक्यानवे'],
|
112 |
+
'ninety two': ['नाइंटी टू', 'बानवे','बानबे'],
|
113 |
+
'ninety three': ['नाइंटी थ्री', 'तिरानवे'],
|
114 |
+
'ninety four': ['नाइंटी फोर', 'चौरानवे'],
|
115 |
+
'ninety five': ['नाइंटी फाइव', 'पचानवे'],
|
116 |
+
'ninety six': ['नाइंटी सिक्स', 'छियानवे'],
|
117 |
+
'ninety seven': ['नाइंटी सेवन', 'सतानवे'],
|
118 |
+
'ninety eight': ['नाइंटी एट', 'अठानवे'],
|
119 |
+
'ninety nine': ['नाइंटी नाइन', 'निन्यानवे'],
|
120 |
+
# Numbers from one to ten
|
121 |
+
'seven': ['सेवन', 'सात'],
|
122 |
+
'zero': ['शून्य', 'जेरो', 'शुन्ना', 'जीरो'],
|
123 |
+
'one': ['वन', 'एंक', 'इक', 'एक'],
|
124 |
+
'two': ['टू', 'दो'],
|
125 |
+
'three': ['थ्री', 'तीना', 'तीन', 'त्री'],
|
126 |
+
'four': ['फोर','फ़ोर', 'फॉर', 'च्यार', 'चार'],
|
127 |
+
'five': ['फाइव', 'पाँच', 'पांच'],
|
128 |
+
'six': ['सिक्स', 'चह', 'छौ', 'छै', 'छह', 'छे'],
|
129 |
+
'eight': ['एट', 'अट', 'आठ'],
|
130 |
+
'nine': ['नाइन', 'नौ'],
|
131 |
+
'ten': ['टेन', 'दस'],
|
132 |
+
# Hundred
|
133 |
+
'hundred': ['हंड्रेड', 'सौ','सो','साव'],
|
134 |
+
# Thousand
|
135 |
+
'thousand' : ['हजार','थौजनड','थाउजंड','हज़ार'],
|
136 |
+
# Lakhs
|
137 |
+
'lac' : ['लाख'],
|
138 |
+
|
139 |
+
# Special for double digits
|
140 |
+
'डबल': ['दबल', 'डबल', 'दुबाल'],
|
141 |
+
}
|
142 |
+
|
143 |
+
words = sentence.split() # Split the sentence by spaces
|
144 |
+
|
145 |
+
# Replace words using the mapping
|
146 |
+
for i, word in enumerate(words):
|
147 |
+
for replacement, patterns in replacement_map.items():
|
148 |
+
if word in patterns:
|
149 |
+
words[i] = replacement # Replace the word if it's fully matched
|
150 |
+
|
151 |
+
# Join the processed words back into a sentence
|
152 |
+
return ' '.join(words)
|
153 |
+
|
154 |
+
|
155 |
+
# In[ ]:
|
156 |
+
|
157 |
+
|
158 |
+
|
159 |
+
|
text2int.py
ADDED
@@ -0,0 +1,199 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# coding: utf-8
|
3 |
+
|
4 |
+
# In[ ]:
|
5 |
+
|
6 |
+
|
7 |
+
# # Function to convert Hindi text to numerical representation
|
8 |
+
# from isNumber import is_number
|
9 |
+
|
10 |
+
# def text_to_int (textnum, numwords={}):
|
11 |
+
# units = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight',
|
12 |
+
# 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen',
|
13 |
+
# 'sixteen', 'seventeen', 'eighteen', 'nineteen']
|
14 |
+
# tens = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']
|
15 |
+
# scales = ['hundred', 'thousand', 'lac','million', 'billion', 'trillion']
|
16 |
+
# ordinal_words = {'first':1, 'second':2, 'third':3, 'fifth':5, 'eighth':8, 'ninth':9, 'twelfth':12}
|
17 |
+
# ordinal_endings = [('ieth', 'y'), ('th', '')]
|
18 |
+
|
19 |
+
# if not numwords:
|
20 |
+
# numwords['and'] = (1, 0)
|
21 |
+
# for idx, word in enumerate(units): numwords[word] = (1, idx)
|
22 |
+
# for idx, word in enumerate(tens): numwords[word] = (1, idx * 10)
|
23 |
+
# for idx, word in enumerate(scales): numwords[word] = (10 ** (idx * 3 or 2), 0)
|
24 |
+
|
25 |
+
# textnum = textnum.replace('-', ' ')
|
26 |
+
|
27 |
+
# current = result = 0
|
28 |
+
# curstring = ''
|
29 |
+
# onnumber = False
|
30 |
+
# lastunit = False
|
31 |
+
# lastscale = False
|
32 |
+
|
33 |
+
# def is_numword(x):
|
34 |
+
# if is_number(x):
|
35 |
+
# return True
|
36 |
+
# if word in numwords:
|
37 |
+
# return True
|
38 |
+
# return False
|
39 |
+
|
40 |
+
# def from_numword(x):
|
41 |
+
# if is_number(x):
|
42 |
+
# scale = 0
|
43 |
+
# increment = int(x.replace(',', ''))
|
44 |
+
# return scale, increment
|
45 |
+
# return numwords[x]
|
46 |
+
|
47 |
+
# for word in textnum.split():
|
48 |
+
# if word in ordinal_words:
|
49 |
+
# scale, increment = (1, ordinal_words[word])
|
50 |
+
# current = current * scale + increment
|
51 |
+
# if scale > 100:
|
52 |
+
# result += current
|
53 |
+
# current = 0
|
54 |
+
# onnumber = True
|
55 |
+
# lastunit = False
|
56 |
+
# lastscale = False
|
57 |
+
# else:
|
58 |
+
# for ending, replacement in ordinal_endings:
|
59 |
+
# if word.endswith(ending):
|
60 |
+
# word = "%s%s" % (word[:-len(ending)], replacement)
|
61 |
+
|
62 |
+
# if (not is_numword(word)) or (word == 'and' and not lastscale):
|
63 |
+
# if onnumber:
|
64 |
+
# # Flush the current number we are building
|
65 |
+
# curstring += repr(result + current) + " "
|
66 |
+
# curstring += word + " "
|
67 |
+
# result = current = 0
|
68 |
+
# onnumber = False
|
69 |
+
# lastunit = False
|
70 |
+
# lastscale = False
|
71 |
+
# else:
|
72 |
+
# scale, increment = from_numword(word)
|
73 |
+
# onnumber = True
|
74 |
+
|
75 |
+
# if lastunit and (word not in scales):
|
76 |
+
# # Assume this is part of a string of individual numbers to
|
77 |
+
# # be flushed, such as a zipcode "one two three four five"
|
78 |
+
# curstring += repr(result + current)
|
79 |
+
# result = current = 0
|
80 |
+
|
81 |
+
# if scale > 1:
|
82 |
+
# current = max(1, current)
|
83 |
+
|
84 |
+
# current = current * scale + increment
|
85 |
+
# if scale > 100:
|
86 |
+
# result += current
|
87 |
+
# current = 0
|
88 |
+
|
89 |
+
# lastscale = False
|
90 |
+
# lastunit = False
|
91 |
+
# if word in scales:
|
92 |
+
# lastscale = True
|
93 |
+
# elif word in units:
|
94 |
+
# lastunit = True
|
95 |
+
|
96 |
+
# if onnumber:
|
97 |
+
# curstring += repr(result + current)
|
98 |
+
|
99 |
+
# return curstring
|
100 |
+
|
101 |
+
|
102 |
+
# In[ ]:
|
103 |
+
|
104 |
+
|
105 |
+
from isNumber import is_number # Remove or replace this if unnecessary
|
106 |
+
|
107 |
+
def text_to_int(textnum, numwords={}):
|
108 |
+
# Define units, tens, and scales including "lac"
|
109 |
+
units = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight',
|
110 |
+
'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen',
|
111 |
+
'sixteen', 'seventeen', 'eighteen', 'nineteen']
|
112 |
+
tens = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']
|
113 |
+
scales = ['hundred', 'thousand', 'lac', 'million', 'billion', 'trillion'] # "lac" added
|
114 |
+
ordinal_words = {'first': 1, 'second': 2, 'third': 3, 'fifth': 5, 'eighth': 8, 'ninth': 9, 'twelfth': 12}
|
115 |
+
ordinal_endings = [('ieth', 'y'), ('th', '')]
|
116 |
+
|
117 |
+
if not numwords:
|
118 |
+
numwords['and'] = (1, 0) # Handle "one hundred and twenty"
|
119 |
+
|
120 |
+
# Add units, tens, and scales to numwords
|
121 |
+
for idx, word in enumerate(units):
|
122 |
+
numwords[word] = (1, idx)
|
123 |
+
for idx, word in enumerate(tens):
|
124 |
+
numwords[word] = (1, idx * 10)
|
125 |
+
|
126 |
+
for idx, word in enumerate(scales):
|
127 |
+
numwords[word] = (10 ** (5 if word == 'lac' else idx * 3 or 2), 0) # Handle "lac" as 10^5
|
128 |
+
|
129 |
+
# Remove hyphens and normalize input
|
130 |
+
textnum = textnum.replace('-', ' ')
|
131 |
+
|
132 |
+
current = result = 0
|
133 |
+
curstring = ''
|
134 |
+
onnumber = False
|
135 |
+
lastunit = False
|
136 |
+
lastscale = False
|
137 |
+
|
138 |
+
def is_numword(x):
|
139 |
+
return is_number(x) or x in numwords
|
140 |
+
|
141 |
+
def from_numword(x):
|
142 |
+
if is_number(x):
|
143 |
+
return 0, int(x.replace(',', ''))
|
144 |
+
return numwords[x]
|
145 |
+
|
146 |
+
for word in textnum.split():
|
147 |
+
if word in ordinal_words:
|
148 |
+
scale, increment = (1, ordinal_words[word])
|
149 |
+
current = current * scale + increment
|
150 |
+
if scale > 100:
|
151 |
+
result += current
|
152 |
+
current = 0
|
153 |
+
onnumber = True
|
154 |
+
lastunit = False
|
155 |
+
lastscale = False
|
156 |
+
else:
|
157 |
+
for ending, replacement in ordinal_endings:
|
158 |
+
if word.endswith(ending):
|
159 |
+
word = f"{word[:-len(ending)]}{replacement}"
|
160 |
+
|
161 |
+
if not is_numword(word) or (word == 'and' and not lastscale):
|
162 |
+
if onnumber:
|
163 |
+
curstring += repr(result + current) + " "
|
164 |
+
curstring += word + " "
|
165 |
+
result = current = 0
|
166 |
+
onnumber = False
|
167 |
+
lastunit = False
|
168 |
+
lastscale = False
|
169 |
+
else:
|
170 |
+
scale, increment = from_numword(word)
|
171 |
+
onnumber = True
|
172 |
+
|
173 |
+
if lastunit and word not in scales:
|
174 |
+
curstring += repr(result + current) + " "
|
175 |
+
result = current = 0
|
176 |
+
|
177 |
+
if scale > 1:
|
178 |
+
current = max(1, current)
|
179 |
+
|
180 |
+
current = current * scale + increment
|
181 |
+
|
182 |
+
if scale >= 100:
|
183 |
+
result += current
|
184 |
+
current = 0
|
185 |
+
|
186 |
+
lastscale = word in scales
|
187 |
+
lastunit = word in units
|
188 |
+
|
189 |
+
if onnumber:
|
190 |
+
curstring += repr(result + current)
|
191 |
+
|
192 |
+
return curstring.strip()
|
193 |
+
|
194 |
+
|
195 |
+
# In[ ]:
|
196 |
+
|
197 |
+
|
198 |
+
|
199 |
+
|