Search code examples
pythonwindowspyttsx3

pyttsx3 string to numeric values


so I am trying to convert a spoken alphanumeric string into useable variable. example is a spoken ip address. As an example:

string(one nine two period one six eight period zero period one slash twenty four) 

turn this into

192.168.000.001/24

I understand some string formatting but this is beyond my knowledge. I figure I could turn it into a dictionary. Use a variable and compare the alphanumeric against the numeric value. I am trying to do this without the google services, as it does phone numbers and addresses, but not something like an IP address.

Any help is appreciated.


Solution

  • I TAKE NO CREDIT OF THIS ANSWER. FULL CREDITS GOES TO recursive WHO PROVIDED THIS BEAUTIFUL ANSWER IN THIS thread.

    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)
    
    current = result = 0
    for word in textnum.split():
        if word not in numwords:
          raise Exception("Illegal word: " + word)
    
        scale, increment = numwords[word]
        current = current * scale + increment
        if scale > 100:
            result += current
            current = 0
    
    return result + current
    
     print text2int("seven billion one hundred million thirty one thousand three hundred thirty seven")
     #7100031337