words_to_number 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/words_to_number/parser.rb +57 -0
- data/lib/words_to_number/version.rb +5 -0
- data/lib/words_to_number.rb +11 -0
- metadata +45 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 4cf82c4005d67282488e4d1d2573d01e01d7ef97957ee1c68f624a8da4f0d47e
|
|
4
|
+
data.tar.gz: fc9cb3725d5629accc5bcc84da98111c0330e2285cf55ac9e0e98bd311bd58a0
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 6aa1ee82e2f810c082fab5d89412d565eb44ad144d00b1bd3b39afdfafedf7e387793a82258860be8211a4a1f0e8b3c68b1105d67b85c5b33630e5e30dfbd319
|
|
7
|
+
data.tar.gz: b59d3e22423a0a8963cf82d8e8761aa7413313dc899a151d8231cd90f6b89623728f590ce93df1e68b1e7932b2ee890fd55b6114e1ce259c61c0cba18efee718
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
module WordsToNumber
|
|
2
|
+
class Parser
|
|
3
|
+
UNITS = {
|
|
4
|
+
"zero" => 0, "one" => 1, "two" => 2, "three" => 3, "four" => 4,
|
|
5
|
+
"five" => 5, "six" => 6, "seven" => 7, "eight" => 8, "nine" => 9,
|
|
6
|
+
"ten" => 10, "eleven" => 11, "twelve" => 12, "thirteen" => 13,
|
|
7
|
+
"fourteen" => 14, "fifteen" => 15, "sixteen" => 16,
|
|
8
|
+
"seventeen" => 17, "eighteen" => 18, "nineteen" => 19
|
|
9
|
+
}.freeze
|
|
10
|
+
|
|
11
|
+
TENS = {
|
|
12
|
+
"twenty" => 20, "thirty" => 30, "forty" => 40,
|
|
13
|
+
"fifty" => 50, "sixty" => 60, "seventy" => 70,
|
|
14
|
+
"eighty" => 80, "ninety" => 90
|
|
15
|
+
}.freeze
|
|
16
|
+
|
|
17
|
+
MULTIPLIERS = {
|
|
18
|
+
"hundred" => 100,
|
|
19
|
+
"thousand" => 1_000,
|
|
20
|
+
"million" => 1_000_000
|
|
21
|
+
}.freeze
|
|
22
|
+
|
|
23
|
+
def initialize(input)
|
|
24
|
+
@words = normalize(input)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def call
|
|
28
|
+
total = 0
|
|
29
|
+
current = 0
|
|
30
|
+
|
|
31
|
+
@words.each do |word|
|
|
32
|
+
next if word == "and"
|
|
33
|
+
|
|
34
|
+
if UNITS.key?(word)
|
|
35
|
+
current += UNITS[word]
|
|
36
|
+
elsif TENS.key?(word)
|
|
37
|
+
current += TENS[word]
|
|
38
|
+
elsif word == "hundred"
|
|
39
|
+
current *= 100
|
|
40
|
+
elsif word == "thousand" || word == "million"
|
|
41
|
+
total += current * MULTIPLIERS[word]
|
|
42
|
+
current = 0
|
|
43
|
+
else
|
|
44
|
+
raise ArgumentError, "Invalid word: #{word}"
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
total + current
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
private
|
|
52
|
+
|
|
53
|
+
def normalize(str)
|
|
54
|
+
str.downcase.gsub("-", " ").split
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: words_to_number
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Kushal Mistry
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
12
|
+
description: Parses English number words into integers up to one million
|
|
13
|
+
email:
|
|
14
|
+
- kushal_mistry@outlook.com
|
|
15
|
+
executables: []
|
|
16
|
+
extensions: []
|
|
17
|
+
extra_rdoc_files: []
|
|
18
|
+
files:
|
|
19
|
+
- lib/words_to_number.rb
|
|
20
|
+
- lib/words_to_number/parser.rb
|
|
21
|
+
- lib/words_to_number/version.rb
|
|
22
|
+
homepage: https://github.com/mistrikushal/words_to_number
|
|
23
|
+
licenses: []
|
|
24
|
+
metadata:
|
|
25
|
+
allowed_push_host: https://rubygems.org
|
|
26
|
+
homepage_uri: https://github.com/mistrikushal/words_to_number
|
|
27
|
+
source_code_uri: https://github.com/mistrikushal/words_to_number
|
|
28
|
+
rdoc_options: []
|
|
29
|
+
require_paths:
|
|
30
|
+
- lib
|
|
31
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
32
|
+
requirements:
|
|
33
|
+
- - ">="
|
|
34
|
+
- !ruby/object:Gem::Version
|
|
35
|
+
version: '2.6'
|
|
36
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - ">="
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '0'
|
|
41
|
+
requirements: []
|
|
42
|
+
rubygems_version: 4.0.9
|
|
43
|
+
specification_version: 4
|
|
44
|
+
summary: Convert numbers written in words to integers
|
|
45
|
+
test_files: []
|