fixed the code. It was an ordering problem. LPEG was matching "six" and then ending at the "ty" in sixty. Fixed the order. Editted code above works with
print(WordtoNum("two trillion four hundred thirty two billion eight hundred seventy six million three hundred forty five thousand nine hundred twenty three")) > 2432876345923
p.s. did I mention Pub Crawl? ;)
code copied since we're on a new page:
function WordtoNum (str)
local P, V, Cf, C, Cg, Cc = lpeg.P, lpeg.V, lpeg.Cf, lpeg.C, lpeg.Cg, lpeg.Cc
lpeg.locale(lpeg)
local numvalues = { 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, twenty = 20,
thirty = 30, forty = 40, fifty = 50, sixty = 60, seventy = 70, eighty = 80,
ninety = 90, hundred = 10^2,
thousand = 10^3,
million = 10^6,
billion = 10^9,
trillion = 10^12,
quadrillion = 10^15,
blah blah
}
local ndigit = P"twenty" + P"thirty" + P"forty" + P"fifty" + P"sixty" + P"seventy" + P"eighty" + P"ninety" +
P'hundred' +
P"ten" + P"eleven" + P"twelve" + P"thirteen" + P"fourteen" + P"fifteen" +
P"sixteen" + P"seventeen" + P"eighteen" + P"nineteen" +
P"one" + P"two" + P"three" + P"four" + P"five" + P"six" + P"seven" + P"eight" + P"nine"
local powers = P'vigintillion' + P'novemdecillion' + P'octodecillion' + P'septendecillion' +
P'sexdecillion' + P'quindecillion' + P'quattuordecillion' + P'tredecillion' +
P'duodecillion' + P'undecillion' + P'decillion' + P'nonillion' + P'octillion' +
P'septillion' + P'sextillion' + P'quintillion' + P'quadrillion' + P'trillion' +
P'billion' + P'million' + P'thousand'
local function SumVals (seed, ...)
local cypher = {...}
local tmp = 0
for x = 1, #cypher do
if numvalues[cypher[x]] == 100 then
if tmp == 0 then
tmp = 100
else
tmp = tmp * 100
end
elseif powers:match(cypher[x] or "") then
tmp = tmp * numvalues[cypher[x]]
else
tmp = tmp + numvalues[cypher[x]]
end
end
return seed + tmp
end
local cypher = Cg( (C(ndigit) * lpeg.space^1)^1 * ((C(powers) * lpeg.space^1) + P(0)) )
local number = Cf(Cc(0) * cypher^1, SumVals)
return number:match(str)
end
