File size: 10,492 Bytes
3b58a97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/env python
# coding: utf-8

# In[1]:


# # # Function to convert Hindi text to numerical representation
# from isNumber import is_number

# def text_to_int (textnum, numwords={}):
#     units = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight',
#             'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen',
#             'sixteen', 'seventeen', 'eighteen', 'nineteen']
#     tens = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']
#     scales = ['hundred', 'thousand', 'lac','million', 'billion', 'trillion']
#     ordinal_words = {'first':1, 'second':2, 'third':3, 'fifth':5, 'eighth':8, 'ninth':9, 'twelfth':12}
#     ordinal_endings = [('ieth', 'y'), ('th', '')]

#     if not numwords:
#         numwords['and'] = (1, 0)
#         for idx, word in enumerate(units): numwords[word] = (1, idx)
#         for idx, word in enumerate(tens): numwords[word] = (1, idx * 10)
#         for idx, word in enumerate(scales): numwords[word] = (10 ** (idx * 3 or 2), 0)

#     textnum = textnum.replace('-', ' ')

#     current = result = 0
#     curstring = ''
#     onnumber = False
#     lastunit = False
#     lastscale = False

#     def is_numword(x):
#         if is_number(x):
#             return True
#         if word in numwords:
#             return True
#         return False

#     def from_numword(x):
#         if is_number(x):
#             scale = 0
#             increment = int(x.replace(',', ''))
#             return scale, increment
#         return numwords[x]

#     for word in textnum.split():
#         if word in ordinal_words:
#             scale, increment = (1, ordinal_words[word])
#             current = current * scale + increment
#             if scale > 100:
#                 result += current
#                 current = 0
#             onnumber = True
#             lastunit = False
#             lastscale = False
#         else:
#             for ending, replacement in ordinal_endings:
#                 if word.endswith(ending):
#                     word = "%s%s" % (word[:-len(ending)], replacement)

#             if (not is_numword(word)) or (word == 'and' and not lastscale):
#                 if onnumber:
#                     # Flush the current number we are building
#                     curstring += repr(result + current) + " "
#                 curstring += word + " "
#                 result = current = 0
#                 onnumber = False
#                 lastunit = False
#                 lastscale = False
#             else:
#                 scale, increment = from_numword(word)
#                 onnumber = True

#                 if lastunit and (word not in scales):                                                                                                                                                                                                                                         
#                     # Assume this is part of a string of individual numbers to                                                                                                                                                                                                                
#                     # be flushed, such as a zipcode "one two three four five"                                                                                                                                                                                                                 
#                     curstring += repr(result + current)                                                                                                                                                                                                                                       
#                     result = current = 0                                                                                                                                                                                                                                                      

#                 if scale > 1:                                                                                                                                                                                                                                                                 
#                     current = max(1, current)                                                                                                                                                                                                                                                 

#                 current = current * scale + increment                                                                                                                                                                                                                                         
#                 if scale > 100:                                                                                                                                                                                                                                                               
#                     result += current                                                                                                                                                                                                                                                         
#                     current = 0                                                                                                                                                                                                                                                               

#                 lastscale = False                                                                                                                                                                                                              
#                 lastunit = False                                                                                                                                                
#                 if word in scales:                                                                                                                                                                                                             
#                     lastscale = True                                                                                                                                                                                                         
#                 elif word in units:                                                                                                                                                                                                             
#                     lastunit = True

#     if onnumber:
#         curstring += repr(result + current)

#     return curstring


# In[5]:


import nbimporter
from isNumber import is_number  # Remove or replace this if unnecessary

def text_to_int(textnum, numwords={}):
    # Define units, tens, and scales including "lac"
    units = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight',
            'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen',
            'sixteen', 'seventeen', 'eighteen', 'nineteen']
    tens = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']
    scales = ['hundred', 'thousand', 'lakh', 'million', 'billion', 'trillion'] 
    ordinal_words = {'first': 1, 'second': 2, 'third': 3, 'fifth': 5, 'eighth': 8, 'ninth': 9, 'twelfth': 12}
    ordinal_endings = [('ieth', 'y'), ('th', '')]

    if not numwords:
        numwords['and'] = (1, 0)  # Handle "one hundred and twenty"
        
        # Add units, tens, and scales to numwords
        for idx, word in enumerate(units):
            numwords[word] = (1, idx)
        for idx, word in enumerate(tens):
            numwords[word] = (1, idx * 10)
        for idx, word in enumerate(scales):
            numwords[word] = (10 ** (5 if word == 'lakh' else idx * 3 or 2), 0)  # Handle "lac" as 10^5

    # Remove hyphens and normalize input
    textnum = textnum.replace('-', ' ')

    current = result = 0
    curstring = ''
    onnumber = False
    lastunit = False
    lastscale = False

    def is_numword(x):
        return is_number(x) or x in numwords

    def from_numword(x):
        if is_number(x):
            return 0, int(x.replace(',', ''))
        return numwords[x]

    for word in textnum.split():
        if word in ordinal_words:
            scale, increment = (1, ordinal_words[word])
            current = current * scale + increment
            if scale > 100:
                result += current
                current = 0
            onnumber = True
            lastunit = False
            lastscale = False
        else:
            for ending, replacement in ordinal_endings:
                if word.endswith(ending):
                    word = f"{word[:-len(ending)]}{replacement}"

            if not is_numword(word) or (word == 'and' and not lastscale):
                if onnumber:
                    curstring += repr(result + current) + " "
                curstring += word + " "
                result = current = 0
                onnumber = False
                lastunit = False
                lastscale = False
            else:
                scale, increment = from_numword(word)
                onnumber = True

                if lastunit and word not in scales:
                    curstring += repr(result + current) + " "
                    result = current = 0

                if scale > 1:
                    current = max(1, current)

                current = current * scale + increment

                if scale >= 100:
                    result += current
                    current = 0

                lastscale = word in scales
                lastunit = word in units

    if onnumber:
        curstring += repr(result + current)

    return curstring.strip()


# In[ ]: