File size: 12,816 Bytes
f63b5a2 |
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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "61185b34-45e0-4a78-a84b-2cedd08ad39a",
"metadata": {},
"outputs": [],
"source": [
"# # Function to convert Hindi text to numerical representation\n",
"# from isNumber import is_number\n",
"\n",
"# def text_to_int (textnum, numwords={}):\n",
"# units = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight',\n",
"# 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen',\n",
"# 'sixteen', 'seventeen', 'eighteen', 'nineteen']\n",
"# tens = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']\n",
"# scales = ['hundred', 'thousand', 'lac','million', 'billion', 'trillion']\n",
"# ordinal_words = {'first':1, 'second':2, 'third':3, 'fifth':5, 'eighth':8, 'ninth':9, 'twelfth':12}\n",
"# ordinal_endings = [('ieth', 'y'), ('th', '')]\n",
"\n",
"# if not numwords:\n",
"# numwords['and'] = (1, 0)\n",
"# for idx, word in enumerate(units): numwords[word] = (1, idx)\n",
"# for idx, word in enumerate(tens): numwords[word] = (1, idx * 10)\n",
"# for idx, word in enumerate(scales): numwords[word] = (10 ** (idx * 3 or 2), 0)\n",
"\n",
"# textnum = textnum.replace('-', ' ')\n",
"\n",
"# current = result = 0\n",
"# curstring = ''\n",
"# onnumber = False\n",
"# lastunit = False\n",
"# lastscale = False\n",
"\n",
"# def is_numword(x):\n",
"# if is_number(x):\n",
"# return True\n",
"# if word in numwords:\n",
"# return True\n",
"# return False\n",
"\n",
"# def from_numword(x):\n",
"# if is_number(x):\n",
"# scale = 0\n",
"# increment = int(x.replace(',', ''))\n",
"# return scale, increment\n",
"# return numwords[x]\n",
"\n",
"# for word in textnum.split():\n",
"# if word in ordinal_words:\n",
"# scale, increment = (1, ordinal_words[word])\n",
"# current = current * scale + increment\n",
"# if scale > 100:\n",
"# result += current\n",
"# current = 0\n",
"# onnumber = True\n",
"# lastunit = False\n",
"# lastscale = False\n",
"# else:\n",
"# for ending, replacement in ordinal_endings:\n",
"# if word.endswith(ending):\n",
"# word = \"%s%s\" % (word[:-len(ending)], replacement)\n",
"\n",
"# if (not is_numword(word)) or (word == 'and' and not lastscale):\n",
"# if onnumber:\n",
"# # Flush the current number we are building\n",
"# curstring += repr(result + current) + \" \"\n",
"# curstring += word + \" \"\n",
"# result = current = 0\n",
"# onnumber = False\n",
"# lastunit = False\n",
"# lastscale = False\n",
"# else:\n",
"# scale, increment = from_numword(word)\n",
"# onnumber = True\n",
"\n",
"# if lastunit and (word not in scales): \n",
"# # Assume this is part of a string of individual numbers to \n",
"# # be flushed, such as a zipcode \"one two three four five\" \n",
"# curstring += repr(result + current) \n",
"# result = current = 0 \n",
"\n",
"# if scale > 1: \n",
"# current = max(1, current) \n",
"\n",
"# current = current * scale + increment \n",
"# if scale > 100: \n",
"# result += current \n",
"# current = 0 \n",
"\n",
"# lastscale = False \n",
"# lastunit = False \n",
"# if word in scales: \n",
"# lastscale = True \n",
"# elif word in units: \n",
"# lastunit = True\n",
"\n",
"# if onnumber:\n",
"# curstring += repr(result + current)\n",
"\n",
"# return curstring\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a87b26d7-4a0e-4fdc-b03e-1537600faf65",
"metadata": {},
"outputs": [],
"source": [
"from isNumber import is_number # Remove or replace this if unnecessary\n",
"\n",
"def text_to_int(textnum, numwords={}):\n",
" # Define units, tens, and scales including \"lac\"\n",
" units = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight',\n",
" 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen',\n",
" 'sixteen', 'seventeen', 'eighteen', 'nineteen']\n",
" tens = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']\n",
" scales = ['hundred', 'thousand', 'lac', 'million', 'billion', 'trillion'] # \"lac\" added\n",
" ordinal_words = {'first': 1, 'second': 2, 'third': 3, 'fifth': 5, 'eighth': 8, 'ninth': 9, 'twelfth': 12}\n",
" ordinal_endings = [('ieth', 'y'), ('th', '')]\n",
"\n",
" if not numwords:\n",
" numwords['and'] = (1, 0) # Handle \"one hundred and twenty\"\n",
" \n",
" # Add units, tens, and scales to numwords\n",
" for idx, word in enumerate(units):\n",
" numwords[word] = (1, idx)\n",
" for idx, word in enumerate(tens):\n",
" numwords[word] = (1, idx * 10)\n",
" \n",
" for idx, word in enumerate(scales):\n",
" numwords[word] = (10 ** (5 if word == 'lac' else idx * 3 or 2), 0) # Handle \"lac\" as 10^5\n",
"\n",
" # Remove hyphens and normalize input\n",
" textnum = textnum.replace('-', ' ')\n",
"\n",
" current = result = 0\n",
" curstring = ''\n",
" onnumber = False\n",
" lastunit = False\n",
" lastscale = False\n",
"\n",
" def is_numword(x):\n",
" return is_number(x) or x in numwords\n",
"\n",
" def from_numword(x):\n",
" if is_number(x):\n",
" return 0, int(x.replace(',', ''))\n",
" return numwords[x]\n",
"\n",
" for word in textnum.split():\n",
" if word in ordinal_words:\n",
" scale, increment = (1, ordinal_words[word])\n",
" current = current * scale + increment\n",
" if scale > 100:\n",
" result += current\n",
" current = 0\n",
" onnumber = True\n",
" lastunit = False\n",
" lastscale = False\n",
" else:\n",
" for ending, replacement in ordinal_endings:\n",
" if word.endswith(ending):\n",
" word = f\"{word[:-len(ending)]}{replacement}\"\n",
"\n",
" if not is_numword(word) or (word == 'and' and not lastscale):\n",
" if onnumber:\n",
" curstring += repr(result + current) + \" \"\n",
" curstring += word + \" \"\n",
" result = current = 0\n",
" onnumber = False\n",
" lastunit = False\n",
" lastscale = False\n",
" else:\n",
" scale, increment = from_numword(word)\n",
" onnumber = True\n",
"\n",
" if lastunit and word not in scales:\n",
" curstring += repr(result + current) + \" \"\n",
" result = current = 0\n",
"\n",
" if scale > 1:\n",
" current = max(1, current)\n",
"\n",
" current = current * scale + increment\n",
"\n",
" if scale >= 100:\n",
" result += current\n",
" current = 0\n",
"\n",
" lastscale = word in scales\n",
" lastunit = word in units\n",
"\n",
" if onnumber:\n",
" curstring += repr(result + current)\n",
"\n",
" return curstring.strip()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "83997c73-e1b4-4863-b1df-d6de6153e80d",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|