Spaces:
Sleeping
Sleeping
Upload 6 files
Browse files- Text2List.py +57 -0
- convert2list.py +87 -0
- isNumber.py +16 -0
- processDoubles.py +93 -0
- replaceWords.py +156 -0
- text2int.py +199 -0
Text2List.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# coding: utf-8
|
3 |
+
|
4 |
+
# In[4]:
|
5 |
+
|
6 |
+
|
7 |
+
def text_to_list():
|
8 |
+
text_list = [
|
9 |
+
# English numbers (11-19)
|
10 |
+
'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen',
|
11 |
+
|
12 |
+
# English multiples of ten (20, 30, ..., 90)
|
13 |
+
'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'seventy', 'seventy', 'seventy', 'seventy', 'seventy', 'eighty', 'ninety',
|
14 |
+
|
15 |
+
# English combinations of 21-29
|
16 |
+
'twenty one', 'twenty two', 'twenty three', 'twenty four', 'twenty five', 'twenty six', 'twenty seven', 'twenty eight', 'twenty nine',
|
17 |
+
|
18 |
+
# English combinations of 31-39
|
19 |
+
'thirty one', 'thirty two', 'thirty three', 'thirty four', 'thirty five', 'thirty six', 'thirty seven', 'thirty eight', 'thirty nine',
|
20 |
+
|
21 |
+
# English combinations of 41-49
|
22 |
+
'forty one', 'forty two', 'forty three', 'forty four', 'forty five', 'forty six', 'forty seven', 'forty eight', 'forty nine',
|
23 |
+
|
24 |
+
# English combinations of 51-59
|
25 |
+
'fifty one', 'fifty two', 'fifty three', 'fifty four', 'fifty five', 'fifty six', 'fifty seven', 'fifty eight', 'fifty nine',
|
26 |
+
|
27 |
+
# English combinations of 61-69
|
28 |
+
'sixty one', 'sixty two', 'sixty three', 'sixty four', 'sixty five', 'sixty six', 'sixty seven', 'sixty eight', 'sixty nine',
|
29 |
+
|
30 |
+
# English combinations of 71-79
|
31 |
+
'seventy one', 'seventy two', 'seventy three', 'seventy four', 'seventy five', 'seventy six', 'seventy seven', 'seventy eight', 'seventy nine',
|
32 |
+
|
33 |
+
# English combinations of 81-89
|
34 |
+
'eighty one', 'eighty two', 'eighty three', 'eighty four', 'eighty five', 'eighty six', 'eighty seven', 'eighty eight', 'eighty nine',
|
35 |
+
|
36 |
+
# English combinations of 91-99
|
37 |
+
'ninety one', 'ninety two', 'ninety three', 'ninety four', 'ninety five', 'ninety six', 'ninety seven', 'ninety eight', 'ninety nine',
|
38 |
+
|
39 |
+
# English numbers (0-10)
|
40 |
+
'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten',
|
41 |
+
|
42 |
+
# English for 100
|
43 |
+
'hundred',
|
44 |
+
# English for 1000
|
45 |
+
'thousand'
|
46 |
+
# English for 100000
|
47 |
+
'lakh'
|
48 |
+
]
|
49 |
+
|
50 |
+
return text_list
|
51 |
+
|
52 |
+
|
53 |
+
# In[ ]:
|
54 |
+
|
55 |
+
|
56 |
+
|
57 |
+
|
convert2list.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# import nbimporter
|
2 |
+
import nbimporter
|
3 |
+
from Text2List import text_to_list
|
4 |
+
def convert_to_list(text, text_list):
|
5 |
+
matched_words = []
|
6 |
+
unmatched_text = '' # To accumulate unmatched characters
|
7 |
+
|
8 |
+
# Sort text_list by length in descending order to prioritize longest matches first
|
9 |
+
text_list_sorted = sorted(text_list, key=len, reverse=True)
|
10 |
+
|
11 |
+
while text:
|
12 |
+
matched = False
|
13 |
+
for word in text_list_sorted:
|
14 |
+
if text.startswith(word):
|
15 |
+
# Add any accumulated unmatched text before appending the matched word
|
16 |
+
if unmatched_text:
|
17 |
+
matched_words.append(unmatched_text)
|
18 |
+
unmatched_text = '' # Reset unmatched text accumulator
|
19 |
+
|
20 |
+
matched_words.append(word)
|
21 |
+
text = text[len(word):] # Remove the matched part from text
|
22 |
+
matched = True
|
23 |
+
break
|
24 |
+
|
25 |
+
if not matched:
|
26 |
+
# Accumulate unmatched characters
|
27 |
+
unmatched_text += text[0]
|
28 |
+
text = text[1:]
|
29 |
+
|
30 |
+
# If there's any remaining unmatched text, add it to the result
|
31 |
+
if unmatched_text:
|
32 |
+
matched_words.append(unmatched_text)
|
33 |
+
|
34 |
+
# Join matched words and unmatched text with a space
|
35 |
+
result = ' '.join(matched_words)
|
36 |
+
return result
|
37 |
+
|
38 |
+
# text = "जीरोएकदोतीनचारपांचछहसातआठनौदसजीरोएकदोतीनचारपांच"
|
39 |
+
|
40 |
+
if __name__=="__main__":
|
41 |
+
converted=convert_to_list(text, text_to_list())
|
42 |
+
print(converted)
|
43 |
+
|
44 |
+
|
45 |
+
# # import nbimporter
|
46 |
+
# import nbimporter
|
47 |
+
# from Text2List import text_to_list
|
48 |
+
# def convert_to_list(text, text_list):
|
49 |
+
# matched_words = []
|
50 |
+
# unmatched_text = '' # To accumulate unmatched characters
|
51 |
+
|
52 |
+
# # Sort text_list by length in descending order to prioritize longest matches first
|
53 |
+
# text_list_sorted = sorted(text_list, key=len, reverse=True)
|
54 |
+
|
55 |
+
# while text:
|
56 |
+
# matched = False
|
57 |
+
# for word in text_list_sorted:
|
58 |
+
# if word in text:
|
59 |
+
# # Add any accumulated unmatched text before appending the matched word
|
60 |
+
# if unmatched_text:
|
61 |
+
# matched_words.append(unmatched_text)
|
62 |
+
# unmatched_text = '' # Reset unmatched text accumulator
|
63 |
+
|
64 |
+
# matched_words.append(word)
|
65 |
+
# text = text[len(word):] # Remove the matched part from text
|
66 |
+
# matched = True
|
67 |
+
# break
|
68 |
+
|
69 |
+
# if not matched:
|
70 |
+
# # Accumulate unmatched characters
|
71 |
+
# unmatched_text += text[0]
|
72 |
+
# text = text[1:]
|
73 |
+
|
74 |
+
# # If there's any remaining unmatched text, add it to the result
|
75 |
+
# if unmatched_text:
|
76 |
+
# matched_words.append(unmatched_text)
|
77 |
+
|
78 |
+
# # Join matched words and unmatched text with a space
|
79 |
+
# result = ' '.join(matched_words)
|
80 |
+
# return result
|
81 |
+
|
82 |
+
# text = "जीरोएकदोतीनचार"
|
83 |
+
|
84 |
+
# if __name__=="__main__":
|
85 |
+
# converted=convert_to_list(text, text_to_list())
|
86 |
+
# print(converted)
|
87 |
+
|
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,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# coding: utf-8
|
3 |
+
|
4 |
+
# In[1]:
|
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] == "double":
|
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 |
+
|
26 |
+
|
27 |
+
# In[ ]:
|
28 |
+
|
29 |
+
|
30 |
+
# import re
|
31 |
+
|
32 |
+
# def process_doubles(sentence):
|
33 |
+
# # Use regex to split 'डबल' followed by numbers/words without space (e.g., "डबलवन" -> "डबल वन")
|
34 |
+
# sentence = re.sub(r'(डबल)(\S+)', r'\1 \2', sentence)
|
35 |
+
|
36 |
+
# tokens = sentence.split()
|
37 |
+
# result = []
|
38 |
+
# i = 0
|
39 |
+
|
40 |
+
# while i < len(tokens):
|
41 |
+
# if tokens[i] == "डबल":
|
42 |
+
# if i + 1 < len(tokens):
|
43 |
+
# result.append(tokens[i + 1]) # Append the next word/number
|
44 |
+
# result.append(tokens[i + 1]) # Append the next word/number again to duplicate
|
45 |
+
# i += 2 # Skip over the next word since it's already processed
|
46 |
+
# else:
|
47 |
+
# result.append(tokens[i])
|
48 |
+
# i += 1
|
49 |
+
# else:
|
50 |
+
# result.append(tokens[i])
|
51 |
+
# i += 1
|
52 |
+
|
53 |
+
# return ' '.join(result)
|
54 |
+
|
55 |
+
|
56 |
+
# In[2]:
|
57 |
+
|
58 |
+
|
59 |
+
# Function to process "double" and "triple" followed by a number
|
60 |
+
|
61 |
+
def process_multiples(sentence):
|
62 |
+
tokens = sentence.split()
|
63 |
+
result = []
|
64 |
+
i = 0
|
65 |
+
while i < len(tokens):
|
66 |
+
if tokens[i] == "double":
|
67 |
+
if i + 1 < len(tokens):
|
68 |
+
result.append(tokens[i + 1])
|
69 |
+
result.append(tokens[i + 1])
|
70 |
+
i += 2
|
71 |
+
else:
|
72 |
+
result.append(tokens[i])
|
73 |
+
i += 1
|
74 |
+
elif tokens[i] == "triple":
|
75 |
+
if i + 1 < len(tokens):
|
76 |
+
result.append(tokens[i + 1])
|
77 |
+
result.append(tokens[i + 1])
|
78 |
+
result.append(tokens[i + 1])
|
79 |
+
i += 2
|
80 |
+
else:
|
81 |
+
result.append(tokens[i])
|
82 |
+
i += 1
|
83 |
+
else:
|
84 |
+
result.append(tokens[i])
|
85 |
+
i += 1
|
86 |
+
return ' '.join(result)
|
87 |
+
|
88 |
+
|
89 |
+
# In[ ]:
|
90 |
+
|
91 |
+
|
92 |
+
|
93 |
+
|
replaceWords.py
ADDED
@@ -0,0 +1,156 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
'lakh' : ['lac','lach','laq','lak'],
|
138 |
+
}
|
139 |
+
|
140 |
+
words = sentence.split() # Split the sentence by spaces
|
141 |
+
|
142 |
+
# Replace words using the mapping
|
143 |
+
for i, word in enumerate(words):
|
144 |
+
for replacement, patterns in replacement_map.items():
|
145 |
+
if word in patterns:
|
146 |
+
words[i] = replacement # Replace the word if it's fully matched
|
147 |
+
|
148 |
+
# Join the processed words back into a sentence
|
149 |
+
return ' '.join(words)
|
150 |
+
|
151 |
+
|
152 |
+
# In[ ]:
|
153 |
+
|
154 |
+
|
155 |
+
|
156 |
+
|
text2int.py
ADDED
@@ -0,0 +1,199 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# coding: utf-8
|
3 |
+
|
4 |
+
# In[1]:
|
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[5]:
|
103 |
+
|
104 |
+
|
105 |
+
import nbimporter
|
106 |
+
from isNumber import is_number # Remove or replace this if unnecessary
|
107 |
+
|
108 |
+
def text_to_int(textnum, numwords={}):
|
109 |
+
# Define units, tens, and scales including "lac"
|
110 |
+
units = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight',
|
111 |
+
'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen',
|
112 |
+
'sixteen', 'seventeen', 'eighteen', 'nineteen']
|
113 |
+
tens = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']
|
114 |
+
scales = ['hundred', 'thousand', 'lakh', 'million', 'billion', 'trillion']
|
115 |
+
ordinal_words = {'first': 1, 'second': 2, 'third': 3, 'fifth': 5, 'eighth': 8, 'ninth': 9, 'twelfth': 12}
|
116 |
+
ordinal_endings = [('ieth', 'y'), ('th', '')]
|
117 |
+
|
118 |
+
if not numwords:
|
119 |
+
numwords['and'] = (1, 0) # Handle "one hundred and twenty"
|
120 |
+
|
121 |
+
# Add units, tens, and scales to numwords
|
122 |
+
for idx, word in enumerate(units):
|
123 |
+
numwords[word] = (1, idx)
|
124 |
+
for idx, word in enumerate(tens):
|
125 |
+
numwords[word] = (1, idx * 10)
|
126 |
+
for idx, word in enumerate(scales):
|
127 |
+
numwords[word] = (10 ** (5 if word == 'lakh' 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 |
+
|