require 'linguistics'
Linguistics::use(:en)
cardinal_numerals = Hash.new
for i in (0..99)
cardinal_numerals[i.en.numwords] = i
end
Please enter your equation in long form:
=>
four hundred and sixty-two thousand, seven hundred and eighty-two plus forty-two thousand, six hundred and ninety-nine
Please enter your equation in long form:
=>
four hundred and sixty-two thousand, seven hundred and eighty-two plus forty-two thousand, six hundred and ninety-nine
[DEBUG} Eval String = (4*100+62)*1000+(7*100+82)+42*1000+(6*100+99).
505481
Please enter your equation in long form:
=>
[/code]
Yay!
CARDINALS = {"zero"=>0.0, "one"=>1.0, "two"=>2.0, "three"=>3.0, "four"=>4.0, "five"=>5.0, "six"=>6.0, "seven"=>7.0, "eight"=>8.0, "nine"=>9.0,
"ten"=>10.0, "eleven"=>11.0, "twelve"=>12.0, "thirteen"=>13.0, "fourteen"=>14.0, "fifteen"=>15.0, "sixteen"=>16.0, "seventeen"=>17.0,
"eighteen"=>18.0, "nineteen"=>19.0, "twenty"=>20.0, "twenty-one"=>21.0, "twenty-two"=>22.0, "twenty-three"=>23.0, "twenty-four"=>24.0,
"twenty-five"=>25.0,"twenty-six"=>26.0, "twenty-seven"=>27.0, "twenty-eight"=>28.0, "twenty-nine"=>29.0, "thirty"=>30.0, "thirty-one"=>31.0,
"thirty-two"=>32.0, "thirty-three"=>33.0, "thirty-four"=>34.0, "thirty-five"=>35.0, "thirty-six"=>36.0, "thirty-seven"=>37.0, "thirty-eight"=>38.0,
"thirty-nine"=>39.0, "forty"=>40.0, "forty-one"=>41.0, "forty-two"=>42.0, "forty-three"=>43.0, "forty-four"=>44.0, "forty-five"=>45.0,
"forty-six"=>46.0, "forty-seven"=>47.0, "forty-eight"=>48.0, "forty-nine"=>49.0, "fifty"=>50.0, "fifty-one"=>51.0, "fifty-two"=>52.0,
"fifty-three"=>53.0, "fifty-four"=>54.0, "fifty-five"=>55.0, "fifty-six"=>56.0, "fifty-seven"=>57.0, "fifty-eight"=>58.0, "fifty-nine"=>59.0,
"sixty"=>60.0, "sixty-one"=>61.0, "sixty-two"=>62.0, "sixty-three"=>63.0, "sixty-four"=>64.0, "sixty-five"=>65.0, "sixty-six"=>66.0,
"sixty-seven"=>67.0, "sixty-eight"=>68.0, "sixty-nine"=>69.0, "seventy"=>70.0, "seventy-one"=>71.0, "seventy-two"=>72.0, "seventy-three"=>73.0,
"seventy-four"=>74.0, "seventy-five"=>75.0, "seventy-six"=>76.0, "seventy-seven"=>77.0, "seventy-eight"=>78.0, "seventy-nine"=>79.0,
"eighty"=>80.0, "eighty-one"=>81.0, "eighty-two"=>82.0, "eighty-three"=>83.0, "eighty-four"=>84.0, "eighty-five"=>85.0, "eighty-six"=>86.0,
"eighty-seven"=>87.0, "eighty-eight"=>88.0, "eighty-nine"=>89.0, "ninety"=>90.0, "ninety-one"=>91.0, "ninety-two"=>92.0, "ninety-three"=>93.0,
"ninety-four"=>94.0, "ninety-five"=>95.0, "ninety-six"=>96.0, "ninety-seven"=>97.0, "ninety-eight"=>98.0, "ninety-nine"=>99.0,}
PLACES = {"hundred"=> 100,
"thousand"=> 1000,
"million"=> 1000000,
"billion"=> 1000000000,
"trillion"=> 1000000000000,}
KEYWORDS = {"plus" => '+',
"minus" => '-',
"times" => '*',
"divided" => '/',
"multiplied" => '*',}
# Hacky singleton
class Parser
@@p, @@c, @@k = PLACES, CARDINALS, KEYWORDS
# Returns a string containing the "equation" to be evaluated.
def self.parse(s)
eq, lw, m, eqs = [],'','',''
s.gsub!(',','')
arr = s.split(' ')
arr.each do |w|
if (m = @@c[w])
eq << '+' if (@@p[lw]);eq << m
if lw.eql? 'and'
eq.insert(eq.size-4,'(')
eq.insert(eq.size-1,'+')
eq << ')'
end
elsif (m = @@p[w]) then (eq << '*' && eq << m)
elsif (m = @@k[w])
eq << ')'
eq << m
eq << '('
end
lw = w
end
eq.insert(0,'(')
eq << ')'
eq.each {|w| eqs << w.to_s}
return eqs
end
end
I've been able to implement most of it myself but I'm having problems with numbers like:
and would rather not fuss with it anymore.