473,396 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
3 2056
zmbd
5,501 Expert Mod 4TB
Did you ever get this sorted?
Jan 19 '16 #2
Yes, i worked out the problem & got my project to work.
Jan 20 '16 #3
zmbd
5,501 Expert Mod 4TB
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

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

Similar topics

1
by: antonovaCCCS | last post by:
In our old VB 6 application we utilize a mail merge feature of Word 97 with MS Access. A table in Access is populated from VB and a Word template is wired to that table. So all we do in the code is...
3
by: Ken Varn | last post by:
I have a managed C++ function that accepts an IntPtr argument. I am passing in a variable of type HANDLE for the IntPtr argument. The compiler does not issue any warnings for this, so I am...
5
by: Pavils Jurjans | last post by:
Hello, I am somewhat lost in the implicit/expicit possible/impossible type casting in C#... I need to write a class, which among other things, must have wat to read a numeric value type, and...
1
by: msinghindia | last post by:
I am creating an application which need to convert document files into XML. Then read the xml files for specific words in specific format. I am using Microsoft.Office.Interop for converting the...
0
by: XHengDF | last post by:
I am a new gay to use the library boost, so i confused by some details! now, anybody could help me to explain the difference between boost::bind and boost::lambda::bind when i use the library. I...
2
by: coder_lol | last post by:
MS VS 7.0 happily resolves by SmartPointer and Inheritance, but I got to use another target compiler and it does not accept user conversion for templates. Can I forced a recast somehow? I have...
1
by: vidhyapriya | last post by:
hi all, is there any DLL file to convert pdf to word using asp.net or vb.net
2
by: doll | last post by:
hi i want to convert a number in the numerical form to words..please help me.. i want to know the design(form design) also..so please provide me the code and the design in vb.net thanking you
3
by: 1230987za | last post by:
Hi, I have the following 2 C files: f.c: #include <stdio.h> void banana_peel(char c, short s, float f){ printf("char c = %c short s = %d float f = %f \n", c, s, f); }
2
by: KYG | last post by:
Hi , I'm trying to design and build a web app (my first C++ app) using the WT C++ web toolkit. Been looking for a way to read an MS Word doc into a 'stream?' and manipulate (search, copy/ delete...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.