By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,641 Members | 2,101 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,641 IT Pros & Developers. It's quick & easy.

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

P: 2
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:
Expand|Select|Wrap|Line Numbers
  1. num = raw_input('Please enter your 5 digit number:')
  2. while len(num) != 5 or (not num.isdigit()):
  3.     print 'Not a 5 digit number'
  4.     num = raw_input('Please enter your 5 digit number:')
  5. num = int(num)
  6.  
  7. def spellnum(num,join=True):
  8.     #Lists of number words
  9.  
  10.         thousands = ['','thousand','million']
  11.         tens = ['','ten','twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety']
  12.         teens = ['','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen']
  13.         units = ['','one','two','three','four','five','six','seven','eight','nine'] 
  14.         #Empty List for number words
  15.         words = [] 
  16.         #zero case
  17.         if num==0: words.append('zero')
  18.         #how to handle negative numbers
  19.         if num < 0: words.append('negative') 
  20.         num = abs(num)
  21.         #a series of steps to process the numbers and turn them into words
  22.         numStr = '%d'%num
  23.         numStrLen = len(numStr)
  24.         groups = (numStrLen+2)/3
  25.         numStr = numStr.zfill(groups*3)
  26.         for i in range(0,groups*3,3):
  27.             h,t,u = int(numStr[i]),int(numStr[i+1]),int(numStr[i+2])
  28.             g = groups-(i/3+1)
  29.             if h>=1:
  30.                 words.append(units[h])
  31.                 words.append('hundred')
  32.             if t>1:
  33.                 words.append(tens[t])
  34.                 if u>=1: words.append(units[u])
  35.             elif t==1:
  36.                 if u>=1: words.append(teens[u])
  37.                 else: words.append(tens[t])
  38.             else:
  39.                 if u>=1: words.append(units[u])
  40.             if (g>=1) and ((h+t+u)>0): words.append(thousands[g]+',')
  41.         #final joining of parts
  42.             if join: return ' '.join(words)
  43.         return words
  44.  
  45. print 'Your number is: ', spellnum(num)
  46.  
  47. import pyttsx
  48. engine = pyttsx.init()
  49. engine.setProperty('rate', 70)
  50.  
  51. voices = engine.getProperty('voices')
  52. for voice in voices:
  53.     print "Using voice:", repr(voice)
  54.     engine.setProperty('voice', voice.id)
  55.     engine.say(spellnum(num))
  56. engine.runAndWait()
  57.  
Output was:

Expand|Select|Wrap|Line Numbers
  1. Please enter your 5 digit number:34342
  2. Your number is:  thirty four thousand,
  3.  
  4. Traceback (most recent call last):
  5.   File "C:\Python27\prj.py", line 48, in <module>
  6.     engine = pyttsx.init()
  7.   File "C:\Python27\lib\site-packages\pyttsx\__init__.py", line 39, in init
  8.     eng = Engine(driverName, debug)
  9.   File "C:\Python27\lib\site-packages\pyttsx\engine.py", line 45, in __init__
  10.     self.proxy = driver.DriverProxy(weakref.proxy(self), driverName, debug)
  11.   File "C:\Python27\lib\site-packages\pyttsx\driver.py", line 64, in __init__
  12.     self._module = __import__(name, globals(), locals(), [driverName])
  13.   File "C:\Python27\lib\site-packages\pyttsx\drivers\sapi5.py", line 19, in <module>
  14.     import win32com.client
  15. 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:

Expand|Select|Wrap|Line Numbers
  1. import subprocess
  2. text = int(str(spellnum(num)))
  3. subprocess.call('espeak '+text, shell=True)
  4.  
Output was:

Expand|Select|Wrap|Line Numbers
  1. Please enter your 5 digit number:35345
  2. Your number is:  thirty five thousand,
  3.  
  4.  Traceback (most recent call last):
  5.      File "C:\Python27\prj.py", line 48, in <module>
  6.      text = int(str(spellnum(num)))
  7.     ValueError: invalid literal for int() with base 10: ' thirty five thousand,'
  8.  
Please know that i'm new to Python. Any kind of help is appreciated.
Dec 2 '15 #1
Share this Question
Share on Google+
3 Replies


zmbd
Expert Mod 5K+
P: 5,397
Did you ever get this sorted?
Jan 19 '16 #2

P: 2
Yes, i worked out the problem & got my project to work.
Jan 20 '16 #3

zmbd
Expert Mod 5K+
P: 5,397
That's great,
If you have a moment, would you mind posting how you solved your quandary? Doing so might help the next poor lost soul... :)

thnx
-z
Jan 21 '16 #4

Post your reply

Sign in to post your reply or Sign up for a free account.