Skip to content

Instantly share code, notes, and snippets.

@etrepum
Created January 5, 2016 05:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save etrepum/914da530c69e01340ecd to your computer and use it in GitHub Desktop.
Save etrepum/914da530c69e01340ecd to your computer and use it in GitHub Desktop.
ruby '2.2.4'
source 'https://rubygems.org'
gem 'numbers_in_words'
gem 'money'
require 'money'
require 'numbers_in_words'
INPUTS = [
"Your food stamp balance is six dollars and twenty five cents. Your cash account balance is eleven dollars and sixty nine cents. As a reminder. By saving the receipt from your last purchase and or your last cash purchase or cashback Prinz action. You will always have your.",
"Your snap balance is four hundred twenty six dollars. Your cash balance is zero dollars. As a reminder by saving the receipt from your last purchase you will know your current balance. Remember you can also access your account information online at.",
"One moment please. OK. I've pulled up your account information. Your food stamp balance is seven hundred sixty six dollars and thirty seven cents. You are eligible to enroll in a free service called my own.",
"One moment please. OK. I've pulled up your account information. Your food stamp balance is seven hundred sixty six dollars and thirty seven cents. You are eligible to enroll in a free service called my alerts.",
"Balance is one hundred seventy one dollars and sixty eight cents. Your cash account balance is zero dollars and ninety cents. As a reminder. By saving the receipt from your last purchase and or your last cash purchase or cashback transaction. You will always have you.",
"Balance is four hundred one dollars and twenty three cents. Your cash account balance is two dollars and fifty one cents. As a reminder. By saving the receipt from your last purchase and or your last cash purchase or cashback transaction. You will always have your current.",
"Snap balance is three hundred fourteen dollars. Your cash balance is zero dollars. As a reminder by saving the receipt from your last purchase you'll know your current balance. Remember you can also access your account information online at W W.",
# the euro edge case
"Balance is the euro. Dollars. Your cash account balance is five hundred forty one dollars as a reminder. By saving the receipt from your last purchase and or your last cash purchase or cashback transaction. You will always have your current balance. Some A.T.M.'s will also print your balance on a cash with the."
]
EXPECTED_OUTPUTS = [
"Your food stamp balance is $6.25. Your cash account balance is $11.69. As a reminder. By saving the receipt from your last purchase and or your last cash purchase or cashback Prinz action. You will always have your.",
"Your snap balance is $426.00. Your cash balance is $0.00. As a reminder by saving the receipt from your last purchase you will know your current balance. Remember you can also access your account information online at.",
"One moment please. OK. I've pulled up your account information. Your food stamp balance is $766.37. You are eligible to enroll in a free service called my own.",
"One moment please. OK. I've pulled up your account information. Your food stamp balance is $766.37. You are eligible to enroll in a free service called my alerts.",
"Balance is $171.68. Your cash account balance is $0.90. As a reminder. By saving the receipt from your last purchase and or your last cash purchase or cashback transaction. You will always have you.",
"Balance is $401.23. Your cash account balance is $2.51. As a reminder. By saving the receipt from your last purchase and or your last cash purchase or cashback transaction. You will always have your current.",
"Snap balance is $314.00. Your cash balance is $0.00. As a reminder by saving the receipt from your last purchase you'll know your current balance. Remember you can also access your account information online at W W.",
# the euro edge case
"Balance is $0.00. Your cash account balance is $541.00 as a reminder. By saving the receipt from your last purchase and or your last cash purchase or cashback transaction. You will always have your current balance. Some A.T.M.'s will also print your balance on a cash with the."
]
def fix_amounts(s)
s.gsub(/(balance is) ([^.]*?|the euro\.) dollars(?: and ([^.]*?) cents)?/i) do
m = Regexp.last_match
before = m[1]
dollars = m[2]
cents = m[3]
value = Money.new(NumbersInWords.in_numbers(dollars) * 100 + NumbersInWords.in_numbers(cents))
"#{before} #{value.format}"
end
end
failures = 0
INPUTS.zip(EXPECTED_OUTPUTS).each do |input, expected|
actual = fix_amounts(input)
if actual != expected
failures += 1
puts "%% FAILURE"
puts "INPUT : #{input}"
puts "EXPECTED: #{expected}"
puts "ACTUAL : #{actual}"
puts "\n\n"
end
end
puts "failures: #{failures}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment