From cd048505301a63d8fddee5bdf2e723a750ab3ca4 Mon Sep 17 00:00:00 2001 From: Dimid Duchovny Date: Thu, 5 Mar 2015 20:05:12 +0200 Subject: [PATCH 01/27] num_parse: support years notation (nineteen twenty) Signed-off-by: Dimid Duchovny --- lib/numbers_in_words/number_parser.rb | 38 +++++++++++++++++++++++++-- spec/words_in_numbers_spec.rb | 10 +++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/lib/numbers_in_words/number_parser.rb b/lib/numbers_in_words/number_parser.rb index 9117661..bf65caa 100644 --- a/lib/numbers_in_words/number_parser.rb +++ b/lib/numbers_in_words/number_parser.rb @@ -40,6 +40,10 @@ module NumbersInWords::NumberParser #4. add 1 to memory 1 2000 #5. finish - add memory to answer 0 2001 def parse(integers) + scales_n = [100, 1000, 1000000, 1000000000, 1000000000000, 10**100] + if [] == scales_n & integers && integers.length > 1 + return pair_parse integers + end memory = 0 answer = 0 reset = true #reset each time memory is reset @@ -58,8 +62,7 @@ def parse(integers) reset = true end end - - if memory < integer + if memory < integer memory *= integer else memory += integer @@ -77,5 +80,36 @@ def power_of_ten? integer power_of_ten(integer) == power_of_ten(integer).to_i end + # 15,16 + # 85,16 + def pair_parse(ints) + ints = compress(ints) + return ints[0] if ints.length == 1 + sum = 0 + ints.each do |n| + sum *= n > 10 ? 100 : 10 + sum += n + end + sum + end + + # [40, 2] => [42] + def compress(ints) + res = [] + is_compressed = false + ints.each.with_index do |n,i| + next if i == 0 + if !is_compressed && n < 10 && ints[i - 1] % 10 == 0 + res << ints[i - 1] + n + is_compressed = true + else + res << ints[i - 1] if i == 1 + res << n unless n % 10 == 0 + is_compressed = false + end + end + res + end + extend self end diff --git a/spec/words_in_numbers_spec.rb b/spec/words_in_numbers_spec.rb index 4dadf27..31805a5 100644 --- a/spec/words_in_numbers_spec.rb +++ b/spec/words_in_numbers_spec.rb @@ -131,4 +131,14 @@ in_numbers.should == 75.84327694518 end + it "should handle years notation" do + "fifteen sixteen".in_numbers .should == 1516 + "eighty five sixteen".in_numbers .should == 8516 + "nineteen ninety six".in_numbers .should == 1996 + "forty nine ninety eight forty seven seventy nine".in_numbers .should == 49984779 + "fifteen sixteen".in_numbers .should == 1516 + "fifteen sixteen seven".in_numbers .should == 15167 + "fifteen sixteen seventeen".in_numbers .should == 151617 + end + end From 2a1f9a167056182e341f8bb743cf597667e3c3a9 Mon Sep 17 00:00:00 2001 From: Dimid Duchovny Date: Tue, 17 Mar 2015 15:33:03 +0200 Subject: [PATCH 02/27] num_parse: fix compress Signed-off-by: Dimid Duchovny --- lib/numbers_in_words/number_parser.rb | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/lib/numbers_in_words/number_parser.rb b/lib/numbers_in_words/number_parser.rb index bf65caa..21581fe 100644 --- a/lib/numbers_in_words/number_parser.rb +++ b/lib/numbers_in_words/number_parser.rb @@ -95,20 +95,17 @@ def pair_parse(ints) # [40, 2] => [42] def compress(ints) - res = [] - is_compressed = false - ints.each.with_index do |n,i| - next if i == 0 - if !is_compressed && n < 10 && ints[i - 1] % 10 == 0 - res << ints[i - 1] + n - is_compressed = true + res = []; i = 0 + while i < ints.length - 1 + if ints[i] % 10 == 0 && ints[i + 1] < 10 + res << ints[i] + ints[i + 1] + i += 2 else - res << ints[i - 1] if i == 1 - res << n unless n % 10 == 0 - is_compressed = false + res << ints[i] + i += 1 end end - res + i < ints.length ? res << ints[-1] : res end extend self From dbf601f6471ac82f6767b34fda0c68b00071d521 Mon Sep 17 00:00:00 2001 From: Dimid Duchovny Date: Sun, 29 Mar 2015 13:06:37 +0300 Subject: [PATCH 03/27] gem: version bump; contact info Signed-off-by: Dimid Duchovny --- lib/numbers_in_words/version.rb | 2 +- numbers_in_words.gemspec | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/numbers_in_words/version.rb b/lib/numbers_in_words/version.rb index 10785e4..7b50ca1 100644 --- a/lib/numbers_in_words/version.rb +++ b/lib/numbers_in_words/version.rb @@ -1,3 +1,3 @@ module NumbersInWords - VERSION = "0.2.0" + VERSION = "0.3.0" end diff --git a/numbers_in_words.gemspec b/numbers_in_words.gemspec index e2b1eec..43ed6b8 100644 --- a/numbers_in_words.gemspec +++ b/numbers_in_words.gemspec @@ -9,9 +9,9 @@ Gem::Specification.new do |gem| gem.summary = "Example: 123.in_words # => \"one hundred and twenty three\", \"seventy-five point eight\".in_numbers # = > 75.8" gem.version = NumbersInWords::VERSION - gem.authors = ["Mark Burns"] - gem.email = ["markthedeveloper@gmail.com"] - gem.homepage = "http://github.com/markburns/numbers_in_words" + gem.authors = ["Mark Burns", "Dimid Duchovny"] + gem.email = ["dimidd@gmail.com"] + gem.homepage = "http://github.com/dimidd/numbers_in_words" gem.add_dependency "activesupport" gem.add_development_dependency "rspec" From 1b42f148d52a9843c613b314de3412d86e879dbc Mon Sep 17 00:00:00 2001 From: Dimid Duchovny Date: Mon, 13 Apr 2015 13:10:32 +0300 Subject: [PATCH 04/27] to_num: add year notation for decimals Signed-off-by: Dimid Duchovny --- lib/numbers_in_words/to_number.rb | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/lib/numbers_in_words/to_number.rb b/lib/numbers_in_words/to_number.rb index 68dec3a..ec404e8 100644 --- a/lib/numbers_in_words/to_number.rb +++ b/lib/numbers_in_words/to_number.rb @@ -48,21 +48,10 @@ def handle_decimals text match = text.match(/\spoint\s/) if match integer = match.pre_match.in_numbers - - decimal = decimal_portion match.post_match - - integer + decimal + integer += ("0." + match.post_match.in_numbers.to_s).to_f end end - - def decimal_portion text - words = text.split " " - integers = word_array_to_integers words - decimal = "0." + integers.join() - decimal.to_f - end - #handles simple single word numbers #e.g. one, seven, twenty, eight, thousand etc def word_to_integer word From 77ba1c2cf0671c50e508b09124e8bfd55b161abd Mon Sep 17 00:00:00 2001 From: Dimid Duchovny Date: Mon, 13 Apr 2015 14:00:45 +0300 Subject: [PATCH 05/27] spec: years Signed-off-by: Dimid Duchovny --- spec/years.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 spec/years.rb diff --git a/spec/years.rb b/spec/years.rb new file mode 100644 index 0000000..d68326d --- /dev/null +++ b/spec/years.rb @@ -0,0 +1,15 @@ +require './spec/spec_helper' + +describe WordsInNumbers do + it "should handle years notation" do + "fifteen sixteen seventeen".in_numbers .should == 151617 + "forty nine ninety eight forty seven seventy nine".in_numbers .should == 49984779 + "sixty seven six".in_numbers .should == 676 + "one fifty".in_numbers.should == 150 + "two fifty".in_numbers.should == 250 + "one point fifty six fifty seven".in_numbers.should == 1.5657 + "one three forty seven".in_numbers.should == 1347 + "one three five point forty seven".in_numbers.should == 135.47 + end + +end From 01aa835084c772196142302e276d66eef389d37d Mon Sep 17 00:00:00 2001 From: Dimid Duchovny Date: Thu, 5 Mar 2015 20:05:12 +0200 Subject: [PATCH 06/27] num_parse: support years notation (nineteen twenty) Signed-off-by: Dimid Duchovny --- lib/numbers_in_words/number_parser.rb | 35 +++++++++++++++++++++++++-- spec/words_in_numbers_spec.rb | 10 ++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/lib/numbers_in_words/number_parser.rb b/lib/numbers_in_words/number_parser.rb index 9117661..21581fe 100644 --- a/lib/numbers_in_words/number_parser.rb +++ b/lib/numbers_in_words/number_parser.rb @@ -40,6 +40,10 @@ module NumbersInWords::NumberParser #4. add 1 to memory 1 2000 #5. finish - add memory to answer 0 2001 def parse(integers) + scales_n = [100, 1000, 1000000, 1000000000, 1000000000000, 10**100] + if [] == scales_n & integers && integers.length > 1 + return pair_parse integers + end memory = 0 answer = 0 reset = true #reset each time memory is reset @@ -58,8 +62,7 @@ def parse(integers) reset = true end end - - if memory < integer + if memory < integer memory *= integer else memory += integer @@ -77,5 +80,33 @@ def power_of_ten? integer power_of_ten(integer) == power_of_ten(integer).to_i end + # 15,16 + # 85,16 + def pair_parse(ints) + ints = compress(ints) + return ints[0] if ints.length == 1 + sum = 0 + ints.each do |n| + sum *= n > 10 ? 100 : 10 + sum += n + end + sum + end + + # [40, 2] => [42] + def compress(ints) + res = []; i = 0 + while i < ints.length - 1 + if ints[i] % 10 == 0 && ints[i + 1] < 10 + res << ints[i] + ints[i + 1] + i += 2 + else + res << ints[i] + i += 1 + end + end + i < ints.length ? res << ints[-1] : res + end + extend self end diff --git a/spec/words_in_numbers_spec.rb b/spec/words_in_numbers_spec.rb index 4dadf27..31805a5 100644 --- a/spec/words_in_numbers_spec.rb +++ b/spec/words_in_numbers_spec.rb @@ -131,4 +131,14 @@ in_numbers.should == 75.84327694518 end + it "should handle years notation" do + "fifteen sixteen".in_numbers .should == 1516 + "eighty five sixteen".in_numbers .should == 8516 + "nineteen ninety six".in_numbers .should == 1996 + "forty nine ninety eight forty seven seventy nine".in_numbers .should == 49984779 + "fifteen sixteen".in_numbers .should == 1516 + "fifteen sixteen seven".in_numbers .should == 15167 + "fifteen sixteen seventeen".in_numbers .should == 151617 + end + end From a71d278132debd9a269511b3f4fa47b7f58f6897 Mon Sep 17 00:00:00 2001 From: Dimid Duchovny Date: Sun, 29 Mar 2015 13:06:37 +0300 Subject: [PATCH 07/27] gem: version bump; contact info Signed-off-by: Dimid Duchovny --- lib/numbers_in_words/version.rb | 2 +- numbers_in_words.gemspec | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/numbers_in_words/version.rb b/lib/numbers_in_words/version.rb index 10785e4..7b50ca1 100644 --- a/lib/numbers_in_words/version.rb +++ b/lib/numbers_in_words/version.rb @@ -1,3 +1,3 @@ module NumbersInWords - VERSION = "0.2.0" + VERSION = "0.3.0" end diff --git a/numbers_in_words.gemspec b/numbers_in_words.gemspec index e2b1eec..43ed6b8 100644 --- a/numbers_in_words.gemspec +++ b/numbers_in_words.gemspec @@ -9,9 +9,9 @@ Gem::Specification.new do |gem| gem.summary = "Example: 123.in_words # => \"one hundred and twenty three\", \"seventy-five point eight\".in_numbers # = > 75.8" gem.version = NumbersInWords::VERSION - gem.authors = ["Mark Burns"] - gem.email = ["markthedeveloper@gmail.com"] - gem.homepage = "http://github.com/markburns/numbers_in_words" + gem.authors = ["Mark Burns", "Dimid Duchovny"] + gem.email = ["dimidd@gmail.com"] + gem.homepage = "http://github.com/dimidd/numbers_in_words" gem.add_dependency "activesupport" gem.add_development_dependency "rspec" From 76e0d069990adbf12493376c2e296f633f489a49 Mon Sep 17 00:00:00 2001 From: Dimid Duchovny Date: Mon, 13 Apr 2015 13:10:32 +0300 Subject: [PATCH 08/27] to_num: add year notation for decimals Signed-off-by: Dimid Duchovny --- lib/numbers_in_words/to_number.rb | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/lib/numbers_in_words/to_number.rb b/lib/numbers_in_words/to_number.rb index 68dec3a..ec404e8 100644 --- a/lib/numbers_in_words/to_number.rb +++ b/lib/numbers_in_words/to_number.rb @@ -48,21 +48,10 @@ def handle_decimals text match = text.match(/\spoint\s/) if match integer = match.pre_match.in_numbers - - decimal = decimal_portion match.post_match - - integer + decimal + integer += ("0." + match.post_match.in_numbers.to_s).to_f end end - - def decimal_portion text - words = text.split " " - integers = word_array_to_integers words - decimal = "0." + integers.join() - decimal.to_f - end - #handles simple single word numbers #e.g. one, seven, twenty, eight, thousand etc def word_to_integer word From e67f358d42f8dfbf6d11b13405087fd8ece7b4a4 Mon Sep 17 00:00:00 2001 From: Dimid Duchovny Date: Mon, 13 Apr 2015 14:00:45 +0300 Subject: [PATCH 09/27] spec: years Signed-off-by: Dimid Duchovny --- spec/years.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 spec/years.rb diff --git a/spec/years.rb b/spec/years.rb new file mode 100644 index 0000000..d68326d --- /dev/null +++ b/spec/years.rb @@ -0,0 +1,15 @@ +require './spec/spec_helper' + +describe WordsInNumbers do + it "should handle years notation" do + "fifteen sixteen seventeen".in_numbers .should == 151617 + "forty nine ninety eight forty seven seventy nine".in_numbers .should == 49984779 + "sixty seven six".in_numbers .should == 676 + "one fifty".in_numbers.should == 150 + "two fifty".in_numbers.should == 250 + "one point fifty six fifty seven".in_numbers.should == 1.5657 + "one three forty seven".in_numbers.should == 1347 + "one three five point forty seven".in_numbers.should == 135.47 + end + +end From 68cac08baafc19d0c62d236412b62714d46e0d1d Mon Sep 17 00:00:00 2001 From: Dimid Duchovny Date: Wed, 15 Apr 2015 09:27:08 +0300 Subject: [PATCH 10/27] Revert " gem: version bump; contact info" This reverts commit dbf601f6471ac82f6767b34fda0c68b00071d521. --- lib/numbers_in_words/version.rb | 2 +- numbers_in_words.gemspec | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/numbers_in_words/version.rb b/lib/numbers_in_words/version.rb index 7b50ca1..10785e4 100644 --- a/lib/numbers_in_words/version.rb +++ b/lib/numbers_in_words/version.rb @@ -1,3 +1,3 @@ module NumbersInWords - VERSION = "0.3.0" + VERSION = "0.2.0" end diff --git a/numbers_in_words.gemspec b/numbers_in_words.gemspec index 43ed6b8..e2b1eec 100644 --- a/numbers_in_words.gemspec +++ b/numbers_in_words.gemspec @@ -9,9 +9,9 @@ Gem::Specification.new do |gem| gem.summary = "Example: 123.in_words # => \"one hundred and twenty three\", \"seventy-five point eight\".in_numbers # = > 75.8" gem.version = NumbersInWords::VERSION - gem.authors = ["Mark Burns", "Dimid Duchovny"] - gem.email = ["dimidd@gmail.com"] - gem.homepage = "http://github.com/dimidd/numbers_in_words" + gem.authors = ["Mark Burns"] + gem.email = ["markthedeveloper@gmail.com"] + gem.homepage = "http://github.com/markburns/numbers_in_words" gem.add_dependency "activesupport" gem.add_development_dependency "rspec" From 2ae6fdb59f41328a6a9e32968f004237f3b65481 Mon Sep 17 00:00:00 2001 From: Dimid Duchovny Date: Tue, 21 Apr 2015 15:15:36 +0300 Subject: [PATCH 11/27] num_parse: allow to get just the compressed array duck punches 'num_compress' to String Signed-off-by: Dimid Duchovny --- lib/numbers_in_words/duck_punch.rb | 4 ++++ lib/numbers_in_words/number_parser.rb | 8 +++++--- lib/numbers_in_words/to_number.rb | 4 ++-- spec/years_compress.rb | 13 +++++++++++++ 4 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 spec/years_compress.rb diff --git a/lib/numbers_in_words/duck_punch.rb b/lib/numbers_in_words/duck_punch.rb index dd10161..5f2514b 100644 --- a/lib/numbers_in_words/duck_punch.rb +++ b/lib/numbers_in_words/duck_punch.rb @@ -8,6 +8,10 @@ module WordsInNumbers def in_numbers language=NumbersInWords.language NumbersInWords::ToNumber.new(self, language).in_numbers end + + def num_compress language=NumbersInWords.language + NumbersInWords::ToNumber.new(self, language).in_numbers true + end end class String diff --git a/lib/numbers_in_words/number_parser.rb b/lib/numbers_in_words/number_parser.rb index 21581fe..11d90bd 100644 --- a/lib/numbers_in_words/number_parser.rb +++ b/lib/numbers_in_words/number_parser.rb @@ -39,10 +39,10 @@ module NumbersInWords::NumberParser #3. add memory to answer,reset, because power of ten>2 0 2000 #4. add 1 to memory 1 2000 #5. finish - add memory to answer 0 2001 - def parse(integers) + def parse(integers, only_compress = false) scales_n = [100, 1000, 1000000, 1000000000, 1000000000000, 10**100] if [] == scales_n & integers && integers.length > 1 - return pair_parse integers + return pair_parse(integers, only_compress) end memory = 0 answer = 0 @@ -82,8 +82,9 @@ def power_of_ten? integer # 15,16 # 85,16 - def pair_parse(ints) + def pair_parse(ints, only_compress = false) ints = compress(ints) + return ints if only_compress return ints[0] if ints.length == 1 sum = 0 ints.each do |n| @@ -96,6 +97,7 @@ def pair_parse(ints) # [40, 2] => [42] def compress(ints) res = []; i = 0 + return [] if ints.empty? while i < ints.length - 1 if ints[i] % 10 == 0 && ints[i + 1] < 10 res << ints[i] + ints[i + 1] diff --git a/lib/numbers_in_words/to_number.rb b/lib/numbers_in_words/to_number.rb index ec404e8..a0fe065 100644 --- a/lib/numbers_in_words/to_number.rb +++ b/lib/numbers_in_words/to_number.rb @@ -20,7 +20,7 @@ def handle_negative text -1 * (text.gsub(/^minus /, "")).in_numbers if text =~ /^minus / end - def in_numbers + def in_numbers(only_compress = false) text = to_s text = strip_punctuation text @@ -32,7 +32,7 @@ def in_numbers integers = word_array_to_integers text.split(" ") - NumbersInWords::NumberParser.parse integers + NumbersInWords::NumberParser.parse integers, only_compress end def strip_punctuation text diff --git a/spec/years_compress.rb b/spec/years_compress.rb new file mode 100644 index 0000000..69d1374 --- /dev/null +++ b/spec/years_compress.rb @@ -0,0 +1,13 @@ +require './spec/spec_helper' + +describe WordsInNumbers do + it "should handle years notation" do + "fifteen sixteen seventeen".num_compress .should == [15, 16, 17] + "forty nine ninety eight forty seven seventy nine".num_compress .should == [49, 98, 47, 79] + "sixty seven six".num_compress .should == [67, 6] + "one fifty".num_compress.should == [1, 50] + "two fifty".num_compress.should == [2, 50] + "one three forty seven".num_compress.should == [1, 3, 47] + end + +end From 6a40c316ca06668ae96678c0f4ea2e97d4b870ca Mon Sep 17 00:00:00 2001 From: Dimid Duchovny Date: Tue, 21 Apr 2015 15:49:22 +0300 Subject: [PATCH 12/27] gem: version bump Signed-off-by: Dimid Duchovny --- lib/numbers_in_words/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/numbers_in_words/version.rb b/lib/numbers_in_words/version.rb index 7b50ca1..fd8ea92 100644 --- a/lib/numbers_in_words/version.rb +++ b/lib/numbers_in_words/version.rb @@ -1,3 +1,3 @@ module NumbersInWords - VERSION = "0.3.0" + VERSION = "0.3.1" end From 383655af3d371988639060cea357787f8fd2b1ed Mon Sep 17 00:00:00 2001 From: Dimid Duchovny Date: Tue, 21 Apr 2015 16:39:10 +0300 Subject: [PATCH 13/27] num_parse: fix num_compress for empty string Signed-off-by: Dimid Duchovny --- lib/numbers_in_words/number_parser.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/numbers_in_words/number_parser.rb b/lib/numbers_in_words/number_parser.rb index 11d90bd..bb1fcc4 100644 --- a/lib/numbers_in_words/number_parser.rb +++ b/lib/numbers_in_words/number_parser.rb @@ -41,7 +41,10 @@ module NumbersInWords::NumberParser #5. finish - add memory to answer 0 2001 def parse(integers, only_compress = false) scales_n = [100, 1000, 1000000, 1000000000, 1000000000000, 10**100] - if [] == scales_n & integers && integers.length > 1 + if integers.length < 2 + return only_compress ? integers : integers.empty? ? 0 : integers[0] + end + if [] == scales_n & integers return pair_parse(integers, only_compress) end memory = 0 From 5b594a18589763fde9222155d81ace620037c6c2 Mon Sep 17 00:00:00 2001 From: Dimid Duchovny Date: Tue, 21 Apr 2015 17:42:44 +0300 Subject: [PATCH 14/27] num_parse: fix compress for 'zero' Signed-off-by: Dimid Duchovny --- lib/numbers_in_words/number_parser.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/numbers_in_words/number_parser.rb b/lib/numbers_in_words/number_parser.rb index bb1fcc4..90fd95b 100644 --- a/lib/numbers_in_words/number_parser.rb +++ b/lib/numbers_in_words/number_parser.rb @@ -102,7 +102,7 @@ def compress(ints) res = []; i = 0 return [] if ints.empty? while i < ints.length - 1 - if ints[i] % 10 == 0 && ints[i + 1] < 10 + if ints[i] > 0 && ints[i] % 10 == 0 && ints[i + 1] < 10 res << ints[i] + ints[i + 1] i += 2 else From a39012d74073b891f43023acb3005edbcfba9189 Mon Sep 17 00:00:00 2001 From: Dimid Duchovny Date: Sun, 2 Aug 2015 16:59:23 +0300 Subject: [PATCH 15/27] num_parse: fix error with pair parsing 10 Signed-off-by: Dimid Duchovny --- lib/numbers_in_words/number_parser.rb | 2 +- spec/years.rb | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/numbers_in_words/number_parser.rb b/lib/numbers_in_words/number_parser.rb index 90fd95b..bb0e505 100644 --- a/lib/numbers_in_words/number_parser.rb +++ b/lib/numbers_in_words/number_parser.rb @@ -91,7 +91,7 @@ def pair_parse(ints, only_compress = false) return ints[0] if ints.length == 1 sum = 0 ints.each do |n| - sum *= n > 10 ? 100 : 10 + sum *= n >= 10 ? 100 : 10 sum += n end sum diff --git a/spec/years.rb b/spec/years.rb index d68326d..b0e3504 100644 --- a/spec/years.rb +++ b/spec/years.rb @@ -9,7 +9,8 @@ "two fifty".in_numbers.should == 250 "one point fifty six fifty seven".in_numbers.should == 1.5657 "one three forty seven".in_numbers.should == 1347 - "one three five point forty seven".in_numbers.should == 135.47 + "one three five point forty seven".in_numbers.should == 135.47 + "one ten sixty three".in_numbers.should == 11063 end end From dafc82d6303e925ff3f2473a569d867a21306ac4 Mon Sep 17 00:00:00 2001 From: Dimid Duchovny Date: Wed, 9 Sep 2015 11:37:02 +0300 Subject: [PATCH 16/27] in_num: support numeric strings Signed-off-by: Dimid Duchovny --- lib/numbers_in_words/to_number.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/numbers_in_words/to_number.rb b/lib/numbers_in_words/to_number.rb index a0fe065..b8fcca6 100644 --- a/lib/numbers_in_words/to_number.rb +++ b/lib/numbers_in_words/to_number.rb @@ -22,6 +22,7 @@ def handle_negative text def in_numbers(only_compress = false) text = to_s + return text.to_f if text =~ /^-?\d+(.\d+)?$/ text = strip_punctuation text i = handle_negative text From b578476f03f9b88905f4a86628d81dc8d18b4985 Mon Sep 17 00:00:00 2001 From: Dimid Duchovny Date: Fri, 23 Oct 2015 07:39:45 +0300 Subject: [PATCH 17/27] gem: replace debugger with pry, update rspec Signed-off-by: Dimid Duchovny --- Gemfile.lock | 64 ++++++++++++++++++++++++---------------- numbers_in_words.gemspec | 4 +-- 2 files changed, 41 insertions(+), 27 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 067dfbf..29bd087 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,39 +1,53 @@ PATH remote: . specs: - numbers_in_words (0.2.0) + numbers_in_words (0.3.1) activesupport GEM remote: https://rubygems.org/ specs: - activesupport (3.2.10) - i18n (~> 0.6) - multi_json (~> 1.0) - columnize (0.3.6) - debugger (1.2.3) - columnize (>= 0.3.1) - debugger-linecache (~> 1.1.1) - debugger-ruby_core_source (~> 1.1.5) - debugger-linecache (1.1.2) - debugger-ruby_core_source (>= 1.1.1) - debugger-ruby_core_source (1.1.6) - diff-lcs (1.1.3) - i18n (0.6.1) - multi_json (1.5.0) - rspec (2.12.0) - rspec-core (~> 2.12.0) - rspec-expectations (~> 2.12.0) - rspec-mocks (~> 2.12.0) - rspec-core (2.12.2) - rspec-expectations (2.12.1) - diff-lcs (~> 1.1.3) - rspec-mocks (2.12.1) + activesupport (4.2.4) + i18n (~> 0.7) + json (~> 1.7, >= 1.7.7) + minitest (~> 5.1) + thread_safe (~> 0.3, >= 0.3.4) + tzinfo (~> 1.1) + coderay (1.1.0) + diff-lcs (1.2.5) + i18n (0.7.0) + json (1.8.3) + method_source (0.8.2) + minitest (5.8.1) + pry (0.10.2) + coderay (~> 1.1.0) + method_source (~> 0.8.1) + slop (~> 3.4) + rspec (3.3.0) + rspec-core (~> 3.3.0) + rspec-expectations (~> 3.3.0) + rspec-mocks (~> 3.3.0) + rspec-core (3.3.2) + rspec-support (~> 3.3.0) + rspec-expectations (3.3.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.3.0) + rspec-mocks (3.3.2) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.3.0) + rspec-support (3.3.0) + slop (3.6.0) + thread_safe (0.3.5) + tzinfo (1.2.2) + thread_safe (~> 0.1) PLATFORMS ruby DEPENDENCIES - debugger numbers_in_words! - rspec + pry + rspec (>= 2.14) + +BUNDLED WITH + 1.10.6 diff --git a/numbers_in_words.gemspec b/numbers_in_words.gemspec index 43ed6b8..76ac3cf 100644 --- a/numbers_in_words.gemspec +++ b/numbers_in_words.gemspec @@ -14,8 +14,8 @@ Gem::Specification.new do |gem| gem.homepage = "http://github.com/dimidd/numbers_in_words" gem.add_dependency "activesupport" - gem.add_development_dependency "rspec" - gem.add_development_dependency "debugger" + gem.add_development_dependency "rspec", ">= 2.14" + gem.add_development_dependency "pry" gem.files = `git ls-files`.split($/) gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) } From c851cf8b321cad821c0c57964313b01c3a0ffc2e Mon Sep 17 00:00:00 2001 From: Dimid Duchovny Date: Fri, 23 Oct 2015 07:42:16 +0300 Subject: [PATCH 18/27] in_num: support simple mixed strings Signed-off-by: Dimid Duchovny --- lib/numbers_in_words/to_number.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/numbers_in_words/to_number.rb b/lib/numbers_in_words/to_number.rb index b8fcca6..41eb0c5 100644 --- a/lib/numbers_in_words/to_number.rb +++ b/lib/numbers_in_words/to_number.rb @@ -25,6 +25,9 @@ def in_numbers(only_compress = false) return text.to_f if text =~ /^-?\d+(.\d+)?$/ text = strip_punctuation text + mixed = text.match /^(-?\d+(.\d+)?) (hundred|thousand|million|billion|trillion)$/ + return mixed[1].in_numbers * mixed[3].in_numbers if mixed && mixed[1] && mixed[3] + i = handle_negative text return i if i @@ -37,7 +40,7 @@ def in_numbers(only_compress = false) end def strip_punctuation text - text = text.downcase.gsub(/[^a-z ]/, " ") + text = text.downcase.gsub(/[^a-z 0-9]/, " ") to_remove = true to_remove = text.gsub! " ", " " while to_remove From 3dc769e3a68b3d74d3e77721d0e89b2917fda7ea Mon Sep 17 00:00:00 2001 From: Dimid Duchovny Date: Fri, 23 Oct 2015 07:44:07 +0300 Subject: [PATCH 19/27] spec: add numerical_strings, fix years Signed-off-by: Dimid Duchovny --- spec/numerical_strings_spec.rb | 19 +++++++++++++++++++ ...ars_compress.rb => years_compress_spec.rb} | 0 spec/{years.rb => years_spec.rb} | 0 3 files changed, 19 insertions(+) create mode 100644 spec/numerical_strings_spec.rb rename spec/{years_compress.rb => years_compress_spec.rb} (100%) rename spec/{years.rb => years_spec.rb} (100%) diff --git a/spec/numerical_strings_spec.rb b/spec/numerical_strings_spec.rb new file mode 100644 index 0000000..5adc20f --- /dev/null +++ b/spec/numerical_strings_spec.rb @@ -0,0 +1,19 @@ +require './spec/spec_helper' + +describe NumbersInWords do + it "should recognize numerical strings" do + arr = %w(8 56 100 5789 3435356) + arr.each{ |s| expect(s.in_numbers).to eql(s.to_f) } + end + + it "should recognize mixed strings" do + mixed = { + "19 hundred" => 1_900.0, + "20 thousand" => 20_000.0, + "100 million" => 100_000_000.0, + "7 billion" => 7_000_000_000.0, + "42 trillion" => 42_000_000_000_000.0 + } + mixed.each{ |k, v| expect(k.in_numbers).to eql(v) } + end +end diff --git a/spec/years_compress.rb b/spec/years_compress_spec.rb similarity index 100% rename from spec/years_compress.rb rename to spec/years_compress_spec.rb diff --git a/spec/years.rb b/spec/years_spec.rb similarity index 100% rename from spec/years.rb rename to spec/years_spec.rb From 9b7d5e623bc6d0ba4a9fa69840331000fe79eb22 Mon Sep 17 00:00:00 2001 From: Dimid Duchovny Date: Fri, 23 Oct 2015 07:45:31 +0300 Subject: [PATCH 20/27] Convert specs to RSpec 3.3.2 syntax with Transpec This conversion is done by Transpec 3.1.1 with the following command: transpec * 143 conversions from: obj.should to: expect(obj).to * 138 conversions from: == expected to: eq(expected) * 5 conversions from: =~ /pattern/ to: match(/pattern/) For more details: https://github.com/yujinakayama/transpec#supported-conversions --- spec/language_writer_spec.rb | 12 +-- spec/number_group_spec.rb | 14 +-- spec/numbers_in_words_spec.rb | 76 ++++++++-------- spec/words_in_numbers_spec.rb | 157 +++++++++++++++++----------------- spec/years_compress_spec.rb | 12 +-- spec/years_spec.rb | 18 ++-- 6 files changed, 145 insertions(+), 144 deletions(-) diff --git a/spec/language_writer_spec.rb b/spec/language_writer_spec.rb index d9563fb..8c056dc 100644 --- a/spec/language_writer_spec.rb +++ b/spec/language_writer_spec.rb @@ -7,13 +7,13 @@ @writer.group_words(3) do |power, name, digits| case count when 0 - power.should == 3 - name.should == "thousand" - digits.should == 2 + expect(power).to eq(3) + expect(name).to eq("thousand") + expect(digits).to eq(2) when 1 - power.should == 0 - name.should == "one" - digits.should == 111 + expect(power).to eq(0) + expect(name).to eq("one") + expect(digits).to eq(111) end count += 1 end diff --git a/spec/number_group_spec.rb b/spec/number_group_spec.rb index 7fd631b..3490ca2 100644 --- a/spec/number_group_spec.rb +++ b/spec/number_group_spec.rb @@ -4,13 +4,13 @@ it "should split into group of three digit numbers" do g = Numeric::NumberGroup - g.groups_of(1 ,3 ).should == {0=>1 } - g.groups_of(12 ,3 ).should == {0=>12 } - g.groups_of(123 ,3 ).should == {0=>123 } - g.groups_of(1111 ,3 ).should == {3=>1 , 0=>111 } - g.groups_of(87654 ,3 ).should == {3=>87 , 0=>654 } - g.groups_of(1_234_567 ,3 ).should == {6=>1 , 3=>234 , 0=>567 } - g.groups_of(123_456_789_101_112 ,3 ).should == {12=>123 , 9=>456 , 6=>789 , 3=>101 , 0=>112 } + expect(g.groups_of(1 ,3 )).to eq({0=>1 }) + expect(g.groups_of(12 ,3 )).to eq({0=>12 }) + expect(g.groups_of(123 ,3 )).to eq({0=>123 }) + expect(g.groups_of(1111 ,3 )).to eq({3=>1 , 0=>111 }) + expect(g.groups_of(87654 ,3 )).to eq({3=>87 , 0=>654 }) + expect(g.groups_of(1_234_567 ,3 )).to eq({6=>1 , 3=>234 , 0=>567 }) + expect(g.groups_of(123_456_789_101_112 ,3 )).to eq({12=>123 , 9=>456 , 6=>789 , 3=>101 , 0=>112 }) end end diff --git a/spec/numbers_in_words_spec.rb b/spec/numbers_in_words_spec.rb index f2251fb..6cc5f81 100644 --- a/spec/numbers_in_words_spec.rb +++ b/spec/numbers_in_words_spec.rb @@ -4,76 +4,76 @@ it "should print the digits 0-9 correctly" do numbers = %w[zero one two three four five six seven eight nine] - 10.times { |i| i.in_words.should==numbers[i] } + 10.times { |i| expect(i.in_words).to eq(numbers[i]) } end it "should print the digits 11-19 correctly" do words = %w[eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen] numbers = [11,12,13,14,15,16,17,18,19] numbers.each_with_index do |number, index| - number.in_words.should==words[index] + expect(number.in_words).to eq(words[index]) end end it "should handle two digit numbers" do - 21.in_words. should == "twenty one" + expect(21.in_words). to eq("twenty one") end it "should handle three digit numbers" do - 113.in_words. should == "one hundred and thirteen" - 299.in_words. should == "two hundred and ninety nine" - 300.in_words. should == "three hundred" - 101.in_words. should == "one hundred and one" + expect(113.in_words). to eq("one hundred and thirteen") + expect(299.in_words). to eq("two hundred and ninety nine") + expect(300.in_words). to eq("three hundred") + expect(101.in_words). to eq("one hundred and one") end it "should print out some random examples correctly" do - 2999 .in_words. should == "two thousand nine hundred and ninety nine" - 99999 .in_words. should == "ninety nine thousand nine hundred and ninety nine" - 999999 .in_words. should == "nine hundred and ninety nine thousand nine hundred and ninety nine" - 123456 .in_words. should == "one hundred and twenty three thousand four hundred and fifty six" - 17054 .in_words. should == "seventeen thousand and fifty four" - 11004 .in_words. should == "eleven thousand and four" - 470154 .in_words. should == "four hundred and seventy thousand one hundred and fifty four" - 417155 .in_words. should == "four hundred and seventeen thousand one hundred and fifty five" - 999999 .in_words. should == "nine hundred and ninety nine thousand nine hundred and ninety nine" - 1000000 .in_words. should == "one million" - 1000001 .in_words. should == "one million and one" - 112 .in_words. should == "one hundred and twelve" + expect(2999 .in_words). to eq("two thousand nine hundred and ninety nine") + expect(99999 .in_words). to eq("ninety nine thousand nine hundred and ninety nine") + expect(999999 .in_words). to eq("nine hundred and ninety nine thousand nine hundred and ninety nine") + expect(123456 .in_words). to eq("one hundred and twenty three thousand four hundred and fifty six") + expect(17054 .in_words). to eq("seventeen thousand and fifty four") + expect(11004 .in_words). to eq("eleven thousand and four") + expect(470154 .in_words). to eq("four hundred and seventy thousand one hundred and fifty four") + expect(417155 .in_words). to eq("four hundred and seventeen thousand one hundred and fifty five") + expect(999999 .in_words). to eq("nine hundred and ninety nine thousand nine hundred and ninety nine") + expect(1000000 .in_words). to eq("one million") + expect(1000001 .in_words). to eq("one million and one") + expect(112 .in_words). to eq("one hundred and twelve") end it "should handle edge cases" do - 1000001 .in_words. should == "one million and one" - (10*10**12 + 10**6 +1) .in_words. should == "ten trillion one million and one" - (10**75) .in_words. should == "one quattuorvigintillion" - 10001001 .in_words. should == "ten million one thousand and one" + expect(1000001 .in_words). to eq("one million and one") + expect((10*10**12 + 10**6 +1) .in_words). to eq("ten trillion one million and one") + expect((10**75) .in_words). to eq("one quattuorvigintillion") + expect(10001001 .in_words). to eq("ten million one thousand and one") end it "should handle a googol and larger" do n=10**100 - (10**100 + 1) .in_words. should == "one googol and one" - (42*10**100 + 16777216).in_words. should == "forty two googol sixteen million seven hundred and seventy seven thousand two hundred and sixteen" - (42* 10**100 * 10**100).in_words. should == "forty two googol googol" + expect((10**100 + 1) .in_words). to eq("one googol and one") + expect((42*10**100 + 16777216).in_words). to eq("forty two googol sixteen million seven hundred and seventy seven thousand two hundred and sixteen") + expect((42* 10**100 * 10**100).in_words). to eq("forty two googol googol") end it "should handle negative numbers" do - -1 .in_words.should == "minus one" - -9 .in_words.should == "minus nine" - -10 .in_words.should == "minus ten" - -15 .in_words.should == "minus fifteen" - -100 .in_words.should == "minus one hundred" - (-1*(10**100)).in_words.should == "minus one googol" - -123456789 .in_words.should == "minus one hundred and twenty three million four hundred and fifty six thousand seven hundred and eighty nine" + expect(-1 .in_words).to eq("minus one") + expect(-9 .in_words).to eq("minus nine") + expect(-10 .in_words).to eq("minus ten") + expect(-15 .in_words).to eq("minus fifteen") + expect(-100 .in_words).to eq("minus one hundred") + expect((-1*(10**100)).in_words).to eq("minus one googol") + expect(-123456789 .in_words).to eq("minus one hundred and twenty three million four hundred and fifty six thousand seven hundred and eighty nine") end it "should handle decimals" do #because of lack of absolute accuracy with floats #the output won't be exactly as you might expect #so we will match rather than find equivalents - 1.1 .in_words.should =~ /one point one/ - 1.2345678 .in_words.should =~ /one point two three four five six seven eight/ - 1000.2345678 .in_words.should =~ /one thousand point two three four five six seven eight/ - 12345.2345678.in_words.should =~ /twelve thousand three hundred and forty five point two three four five six seven eight/ - (10**9 + 0.1).in_words.should =~ /one billion point one/ + expect(1.1 .in_words).to match(/one point one/) + expect(1.2345678 .in_words).to match(/one point two three four five six seven eight/) + expect(1000.2345678 .in_words).to match(/one thousand point two three four five six seven eight/) + expect(12345.2345678.in_words).to match(/twelve thousand three hundred and forty five point two three four five six seven eight/) + expect((10**9 + 0.1).in_words).to match(/one billion point one/) end end diff --git a/spec/words_in_numbers_spec.rb b/spec/words_in_numbers_spec.rb index 31805a5..ddea76c 100644 --- a/spec/words_in_numbers_spec.rb +++ b/spec/words_in_numbers_spec.rb @@ -2,105 +2,105 @@ describe WordsInNumbers do it "should do the digits 0-10" do - "zero" .in_numbers.should == 0 - "one" .in_numbers.should == 1 - "two" .in_numbers.should == 2 - "three" .in_numbers.should == 3 - "four" .in_numbers.should == 4 - "five" .in_numbers.should == 5 - "six" .in_numbers.should == 6 - "seven" .in_numbers.should == 7 - "eight" .in_numbers.should == 8 - "nine" .in_numbers.should == 9 + expect("zero" .in_numbers).to eq(0) + expect("one" .in_numbers).to eq(1) + expect("two" .in_numbers).to eq(2) + expect("three" .in_numbers).to eq(3) + expect("four" .in_numbers).to eq(4) + expect("five" .in_numbers).to eq(5) + expect("six" .in_numbers).to eq(6) + expect("seven" .in_numbers).to eq(7) + expect("eight" .in_numbers).to eq(8) + expect("nine" .in_numbers).to eq(9) end it "should handle numbers for which there is one word" do - "ten" .in_numbers.should == 10 - "eleven" .in_numbers.should == 11 - "twelve" .in_numbers.should == 12 - "thirteen" .in_numbers.should == 13 - "fourteen" .in_numbers.should == 14 - "fifteen" .in_numbers.should == 15 - "sixteen" .in_numbers.should == 16 - "seventeen" .in_numbers.should == 17 - "eighteen" .in_numbers.should == 18 - "nineteen" .in_numbers.should == 19 - "twenty" .in_numbers.should == 20 + expect("ten" .in_numbers).to eq(10) + expect("eleven" .in_numbers).to eq(11) + expect("twelve" .in_numbers).to eq(12) + expect("thirteen" .in_numbers).to eq(13) + expect("fourteen" .in_numbers).to eq(14) + expect("fifteen" .in_numbers).to eq(15) + expect("sixteen" .in_numbers).to eq(16) + expect("seventeen" .in_numbers).to eq(17) + expect("eighteen" .in_numbers).to eq(18) + expect("nineteen" .in_numbers).to eq(19) + expect("twenty" .in_numbers).to eq(20) end it "should handle two word numbers up to 100" do - "twenty one" .in_numbers.should == 21 - "twenty two" .in_numbers.should == 22 - "twenty three" .in_numbers.should == 23 - "twenty four" .in_numbers.should == 24 - "twenty five" .in_numbers.should == 25 - "twenty six" .in_numbers.should == 26 - "twenty seven" .in_numbers.should == 27 - "twenty eight" .in_numbers.should == 28 - "seventy six" .in_numbers.should == 76 - "ninety nine" .in_numbers.should == 99 + expect("twenty one" .in_numbers).to eq(21) + expect("twenty two" .in_numbers).to eq(22) + expect("twenty three" .in_numbers).to eq(23) + expect("twenty four" .in_numbers).to eq(24) + expect("twenty five" .in_numbers).to eq(25) + expect("twenty six" .in_numbers).to eq(26) + expect("twenty seven" .in_numbers).to eq(27) + expect("twenty eight" .in_numbers).to eq(28) + expect("seventy six" .in_numbers).to eq(76) + expect("ninety nine" .in_numbers).to eq(99) end it "should handle hundreds" do - "one hundred" .in_numbers.should == 100 - "two hundred" .in_numbers.should == 200 - "three hundred" .in_numbers.should == 300 - "nine hundred" .in_numbers.should == 900 - "one hundred and seventy six" .in_numbers.should == 176 - "one hundred and seventy nine" .in_numbers.should == 179 - "nine hundred and ninety nine" .in_numbers.should == 999 + expect("one hundred" .in_numbers).to eq(100) + expect("two hundred" .in_numbers).to eq(200) + expect("three hundred" .in_numbers).to eq(300) + expect("nine hundred" .in_numbers).to eq(900) + expect("one hundred and seventy six" .in_numbers).to eq(176) + expect("one hundred and seventy nine" .in_numbers).to eq(179) + expect("nine hundred and ninety nine" .in_numbers).to eq(999) end it "should handle unusual hundreds" do - "eleven hundred" .in_numbers.should == 1100 - "twelve hundred" .in_numbers.should == 1200 - "thirteen hundred" .in_numbers.should == 1300 - "fifteen hundred" .in_numbers.should == 1500 - "nineteen hundred" .in_numbers.should == 1900 + expect("eleven hundred" .in_numbers).to eq(1100) + expect("twelve hundred" .in_numbers).to eq(1200) + expect("thirteen hundred" .in_numbers).to eq(1300) + expect("fifteen hundred" .in_numbers).to eq(1500) + expect("nineteen hundred" .in_numbers).to eq(1900) end it "should handle thousands" do - "two thousand and one" .in_numbers .should == 2001 - "one thousand" .in_numbers .should == 1000 - "two thousand" .in_numbers .should == 2000 - "three thousand" .in_numbers .should == 3000 - "nine thousand" .in_numbers .should == 9000 - "nine thousand two hundred" .in_numbers .should == 9200 - "nine thousand two hundred and seven" .in_numbers .should == 9207 - "nine thousand two hundred and ninety seven" .in_numbers .should == 9297 + expect("two thousand and one" .in_numbers) .to eq(2001) + expect("one thousand" .in_numbers) .to eq(1000) + expect("two thousand" .in_numbers) .to eq(2000) + expect("three thousand" .in_numbers) .to eq(3000) + expect("nine thousand" .in_numbers) .to eq(9000) + expect("nine thousand two hundred" .in_numbers) .to eq(9200) + expect("nine thousand two hundred and seven" .in_numbers) .to eq(9207) + expect("nine thousand two hundred and ninety seven" .in_numbers) .to eq(9297) end it "should handle larger numbers" do - "one million" .in_numbers .should == 1000000 - "two googol five billion and seventy six" .in_numbers .should == (2*10**100 + 5*10**9 + 76) - "thirty seven million" .in_numbers .should == 37 * 10**6 - "twenty six googol" .in_numbers .should == 26 * 10**100 + expect("one million" .in_numbers) .to eq(1000000) + expect("two googol five billion and seventy six" .in_numbers) .to eq(2*10**100 + 5*10**9 + 76) + expect("thirty seven million" .in_numbers) .to eq(37 * 10**6) + expect("twenty six googol" .in_numbers) .to eq(26 * 10**100) end it "should handle numbers in hundreds of thousands etc" do - "nine hundred thousand" .in_numbers .should == 900000 - "three hundred and fifty seven thousand" .in_numbers .should == 357000 - "five million three hundred and fifty seven thousand" .in_numbers .should == 5357000 - "nine hundred and ninety nine trillion" .in_numbers .should == 999 * 10**12 + expect("nine hundred thousand" .in_numbers) .to eq(900000) + expect("three hundred and fifty seven thousand" .in_numbers) .to eq(357000) + expect("five million three hundred and fifty seven thousand" .in_numbers) .to eq(5357000) + expect("nine hundred and ninety nine trillion" .in_numbers) .to eq(999 * 10**12) end it "should handle negative numbers" do - "minus one" .in_numbers .should == -1 - "minus two googol" .in_numbers .should == -2 * 10**100 - "minus nine hundred and ninety nine trillion" .in_numbers .should == -999 * 10**12 + expect("minus one" .in_numbers) .to eq(-1) + expect("minus two googol" .in_numbers) .to eq(-2 * 10**100) + expect("minus nine hundred and ninety nine trillion" .in_numbers) .to eq(-999 * 10**12) end it "should ignore punctuation and capitalisation" do - "Minus one" .in_numbers .should == -1 - "FIVE Million, three hundred and fifty-seVen Thousand" .in_numbers .should == 5357000 - "FIVE,,./';';';[] Million, three hundred and fifty-seVen Thousand" .in_numbers .should == 5357000 + expect("Minus one" .in_numbers) .to eq(-1) + expect("FIVE Million, three hundred and fifty-seVen Thousand" .in_numbers) .to eq(5357000) + expect("FIVE,,./';';';[] Million, three hundred and fifty-seVen Thousand" .in_numbers) .to eq(5357000) end it "should handle decimal points" do - "one point one" .in_numbers .should == 1.1 + expect("one point one" .in_numbers) .to eq(1.1) - "zero point seven six five three four" .in_numbers .should == 0.76534 - "one trillion point six" .in_numbers .should == 10**12 + 0.6 + expect("zero point seven six five three four" .in_numbers) .to eq(0.76534) + expect("one trillion point six" .in_numbers) .to eq(10**12 + 0.6) long_number = <<-NUMBER nine duotrigintillion seven hundred and seventy seven untrigintillion @@ -124,21 +124,22 @@ million ninety three thousand seven hundred and ninety one point eight nine five six four three two one eight nine five six seven eight NUMBER - long_number.in_numbers.should == + expect(long_number.in_numbers).to eq( 9777059160806736471970632827836952710801948705683106707757426795746813127465237139153046752803093791.89564321895678 + ) - "seventy five point eight four three two seven six nine four five one eight". - in_numbers.should == 75.84327694518 + expect("seventy five point eight four three two seven six nine four five one eight". + in_numbers).to eq(75.84327694518) end it "should handle years notation" do - "fifteen sixteen".in_numbers .should == 1516 - "eighty five sixteen".in_numbers .should == 8516 - "nineteen ninety six".in_numbers .should == 1996 - "forty nine ninety eight forty seven seventy nine".in_numbers .should == 49984779 - "fifteen sixteen".in_numbers .should == 1516 - "fifteen sixteen seven".in_numbers .should == 15167 - "fifteen sixteen seventeen".in_numbers .should == 151617 + expect("fifteen sixteen".in_numbers) .to eq(1516) + expect("eighty five sixteen".in_numbers) .to eq(8516) + expect("nineteen ninety six".in_numbers) .to eq(1996) + expect("forty nine ninety eight forty seven seventy nine".in_numbers) .to eq(49984779) + expect("fifteen sixteen".in_numbers) .to eq(1516) + expect("fifteen sixteen seven".in_numbers) .to eq(15167) + expect("fifteen sixteen seventeen".in_numbers) .to eq(151617) end end diff --git a/spec/years_compress_spec.rb b/spec/years_compress_spec.rb index 69d1374..86e5cd4 100644 --- a/spec/years_compress_spec.rb +++ b/spec/years_compress_spec.rb @@ -2,12 +2,12 @@ describe WordsInNumbers do it "should handle years notation" do - "fifteen sixteen seventeen".num_compress .should == [15, 16, 17] - "forty nine ninety eight forty seven seventy nine".num_compress .should == [49, 98, 47, 79] - "sixty seven six".num_compress .should == [67, 6] - "one fifty".num_compress.should == [1, 50] - "two fifty".num_compress.should == [2, 50] - "one three forty seven".num_compress.should == [1, 3, 47] + expect("fifteen sixteen seventeen".num_compress) .to eq([15, 16, 17]) + expect("forty nine ninety eight forty seven seventy nine".num_compress) .to eq([49, 98, 47, 79]) + expect("sixty seven six".num_compress) .to eq([67, 6]) + expect("one fifty".num_compress).to eq([1, 50]) + expect("two fifty".num_compress).to eq([2, 50]) + expect("one three forty seven".num_compress).to eq([1, 3, 47]) end end diff --git a/spec/years_spec.rb b/spec/years_spec.rb index b0e3504..2be67ea 100644 --- a/spec/years_spec.rb +++ b/spec/years_spec.rb @@ -2,15 +2,15 @@ describe WordsInNumbers do it "should handle years notation" do - "fifteen sixteen seventeen".in_numbers .should == 151617 - "forty nine ninety eight forty seven seventy nine".in_numbers .should == 49984779 - "sixty seven six".in_numbers .should == 676 - "one fifty".in_numbers.should == 150 - "two fifty".in_numbers.should == 250 - "one point fifty six fifty seven".in_numbers.should == 1.5657 - "one three forty seven".in_numbers.should == 1347 - "one three five point forty seven".in_numbers.should == 135.47 - "one ten sixty three".in_numbers.should == 11063 + expect("fifteen sixteen seventeen".in_numbers) .to eq(151617) + expect("forty nine ninety eight forty seven seventy nine".in_numbers) .to eq(49984779) + expect("sixty seven six".in_numbers) .to eq(676) + expect("one fifty".in_numbers).to eq(150) + expect("two fifty".in_numbers).to eq(250) + expect("one point fifty six fifty seven".in_numbers).to eq(1.5657) + expect("one three forty seven".in_numbers).to eq(1347) + expect("one three five point forty seven".in_numbers).to eq(135.47) + expect("one ten sixty three".in_numbers).to eq(11063) end end From c0da9e441fe740e9ee0baeb0e5bd724cb67945a8 Mon Sep 17 00:00:00 2001 From: Dimid Duchovny Date: Tue, 27 Oct 2015 15:14:50 +0200 Subject: [PATCH 21/27] gem: add pry-nav Signed-off-by: Dimid Duchovny --- numbers_in_words.gemspec | 1 + 1 file changed, 1 insertion(+) diff --git a/numbers_in_words.gemspec b/numbers_in_words.gemspec index 76ac3cf..f11e8d2 100644 --- a/numbers_in_words.gemspec +++ b/numbers_in_words.gemspec @@ -16,6 +16,7 @@ Gem::Specification.new do |gem| gem.add_dependency "activesupport" gem.add_development_dependency "rspec", ">= 2.14" gem.add_development_dependency "pry" + gem.add_development_dependency "pry-nav" gem.files = `git ls-files`.split($/) gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) } From a9f61dd85d9c16c9a654ed2d99f00587dbe0e39c Mon Sep 17 00:00:00 2001 From: Dimid Duchovny Date: Tue, 27 Oct 2015 15:17:07 +0200 Subject: [PATCH 22/27] in_num: support 'one 10**n' Signed-off-by: Dimid Duchovny --- lib/numbers_in_words/to_number.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/numbers_in_words/to_number.rb b/lib/numbers_in_words/to_number.rb index 41eb0c5..1036761 100644 --- a/lib/numbers_in_words/to_number.rb +++ b/lib/numbers_in_words/to_number.rb @@ -28,6 +28,9 @@ def in_numbers(only_compress = false) mixed = text.match /^(-?\d+(.\d+)?) (hundred|thousand|million|billion|trillion)$/ return mixed[1].in_numbers * mixed[3].in_numbers if mixed && mixed[1] && mixed[3] + one = text.match /^one (hundred|thousand|million|billion|trillion)$/ + return only_compress ? [one[1].in_numbers] : one[1].in_numbers if one + i = handle_negative text return i if i From fe3e07825b2507764ec6586178cf6976a061c5b9 Mon Sep 17 00:00:00 2001 From: Dimid Duchovny Date: Tue, 27 Oct 2015 15:17:36 +0200 Subject: [PATCH 23/27] spec: fix WS Signed-off-by: Dimid Duchovny --- Gemfile.lock | 3 +++ spec/years_compress_spec.rb | 9 +++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 29bd087..a53b3e8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -23,6 +23,8 @@ GEM coderay (~> 1.1.0) method_source (~> 0.8.1) slop (~> 3.4) + pry-nav (0.2.4) + pry (>= 0.9.10, < 0.11.0) rspec (3.3.0) rspec-core (~> 3.3.0) rspec-expectations (~> 3.3.0) @@ -47,6 +49,7 @@ PLATFORMS DEPENDENCIES numbers_in_words! pry + pry-nav rspec (>= 2.14) BUNDLED WITH diff --git a/spec/years_compress_spec.rb b/spec/years_compress_spec.rb index 86e5cd4..ddb31a8 100644 --- a/spec/years_compress_spec.rb +++ b/spec/years_compress_spec.rb @@ -4,10 +4,11 @@ it "should handle years notation" do expect("fifteen sixteen seventeen".num_compress) .to eq([15, 16, 17]) expect("forty nine ninety eight forty seven seventy nine".num_compress) .to eq([49, 98, 47, 79]) - expect("sixty seven six".num_compress) .to eq([67, 6]) - expect("one fifty".num_compress).to eq([1, 50]) - expect("two fifty".num_compress).to eq([2, 50]) - expect("one three forty seven".num_compress).to eq([1, 3, 47]) + expect("sixty seven six".num_compress) .to eq([67, 6]) + expect("one fifty".num_compress).to eq([1, 50]) + expect("two fifty".num_compress).to eq([2, 50]) + expect("one three forty seven".num_compress).to eq([1, 3, 47]) + expect("one hundred".num_compress).to eq([100]) end end From 337c6acfb8d777a1990abc8dd82178f21e6e637d Mon Sep 17 00:00:00 2001 From: Dimid Duchovny Date: Tue, 27 Oct 2015 17:21:13 +0200 Subject: [PATCH 24/27] in_num: fix negative num_compress Signed-off-by: Dimid Duchovny --- lib/numbers_in_words/duck_punch.rb | 4 ++-- lib/numbers_in_words/to_number.rb | 14 +++++++++----- spec/years_compress_spec.rb | 1 + 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/numbers_in_words/duck_punch.rb b/lib/numbers_in_words/duck_punch.rb index 5f2514b..1d0bc95 100644 --- a/lib/numbers_in_words/duck_punch.rb +++ b/lib/numbers_in_words/duck_punch.rb @@ -5,8 +5,8 @@ def in_words language=NumbersInWords.language end module WordsInNumbers - def in_numbers language=NumbersInWords.language - NumbersInWords::ToNumber.new(self, language).in_numbers + def in_numbers(only_compress = false, language=NumbersInWords.language) + NumbersInWords::ToNumber.new(self, language).in_numbers(only_compress) end def num_compress language=NumbersInWords.language diff --git a/lib/numbers_in_words/to_number.rb b/lib/numbers_in_words/to_number.rb index 1036761..0da1a1a 100644 --- a/lib/numbers_in_words/to_number.rb +++ b/lib/numbers_in_words/to_number.rb @@ -16,8 +16,11 @@ def language end end - def handle_negative text - -1 * (text.gsub(/^minus /, "")).in_numbers if text =~ /^minus / + def handle_negative(text, only_compress) + if text =~ /^minus/ + stripped = text.gsub(/^minus /, "").in_numbers(only_compress) + only_compress ? stripped.map{ |k| k * -1 } : -1 * stripped + end end def in_numbers(only_compress = false) @@ -25,15 +28,16 @@ def in_numbers(only_compress = false) return text.to_f if text =~ /^-?\d+(.\d+)?$/ text = strip_punctuation text + + i = handle_negative(text, only_compress) + return i if i + mixed = text.match /^(-?\d+(.\d+)?) (hundred|thousand|million|billion|trillion)$/ return mixed[1].in_numbers * mixed[3].in_numbers if mixed && mixed[1] && mixed[3] one = text.match /^one (hundred|thousand|million|billion|trillion)$/ return only_compress ? [one[1].in_numbers] : one[1].in_numbers if one - i = handle_negative text - return i if i - h = handle_decimals text return h if h diff --git a/spec/years_compress_spec.rb b/spec/years_compress_spec.rb index ddb31a8..d897cb7 100644 --- a/spec/years_compress_spec.rb +++ b/spec/years_compress_spec.rb @@ -9,6 +9,7 @@ expect("two fifty".num_compress).to eq([2, 50]) expect("one three forty seven".num_compress).to eq([1, 3, 47]) expect("one hundred".num_compress).to eq([100]) + expect("minus one hundred".num_compress).to eq([-100]) end end From ceded97f373c137c9f4b60457284065af310b765 Mon Sep 17 00:00:00 2001 From: Dimid Duchovny Date: Tue, 27 Oct 2015 17:32:47 +0200 Subject: [PATCH 25/27] in_num: strip trailing WS from text Signed-off-by: Dimid Duchovny --- lib/numbers_in_words/to_number.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/numbers_in_words/to_number.rb b/lib/numbers_in_words/to_number.rb index 0da1a1a..4fcfaf1 100644 --- a/lib/numbers_in_words/to_number.rb +++ b/lib/numbers_in_words/to_number.rb @@ -24,7 +24,7 @@ def handle_negative(text, only_compress) end def in_numbers(only_compress = false) - text = to_s + text = to_s.strip return text.to_f if text =~ /^-?\d+(.\d+)?$/ text = strip_punctuation text From 7002c91d2b3903493526a252ea84073cb6fdda24 Mon Sep 17 00:00:00 2001 From: Dimid Duchovny Date: Wed, 28 Oct 2015 14:23:48 +0200 Subject: [PATCH 26/27] in_num: fix 'minus' Signed-off-by: Dimid Duchovny --- lib/numbers_in_words/to_number.rb | 5 +++-- spec/years_compress_spec.rb | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/numbers_in_words/to_number.rb b/lib/numbers_in_words/to_number.rb index 4fcfaf1..2d1362c 100644 --- a/lib/numbers_in_words/to_number.rb +++ b/lib/numbers_in_words/to_number.rb @@ -18,8 +18,9 @@ def language def handle_negative(text, only_compress) if text =~ /^minus/ - stripped = text.gsub(/^minus /, "").in_numbers(only_compress) - only_compress ? stripped.map{ |k| k * -1 } : -1 * stripped + stripped = text.gsub(/^minus/, "") + stripped_n = stripped.in_numbers(only_compress) + only_compress ? stripped_n.map{ |k| k * -1 } : -1 * stripped_n end end diff --git a/spec/years_compress_spec.rb b/spec/years_compress_spec.rb index d897cb7..7c7a77e 100644 --- a/spec/years_compress_spec.rb +++ b/spec/years_compress_spec.rb @@ -10,6 +10,7 @@ expect("one three forty seven".num_compress).to eq([1, 3, 47]) expect("one hundred".num_compress).to eq([100]) expect("minus one hundred".num_compress).to eq([-100]) + expect("minus".num_compress).to eq([]) end end From 060e6027be1facbc606b28fd2c288a1966b58199 Mon Sep 17 00:00:00 2001 From: Mark Burns Date: Mon, 30 Nov 2015 12:39:13 +0900 Subject: [PATCH 27/27] fix non monkey patched version --- lib/numbers_in_words.rb | 8 ++++---- lib/numbers_in_words/to_number.rb | 20 +++++++++++++++----- spec/non_monkey_patch_spec.rb | 6 ++++++ 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/lib/numbers_in_words.rb b/lib/numbers_in_words.rb index e004d3b..8dcc85f 100644 --- a/lib/numbers_in_words.rb +++ b/lib/numbers_in_words.rb @@ -21,12 +21,12 @@ def language @language ||= "English" end - def in_words(i, language=NumbersInWords.language) - NumbersInWords::ToWord.new(i, language).in_words + def in_words(i, language=NumbersInWords.language, only_compress=false) + NumbersInWords::ToWord.new(i, language).in_words(only_compress) end - def in_numbers(s, language=NumbersInWords.language) - NumbersInWords::ToNumber.new(s, language).in_numbers + def in_numbers(s, language=NumbersInWords.language, only_compress=false) + NumbersInWords::ToNumber.new(s, language).in_numbers(only_compress) end end end diff --git a/lib/numbers_in_words/to_number.rb b/lib/numbers_in_words/to_number.rb index 2d1362c..d637ebf 100644 --- a/lib/numbers_in_words/to_number.rb +++ b/lib/numbers_in_words/to_number.rb @@ -19,7 +19,7 @@ def language def handle_negative(text, only_compress) if text =~ /^minus/ stripped = text.gsub(/^minus/, "") - stripped_n = stripped.in_numbers(only_compress) + stripped_n = NumbersInWords.in_numbers(stripped, language, only_compress) only_compress ? stripped_n.map{ |k| k * -1 } : -1 * stripped_n end end @@ -34,10 +34,19 @@ def in_numbers(only_compress = false) return i if i mixed = text.match /^(-?\d+(.\d+)?) (hundred|thousand|million|billion|trillion)$/ - return mixed[1].in_numbers * mixed[3].in_numbers if mixed && mixed[1] && mixed[3] + + if mixed && mixed[1] && mixed[3] + third_match = NumbersInWords.in_numbers(mixed[3]) + first_match = NumbersInWords.in_numbers(mixed[1]) + return first_match * third_match + end one = text.match /^one (hundred|thousand|million|billion|trillion)$/ - return only_compress ? [one[1].in_numbers] : one[1].in_numbers if one + + if one + first_match = NumbersInWords.in_numbers(one[1]) + return only_compress ? [first_match] : first_match + end h = handle_decimals text return h if h @@ -59,8 +68,9 @@ def strip_punctuation text def handle_decimals text match = text.match(/\spoint\s/) if match - integer = match.pre_match.in_numbers - integer += ("0." + match.post_match.in_numbers.to_s).to_f + integer = NumbersInWords.in_numbers(match.pre_match) + decimal = NumbersInWords.in_numbers(match.post_match) + integer += "0.#{decimal}".to_f end end diff --git a/spec/non_monkey_patch_spec.rb b/spec/non_monkey_patch_spec.rb index 5af790d..7b433dc 100644 --- a/spec/non_monkey_patch_spec.rb +++ b/spec/non_monkey_patch_spec.rb @@ -19,6 +19,12 @@ describe ".in_numbers" do it do expect(NumbersInWords.in_numbers("one hundred")).to eq 100 + + expect(NumbersInWords.in_numbers("minus one hundred")).to eq -100 + expect(NumbersInWords.in_numbers("twenty four" )).to eq 24 + expect(NumbersInWords.in_numbers("one point two")).to eq 1.2 + expect(NumbersInWords.in_numbers("one hundred googol")).to eq 100*10**100 + expect(NumbersInWords.in_numbers("one hundred googol and thirty")).to eq 30 + 100*10**100 end end