Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## number_magick.rb [ruby]
- #!/usr/bin/env ruby
- # Author:: Jan Wikholm [jw@jw.fi]
- # License:: MIT
- require 'logger'
- module JW
- # not in any way associated with ImageMagick
- class NumberMagick
- @@logger = Logger.new('numbers.log')
- NUMBERS = {
- '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, 'fifteen' => 15, 'eighteen' => 18,
- 'twenty' => 20, 'thirty' => 30, 'forty' => 40, 'fifty' => 50,
- 'eighty' => 80
- }
- MULTIPLIERS = {
- 'hundred' => 100, 'thousand' => 1000,
- 'million' => 1_000_000, 'billion' => 1_000_000_000
- }
- class << self
- def convert(number_as_string)
- @@logger.info "*** Original string: #{number_as_string}"
- parts = number_as_string.gsub(/-/, ' ').gsub(/\ban?d?\b/, '').split
- @@logger.info "Number cleaned was: #{parts.join(' ')}"
- total = 0
- part_total = 0
- parts.each do |part|
- @@logger.info "---\nStarting with\t\t#{part}"
- if MULTIPLIERS.include? part
- if part_total != 0
- total += part_total * MULTIPLIERS[part]
- part_total = 0
- else
- total += MULTIPLIERS[part]
- end
- elsif NUMBERS.include? part
- part_total += NUMBERS[part]
- elsif part =~ /^[0-9]+$/
- part_total += part.to_i
- else
- case part
- when /teen$/
- number_part = part.gsub(/teen$/, '')
- raise "Unknown number #{part}" unless NUMBERS.include? number_part
- part_total += NUMBERS[number_part] + 10 # for the -teen
- when /ty$/
- number_part = part.gsub(/ty$/, '')
- raise "Unknown number #{part}" unless NUMBERS.include? number_part
- part_total += NUMBERS[number_part] * 10 # for the -ty (like sixty = 6*10)
- end
- end
- @@logger.info "---\nEnding with\t\t#{part}"
- end
- total += part_total
- end
- end
- end
- end
- ## number_magic_test.rb [ruby]
- #!/usr/bin/env ruby
- # Author:: Jan Wikholm [jw@jw.fi]
- # License:: MIT
- require 'test/unit'
- require 'number_magick.rb'
- class ParseNumbersTest < Test::Unit::TestCase
- def test_parse
- strings = {1 => 'one',
- 5 => 'five',
- 10 => 'ten',
- 11 => 'eleven',
- 12 => 'twelve',
- 13 => 'thirteen',
- 14 => 'fourteen',
- 15 => 'fifteen',
- 16 => 'sixteen',
- 17 => 'seventeen',
- 18 => 'eighteen',
- 19 => 'nineteen',
- 20 => 'twenty',
- 27 => 'twenty seven',
- 31 => 'thirty-one',
- 59 => 'fifty nine',
- 100 => 'a hundred',
- 100 => 'one hundred',
- 150 => 'one hundred and fifty',
- #150 => 'one fifty',
- 200 => 'two-hundred',
- 500 => '5 hundred',
- 999 => 'nine hundred and ninety nine',
- 1_000 => 'one thousand',
- 1_200 => 'twelve hundred',
- 1_200 => 'one thousand two hundred',
- 17_000 => 'seventeen thousand',
- 21_473 => 'twenty-one-thousand-four-hundred-and-seventy-three',
- 74_002 => 'seventy four thousand and two',
- 99_999 => 'ninety nine thousand nine hundred ninety nine',
- 100_000 => '100 thousand',
- 250_000 => 'two hundred and fifty thousand',
- 1_000_000 => 'one million',
- #1_250_007 => 'one million two hundred fifty thousand and seven',
- 1_000_000_000 => 'one billion',
- 1_000_000_001 => 'one billion and one'}
- strings.keys.sort.each do |key|
- assert_equal key, JW::NumberMagick.convert(strings[key])
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement