Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

I'm confused about number to word conversion & pyttsx.

python2.7

Best Answer dargueta, 06 December 2015 - 06:41 PM

You're still including the "-" in the condition for your while loop.

Go to the full post


This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1 SidrahAHafeez

SidrahAHafeez

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 02 December 2015 - 05:10 AM

I am writing a python program on Windows using Python 2.7. Basically, it's a program that takes a 5-digit, negative & positive, number from user & spells it out in English using num2word & then speaks it out too. I have used espeak & pyttsx both but i'm getting errors in all of them. I have tried this:

num = raw_input('Please enter your 5 digit number:')
while len(num) != 5 or (not num.isdigit()):
    print 'Not a 5 digit number'
    num = raw_input('Please enter your 5 digit number:')
    num = int(num)

def spellnum(num,join=True):
    #Lists of number words

        thousands = ['','thousand','million']
        tens = ['','ten','twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety']
        teens = ['','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen']
        units = ['','one','two','three','four','five','six','seven','eight','nine'] 
        #Empty List for number words
        words = [] 
        #zero case
        if num==0: words.append('zero')
        #how to handle negative numbers
        if num < 0: words.append('negative') 
        num = abs(num)
        #a series of steps to process the numbers and turn them into words
        numStr = '%d'%num
        numStrLen = len(numStr)
        groups = (numStrLen+2)/3
        numStr = numStr.zfill(groups*3)
        for i in range(0,groups*3,3):
            h,t,u = int(numStr[i]),int(numStr[i+1]),int(numStr[i+2])
            g = groups-(i/3+1)
            if h>=1:
                words.append(units[h])
                words.append('hundred')
            if t>1:
                words.append(tens[t])
                if u>=1: words.append(units[u])
            elif t==1:
                if u>=1: words.append(teens[u])
                else: words.append(tens[t])
            else:
                if u>=1: words.append(units[u])
            if (g>=1) and ((h+t+u)>0): words.append(thousands[g]+',')
        #final joining of parts
            if join: return ' '.join(words)
        return words

print 'Your number is: ', spellnum(num)

import pyttsx
engine = pyttsx.init()
engine.say(num)
engine.runAndWait()

The output i'm getting is:

Please enter your 5 digit number:34534
Your number is:  thirty four thousand,

Traceback (most recent call last):
  File "C:\Python27\prj.py", line 48, in <module>
    engine = pyttsx.init()
  File "C:\Python27\lib\site-packages\pyttsx\__init__.py", line 39, in init
    eng = Engine(driverName, debug)
  File "C:\Python27\lib\site-packages\pyttsx\engine.py", line 45, in __init__
    self.proxy = driver.DriverProxy(weakref.proxy(self), driverName, debug)
  File "C:\Python27\lib\site-packages\pyttsx\driver.py", line 64, in __init__
    self._module = __import__(name, globals(), locals(), [driverName])
  File "C:\Python27\lib\site-packages\pyttsx\drivers\sapi5.py", line 19, in <module>
    import win32com.client
ImportError: No module named win32com.client


First, it's not spelling the complete number that the user provided. For e.g.: I entered 20300 & it said "twenty thousand,", that's it. It didn't say the proper "Twenty thousand, three hundred".

Second, when i entered -20300, it returned "not a 5-digit number". I know that this has something to do with .isdigit method that i used, i don't know what to do about it so that negative numbers can also be considered as proper inputs.

Third, i have tried to make it say the complete spelling of numbers by using Espeak & pyttsx both but it keeps returning errors. I tried this:

import subprocess
text = int(str(num))
subprocess.call('espeak '+text, shell=True)

Output was:

Please enter your 5 digit number:35345
Your number is:  thirty five thousand,
 
Traceback (most recent call last):
  File "C:\Python27\prj.py", line 49, in <module>
    subprocess.call('espeak '+text, shell=True)
TypeError: cannot concatenate 'str' and 'int' objects

And i also tried:

import pyttsx
engine = pyttsx.init()
engine.setProperty('rate', 70)

voices = engine.getProperty('voices')
for voice in voices:
    print "Using voice:", repr(voice)
    engine.setProperty('voice', voice.id)
    engine.say(num)
engine.runAndWait()

Output was:

Please enter your 5 digit number:34342
Your number is:  thirty four thousand,

Traceback (most recent call last):
  File "C:\Python27\prj.py", line 48, in <module>
    engine = pyttsx.init()
  File "C:\Python27\lib\site-packages\pyttsx\__init__.py", line 39, in init
    eng = Engine(driverName, debug)
  File "C:\Python27\lib\site-packages\pyttsx\engine.py", line 45, in __init__
    self.proxy = driver.DriverProxy(weakref.proxy(self), driverName, debug)
  File "C:\Python27\lib\site-packages\pyttsx\driver.py", line 64, in __init__
    self._module = __import__(name, globals(), locals(), [driverName])
  File "C:\Python27\lib\site-packages\pyttsx\drivers\sapi5.py", line 19, in <module>
    import win32com.client
ImportError: No module named win32com.client

Please know that i'm new to Python. Any kind of help is appreciated.



#2 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 05 December 2015 - 07:50 PM

Looks like you're missing some libraries. Check here to see how to fix the ImportError being thrown.

 

Also: isdigit() checks to see if the string is composed entirely of "0", "1", "2", etc. "-" is not a number and therefore isdigit() will return false. You need to ignore the leading negative sign. I'll let you try and figure that out as a learning exercise.


sudo rm -rf / && echo $'Sanitize your inputs!'


#3 SidrahAHafeez

SidrahAHafeez

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 06 December 2015 - 05:22 AM

Looks like you're missing some libraries. Check here to see how to fix the ImportError being thrown.

 

Also: isdigit() checks to see if the string is composed entirely of "0", "1", "2", etc. "-" is not a number and therefore isdigit() will return false. You need to ignore the leading negative sign. I'll let you try and figure that out as a learning exercise.

I have fixed the ImportError. And i have tried this:

def check_int(num):
    if num[0] in ('-', '+'):
    	return num[1:].isdigit()
    return num.isdigit()

num = raw_input('Please enter your 5 digit number:')
while len(num) != 5 or (not num.isdigit()):
    print 'Not a 5 digit number'
    num = raw_input('Please enter your 5 digit number:')
num = int(num)

Output is:

Please enter your 5 digit number:-45645
Not a 5 digit number
Please enter your 5 digit number:

Thank you for your help.  :thumbup:



#4 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 06 December 2015 - 06:41 PM   Best Answer

You're still including the "-" in the condition for your while loop.


sudo rm -rf / && echo $'Sanitize your inputs!'