Delete text2int.ipynb
Browse files- text2int.ipynb +0 -232
text2int.ipynb
DELETED
|
@@ -1,232 +0,0 @@
|
|
| 1 |
-
{
|
| 2 |
-
"cells": [
|
| 3 |
-
{
|
| 4 |
-
"cell_type": "code",
|
| 5 |
-
"execution_count": null,
|
| 6 |
-
"id": "61185b34-45e0-4a78-a84b-2cedd08ad39a",
|
| 7 |
-
"metadata": {},
|
| 8 |
-
"outputs": [],
|
| 9 |
-
"source": [
|
| 10 |
-
"# # Function to convert Hindi text to numerical representation\n",
|
| 11 |
-
"# from isNumber import is_number\n",
|
| 12 |
-
"\n",
|
| 13 |
-
"# def text_to_int (textnum, numwords={}):\n",
|
| 14 |
-
"# units = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight',\n",
|
| 15 |
-
"# 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen',\n",
|
| 16 |
-
"# 'sixteen', 'seventeen', 'eighteen', 'nineteen']\n",
|
| 17 |
-
"# tens = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']\n",
|
| 18 |
-
"# scales = ['hundred', 'thousand', 'lac','million', 'billion', 'trillion']\n",
|
| 19 |
-
"# ordinal_words = {'first':1, 'second':2, 'third':3, 'fifth':5, 'eighth':8, 'ninth':9, 'twelfth':12}\n",
|
| 20 |
-
"# ordinal_endings = [('ieth', 'y'), ('th', '')]\n",
|
| 21 |
-
"\n",
|
| 22 |
-
"# if not numwords:\n",
|
| 23 |
-
"# numwords['and'] = (1, 0)\n",
|
| 24 |
-
"# for idx, word in enumerate(units): numwords[word] = (1, idx)\n",
|
| 25 |
-
"# for idx, word in enumerate(tens): numwords[word] = (1, idx * 10)\n",
|
| 26 |
-
"# for idx, word in enumerate(scales): numwords[word] = (10 ** (idx * 3 or 2), 0)\n",
|
| 27 |
-
"\n",
|
| 28 |
-
"# textnum = textnum.replace('-', ' ')\n",
|
| 29 |
-
"\n",
|
| 30 |
-
"# current = result = 0\n",
|
| 31 |
-
"# curstring = ''\n",
|
| 32 |
-
"# onnumber = False\n",
|
| 33 |
-
"# lastunit = False\n",
|
| 34 |
-
"# lastscale = False\n",
|
| 35 |
-
"\n",
|
| 36 |
-
"# def is_numword(x):\n",
|
| 37 |
-
"# if is_number(x):\n",
|
| 38 |
-
"# return True\n",
|
| 39 |
-
"# if word in numwords:\n",
|
| 40 |
-
"# return True\n",
|
| 41 |
-
"# return False\n",
|
| 42 |
-
"\n",
|
| 43 |
-
"# def from_numword(x):\n",
|
| 44 |
-
"# if is_number(x):\n",
|
| 45 |
-
"# scale = 0\n",
|
| 46 |
-
"# increment = int(x.replace(',', ''))\n",
|
| 47 |
-
"# return scale, increment\n",
|
| 48 |
-
"# return numwords[x]\n",
|
| 49 |
-
"\n",
|
| 50 |
-
"# for word in textnum.split():\n",
|
| 51 |
-
"# if word in ordinal_words:\n",
|
| 52 |
-
"# scale, increment = (1, ordinal_words[word])\n",
|
| 53 |
-
"# current = current * scale + increment\n",
|
| 54 |
-
"# if scale > 100:\n",
|
| 55 |
-
"# result += current\n",
|
| 56 |
-
"# current = 0\n",
|
| 57 |
-
"# onnumber = True\n",
|
| 58 |
-
"# lastunit = False\n",
|
| 59 |
-
"# lastscale = False\n",
|
| 60 |
-
"# else:\n",
|
| 61 |
-
"# for ending, replacement in ordinal_endings:\n",
|
| 62 |
-
"# if word.endswith(ending):\n",
|
| 63 |
-
"# word = \"%s%s\" % (word[:-len(ending)], replacement)\n",
|
| 64 |
-
"\n",
|
| 65 |
-
"# if (not is_numword(word)) or (word == 'and' and not lastscale):\n",
|
| 66 |
-
"# if onnumber:\n",
|
| 67 |
-
"# # Flush the current number we are building\n",
|
| 68 |
-
"# curstring += repr(result + current) + \" \"\n",
|
| 69 |
-
"# curstring += word + \" \"\n",
|
| 70 |
-
"# result = current = 0\n",
|
| 71 |
-
"# onnumber = False\n",
|
| 72 |
-
"# lastunit = False\n",
|
| 73 |
-
"# lastscale = False\n",
|
| 74 |
-
"# else:\n",
|
| 75 |
-
"# scale, increment = from_numword(word)\n",
|
| 76 |
-
"# onnumber = True\n",
|
| 77 |
-
"\n",
|
| 78 |
-
"# if lastunit and (word not in scales): \n",
|
| 79 |
-
"# # Assume this is part of a string of individual numbers to \n",
|
| 80 |
-
"# # be flushed, such as a zipcode \"one two three four five\" \n",
|
| 81 |
-
"# curstring += repr(result + current) \n",
|
| 82 |
-
"# result = current = 0 \n",
|
| 83 |
-
"\n",
|
| 84 |
-
"# if scale > 1: \n",
|
| 85 |
-
"# current = max(1, current) \n",
|
| 86 |
-
"\n",
|
| 87 |
-
"# current = current * scale + increment \n",
|
| 88 |
-
"# if scale > 100: \n",
|
| 89 |
-
"# result += current \n",
|
| 90 |
-
"# current = 0 \n",
|
| 91 |
-
"\n",
|
| 92 |
-
"# lastscale = False \n",
|
| 93 |
-
"# lastunit = False \n",
|
| 94 |
-
"# if word in scales: \n",
|
| 95 |
-
"# lastscale = True \n",
|
| 96 |
-
"# elif word in units: \n",
|
| 97 |
-
"# lastunit = True\n",
|
| 98 |
-
"\n",
|
| 99 |
-
"# if onnumber:\n",
|
| 100 |
-
"# curstring += repr(result + current)\n",
|
| 101 |
-
"\n",
|
| 102 |
-
"# return curstring\n"
|
| 103 |
-
]
|
| 104 |
-
},
|
| 105 |
-
{
|
| 106 |
-
"cell_type": "code",
|
| 107 |
-
"execution_count": null,
|
| 108 |
-
"id": "a87b26d7-4a0e-4fdc-b03e-1537600faf65",
|
| 109 |
-
"metadata": {},
|
| 110 |
-
"outputs": [],
|
| 111 |
-
"source": [
|
| 112 |
-
"from isNumber import is_number # Remove or replace this if unnecessary\n",
|
| 113 |
-
"\n",
|
| 114 |
-
"def text_to_int(textnum, numwords={}):\n",
|
| 115 |
-
" # Define units, tens, and scales including \"lac\"\n",
|
| 116 |
-
" units = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight',\n",
|
| 117 |
-
" 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen',\n",
|
| 118 |
-
" 'sixteen', 'seventeen', 'eighteen', 'nineteen']\n",
|
| 119 |
-
" tens = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']\n",
|
| 120 |
-
" scales = ['hundred', 'thousand', 'lac', 'million', 'billion', 'trillion'] # \"lac\" added\n",
|
| 121 |
-
" ordinal_words = {'first': 1, 'second': 2, 'third': 3, 'fifth': 5, 'eighth': 8, 'ninth': 9, 'twelfth': 12}\n",
|
| 122 |
-
" ordinal_endings = [('ieth', 'y'), ('th', '')]\n",
|
| 123 |
-
"\n",
|
| 124 |
-
" if not numwords:\n",
|
| 125 |
-
" numwords['and'] = (1, 0) # Handle \"one hundred and twenty\"\n",
|
| 126 |
-
" \n",
|
| 127 |
-
" # Add units, tens, and scales to numwords\n",
|
| 128 |
-
" for idx, word in enumerate(units):\n",
|
| 129 |
-
" numwords[word] = (1, idx)\n",
|
| 130 |
-
" for idx, word in enumerate(tens):\n",
|
| 131 |
-
" numwords[word] = (1, idx * 10)\n",
|
| 132 |
-
" \n",
|
| 133 |
-
" for idx, word in enumerate(scales):\n",
|
| 134 |
-
" numwords[word] = (10 ** (5 if word == 'lac' else idx * 3 or 2), 0) # Handle \"lac\" as 10^5\n",
|
| 135 |
-
"\n",
|
| 136 |
-
" # Remove hyphens and normalize input\n",
|
| 137 |
-
" textnum = textnum.replace('-', ' ')\n",
|
| 138 |
-
"\n",
|
| 139 |
-
" current = result = 0\n",
|
| 140 |
-
" curstring = ''\n",
|
| 141 |
-
" onnumber = False\n",
|
| 142 |
-
" lastunit = False\n",
|
| 143 |
-
" lastscale = False\n",
|
| 144 |
-
"\n",
|
| 145 |
-
" def is_numword(x):\n",
|
| 146 |
-
" return is_number(x) or x in numwords\n",
|
| 147 |
-
"\n",
|
| 148 |
-
" def from_numword(x):\n",
|
| 149 |
-
" if is_number(x):\n",
|
| 150 |
-
" return 0, int(x.replace(',', ''))\n",
|
| 151 |
-
" return numwords[x]\n",
|
| 152 |
-
"\n",
|
| 153 |
-
" for word in textnum.split():\n",
|
| 154 |
-
" if word in ordinal_words:\n",
|
| 155 |
-
" scale, increment = (1, ordinal_words[word])\n",
|
| 156 |
-
" current = current * scale + increment\n",
|
| 157 |
-
" if scale > 100:\n",
|
| 158 |
-
" result += current\n",
|
| 159 |
-
" current = 0\n",
|
| 160 |
-
" onnumber = True\n",
|
| 161 |
-
" lastunit = False\n",
|
| 162 |
-
" lastscale = False\n",
|
| 163 |
-
" else:\n",
|
| 164 |
-
" for ending, replacement in ordinal_endings:\n",
|
| 165 |
-
" if word.endswith(ending):\n",
|
| 166 |
-
" word = f\"{word[:-len(ending)]}{replacement}\"\n",
|
| 167 |
-
"\n",
|
| 168 |
-
" if not is_numword(word) or (word == 'and' and not lastscale):\n",
|
| 169 |
-
" if onnumber:\n",
|
| 170 |
-
" curstring += repr(result + current) + \" \"\n",
|
| 171 |
-
" curstring += word + \" \"\n",
|
| 172 |
-
" result = current = 0\n",
|
| 173 |
-
" onnumber = False\n",
|
| 174 |
-
" lastunit = False\n",
|
| 175 |
-
" lastscale = False\n",
|
| 176 |
-
" else:\n",
|
| 177 |
-
" scale, increment = from_numword(word)\n",
|
| 178 |
-
" onnumber = True\n",
|
| 179 |
-
"\n",
|
| 180 |
-
" if lastunit and word not in scales:\n",
|
| 181 |
-
" curstring += repr(result + current) + \" \"\n",
|
| 182 |
-
" result = current = 0\n",
|
| 183 |
-
"\n",
|
| 184 |
-
" if scale > 1:\n",
|
| 185 |
-
" current = max(1, current)\n",
|
| 186 |
-
"\n",
|
| 187 |
-
" current = current * scale + increment\n",
|
| 188 |
-
"\n",
|
| 189 |
-
" if scale >= 100:\n",
|
| 190 |
-
" result += current\n",
|
| 191 |
-
" current = 0\n",
|
| 192 |
-
"\n",
|
| 193 |
-
" lastscale = word in scales\n",
|
| 194 |
-
" lastunit = word in units\n",
|
| 195 |
-
"\n",
|
| 196 |
-
" if onnumber:\n",
|
| 197 |
-
" curstring += repr(result + current)\n",
|
| 198 |
-
"\n",
|
| 199 |
-
" return curstring.strip()"
|
| 200 |
-
]
|
| 201 |
-
},
|
| 202 |
-
{
|
| 203 |
-
"cell_type": "code",
|
| 204 |
-
"execution_count": null,
|
| 205 |
-
"id": "83997c73-e1b4-4863-b1df-d6de6153e80d",
|
| 206 |
-
"metadata": {},
|
| 207 |
-
"outputs": [],
|
| 208 |
-
"source": []
|
| 209 |
-
}
|
| 210 |
-
],
|
| 211 |
-
"metadata": {
|
| 212 |
-
"kernelspec": {
|
| 213 |
-
"display_name": "Python 3 (ipykernel)",
|
| 214 |
-
"language": "python",
|
| 215 |
-
"name": "python3"
|
| 216 |
-
},
|
| 217 |
-
"language_info": {
|
| 218 |
-
"codemirror_mode": {
|
| 219 |
-
"name": "ipython",
|
| 220 |
-
"version": 3
|
| 221 |
-
},
|
| 222 |
-
"file_extension": ".py",
|
| 223 |
-
"mimetype": "text/x-python",
|
| 224 |
-
"name": "python",
|
| 225 |
-
"nbconvert_exporter": "python",
|
| 226 |
-
"pygments_lexer": "ipython3",
|
| 227 |
-
"version": "3.11.7"
|
| 228 |
-
}
|
| 229 |
-
},
|
| 230 |
-
"nbformat": 4,
|
| 231 |
-
"nbformat_minor": 5
|
| 232 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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