Here's a wordtovalue converter, just as a sample. This is the kind of
problem that is most suitable for pyparsing. I'll include it in the
examples directory for the next release.
 Paul
# wordsToNum.py
# Copyright 2006, Paul McGuire
#
# Sample parser grammar to read a number given in words, and return the
numeric value.
#
from pyparsing import *
def makeNumericParseAction(val):
return lambda s,l,t: val
def makeLit(s,val):
return CaselessLiteral(s).setName(s).setParseAction(
makeNumericParseAction(val) )
unitDefinitions = [
("zero", 0),
("one", 1),
("two", 2),
("three", 3),
("four", 4),
("five", 5),
("six", 6),
("seven", 7),
("eight", 8),
("nine", 9),
("ten", 10),
("eleven", 11),
("twelve", 12),
("thirteen", 13),
("fourteen", 14),
("fifteen", 15),
("sixteen", 16),
("seventeen", 17),
("eighteen", 18),
("nineteen", 19),
]
units = Or( [ makeLit(s,v) for s,v in unitDefinitions ] )
tensDefinitions = [
("ten", 10),
("twenty", 20),
("thirty", 30),
("forty", 40),
("fifty", 50),
("sixty", 60),
("seventy", 70),
("eighty", 80),
("ninety", 90),
]
tens = Or( [ makeLit(s,v) for s,v in tensDefinitions ] )
hundreds = makeLit("hundred", 100)
majorDefinitions = [
("thousand", int(1e3)),
("million", int(1e6)),
("billion", int(1e9)),
("trillion", int(1e12)),
("quadrillion", int(1e15)),
("quintillion", int(1e18)),
]
mag = Or( [ makeLit(s,v) for s,v in majorDefinitions ] )
def wordprod(s,l,t):
ret = 1
for v in t:
ret *= v
return ret
def wordsum(s,l,t):
return sum(t)
and_ = Suppress(makeLit("and",0))
numPart = (((( units + Optional(hundreds) ).setParseAction(wordprod) +
Optional(and_) + Optional(tens)).setParseAction(wordsum)
^ tens )
+ Optional(units) ).setParseAction(wordsum)
numWords = OneOrMore( (numPart + Optional(mag)).setParseAction(wordprod) +
Optional(and_) ).setParseAction(wordsum)
numWords.ignore("")
print numWords.parseString("one hundred twenty")
print numWords.parseString("one hundred and twenty")
print numWords.parseString("one hundred and three")
print numWords.parseString("one hundred twentythree")
print numWords.parseString("one hundred and twenty three")
print numWords.parseString("one hundred twenty three million")
print numWords.parseString("one hundred and twenty three million")
print numWords.parseString("one hundred twenty three million and three")
print numWords.parseString("fifteen hundred and sixty five")
print numWords.parseString("zero")
