I have a university project. I want to split and convert words to numbers like five hundred three to 503. I take string from text file but I don't how to split it.
The sentence I want to convert for test
there is five hundred three people
I want to split like this
there, is, five hundred three, people
and take in list to use dictionary to convert it to
there is 503 people
I searched so much site but I can't find anything about this. I tried .split() but it split every word and I can't use it for project.
It's python, so there is a library for this: https://github.com/careless25/text2digits
But, if you do not prefer using the library, this method (from the library) that does exactly what you want:
def text2int (textnum, numwords={}):
if not 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", "million", "billion", "trillion"]
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)
ordinal_words = {'first':1, 'second':2, 'third':3, 'fifth':5, 'eighth':8, 'ninth':9, 'twelfth':12}
ordinal_endings = [('ieth', 'y'), ('th', '')]
textnum = textnum.replace('-', ' ')
current = result = 0
curstring = ""
onnumber = False
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
else:
for ending, replacement in ordinal_endings:
if word.endswith(ending):
word = "%s%s" % (word[:-len(ending)], replacement)
if word not in numwords:
if onnumber:
curstring += repr(result + current) + " "
curstring += word + " "
result = current = 0
onnumber = False
else:
scale, increment = numwords[word]
current = current * scale + increment
if scale > 100:
result += current
current = 0
onnumber = True
if onnumber:
curstring += repr(result + current)
return curstring
You can use it like this:
>>> text2int("I want fifty five hot dogs for two hundred dollars.")
I want 55 hot dogs for 200 dollars.
You can install text2digits
package with:
pip install text2digits
Then use the package as follows to work with your example:
from text2digits import text2digits
t2d = text2digits.Text2Digits()
print t2d.convert("there is five hundred three people")
And the output is:
>>>
there is 503 people
You would have to use a list of numbers written out and then search the string for all of them and replace them.
ie something like this
strings["one", "two", "three"...] #list of numbers represented as strings
numbers[1, 2, 3...] #corrasponding numbers
def replaceNumbers(string): #function to replace numbers
for x in range(len(strings)): #loop through strings
#replace string with number
string= string[:string.find(x)] + str(numbers[x]) + string[string.find(x) + len(x):]
return string
then you then need to figure out how to deal with hundreds, thousands, ect
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.