word to digit module

John Machin sjmachin at lexicon.net
Wed Dec 22 13:47:52 CET 2004


Stephen Thorne wrote:
> On Wed, 22 Dec 2004 10:27:16 +0530, Gurpreet Sachdeva
> <gurpreet.sachdeva at gmail.com> wrote:
> > Is there any module available that converts word like 'one', 'two',
> > 'three' to corresponding digits 1, 2, 3??
>
> This seemed like an interesting problem! So I decided to solve it.
>
> I started with
> http://www.python.org/pycon/dc2004/papers/42/ex1-C/ which allowed me
> to create a nice test suite.
> import num2eng
> for i in range(40000):
>     e = num2eng.num2eng(i)
>     if toNumber(e) != i:
>         print e, i, toNumber(e)
>
> once this all important test suite was created I was able to knock up
> the following script. This is tested up to 'ninty nine thousand nine
> hundred and ninty nine'. It won't do 'one hundred thousand', and
isn't
> exceptionally agile. If I were to go any higher than 'one hundred
> thousand' I would probably pull out http://dparser.sf.net/ and write
a
> parser.
>

Parser?

The following appears to work, with appropriate dict entries for
'million', 'billion', etc:
[apologies in advance if @#$% groups-beta.google stuffs the indenting]
[apologies for the dots, which attempt to the defeat the
indent-stuffing]
.def toNumber2(s):
.   items = s.replace(',', '').split()
.   numbers = [translation.get(item.strip(), -1) for item in items if
item.strip()]
.   stack = [0]
.   for num in numbers:
.      if num == -1:
.         raise ValueError("Invalid string '%s'" % (s,))
.      if num >= 100:
.         stack[-1] *= num
.         if num >= 1000:
.            stack.append(0)
.      else:
.         stack[-1] += num
.   return sum(stack)




More information about the Python-list mailing list