-1

I need a function to convert numbers like "twenty two" to 22.

I've to write it in spanish, and I'll probably write down (or search) the formal gramatic that defines the complete way to write down numbers and make a parser, however, as the complete code is in Python, I assumed there has to be an already existing library to do the magic, atleast in english.

Is there any known library that does this in a reasonable way? I don't need very big numbers, just zero to one million or so.

khelwood
  • 55,782
  • 14
  • 81
  • 108
DGoiko
  • 346
  • 2
  • 19
  • very interesting question... there seems to be [something similar here](https://stackoverflow.com/questions/493174/is-there-a-way-to-convert-number-words-to-integers) - but that is form 10y ago so maybe needs an update? – FObersteiner Jul 19 '19 at 10:57
  • Possible duplicate of [Is there a way to convert number words to Integers?](https://stackoverflow.com/questions/493174/is-there-a-way-to-convert-number-words-to-integers) – norok2 Jul 19 '19 at 11:00

1 Answers1

0

you need something like this: ( key point is how you will prepare data )

words2num = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6, 'seven': 7, 'eight': 8, 'nine': 9, 'ten': 10, 'eleven': 11, 'twelve': 12, 'thirteen': 13, 'fourteen': 14, 'fifteen': 15, 'sixteen': 16, 'seventeen': 17, 'eighteen': 18, 'nineteen': 19}

words2num2 = {'twenty': 2, 'thirty': 3, 'forty': 4, 'fifty': 5, 'sixty': 6, 'seventy': 7, 'eighty': 8, 'ninety': 9}

def number(Number):
    if len(Number) < 2:
        print (words2num[Number[0]])
    else:
        print ("{}{}".format(words2num2[Number[0]], words2num[Number[1]]))


num = input("Please enter a number between 0 and 99: ")
number(num.split())

output:

Please enter a number between 0 and 99: fifty five
55
ncica
  • 7,015
  • 1
  • 15
  • 37
  • Thanks for your answer. Since most my numbers are low numbers, I have something like that for the first 30 numbers (which are written with one word in Spanish). The problem with that approach is that spanish has more variability than english, so the dict would grow too much, and it would need a lot of validation in order to be trustable. Aditionally, we can have twenty two thousands and two (spanish has no and there), so I would need something a bit more complicated than that: you need to multiply and to sum, depending on match. cuarenta y dos mil doscientos veintidos -> (40 + 2) x1000 + 22 – DGoiko Jul 20 '19 at 01:31
  • So in spanish, you have: thousands and millions multiply, hundreds, tens and singles sum. But then you can have tens of thousands, which can be confusing if you dont make a lookahead before suming the ten in order to detect it is actually "ten housands". When you finish to add all the legal weird cases you end up finding people writting so badly it becomes really hard to detect what they want to say if you dont have a human to read (I thought about correcting first, but it grows even harder xD). – DGoiko Jul 20 '19 at 01:38
  • The best approach in order to make it scalable seems to try compounding mathematical expressions from the words like I did above, and then interpreting them. I thought someone could've done this before :(. Not to mention if someone decides to write 3 * 10^5 with words xD, although it is simpler in spanish: 3 (tres) por (*) diez (10) elevado a (^) cinco (5), although we dont expect this kind of numbers, not even decimals. – DGoiko Jul 20 '19 at 01:50