dot_number 0.0.1 → 0.0.3

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/text_to_number.rb +34 -26
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ae9dc53cb240c2ecf4cd40019cd9ee5d9bb33217ee8128b552807994e2e347c6
4
- data.tar.gz: a5cb3fb5769196f5c5d31226a9b8b5c4e8f5ae6b2ef14b145c793edee4a82400
3
+ metadata.gz: 40a7a3a3950891f6d0627e5afd2fc6bb388a91d3911984e428bea39cfa490367
4
+ data.tar.gz: 6238ac3fc0e9ba5fbe8a578be6b09c91e82b707ecf46ee2fc948da37d0ac3a56
5
5
  SHA512:
6
- metadata.gz: 7d52df9aa6ab1c3ba738c8bfce4eeddac8b0aa1105343fd786fa785787627d464f2c477379b71e134c728f39f37494b6776b26ca196813225554d81f49e8d77b
7
- data.tar.gz: 11e352eb200e6d3a9df55d4bfe1fc3bc8f76cb4e58d83ab13d44c5db72fb1c9507a2e87ce06019e7cad534f618295658d5ad7d1c6e4327051acfa966ce18400d
6
+ metadata.gz: 1a15e2d3e20ce6258fb79ebe58758c58243f93419bab60c866072f1963a389b82689fe98d7aab01ea345abc0ffab3dca6ea766be9e6de91ff5a8e098c21de4c9
7
+ data.tar.gz: d8ad72bca903b023ff6291e1123030bde411123a92eaf7d8cf55d65af3531993fd6879c5963b1ddae52754a49d8f88f7651be5fdc0d19f9d60d62276e7624e6c
@@ -12,26 +12,20 @@ See the License for the specific language governing permissions and
12
12
  limitations under the License.
13
13
  '''
14
14
 
15
- def text_to_place_value(t)
16
- placevalues = {
17
- "thous" => 1000, # "thous" because "and" is removed
18
- "million" => 10**6,
19
- "billion" => 10**9,
20
- "trillion" => 10**12,
21
- "quadrillion" => 10**15,
22
- "quintillion" => 10**18,
23
- "sextillion" => 10**21,
24
- "septillion" => 10**24,
25
- "octillion" => 10**27,
26
- "nonillion" => 10**30,
27
- "decillion" => 10**33,
28
- "" => 1
29
- }
30
- return 1 if t == nil
31
- baseplace = t.start_with?("hundred") ? 100 : 1
32
- t.gsub! "hundred",""
33
- raise ArgumentError.new("Could not interpret \"#{t}\"") unless placevalues.has_key? t
34
- baseplace * placevalues[t]
15
+
16
+ def text_to_small_number(t)
17
+ ##
18
+ # Convert text representing a number between zero and 999 to integer
19
+ smallestnumbers = {"ninetynine"=>99, "ninetyeight"=>98, "ninetyseven"=>97, "ninetysix"=>96, "ninetyfive"=>95, "ninetyfour"=>94, "ninetythree"=>93, "ninetytwo"=>92, "ninetyone"=>91, "ninety"=>90, "eightynine"=>89, "eightyeight"=>88, "eightyseven"=>87, "eightysix"=>86, "eightyfive"=>85, "eightyfour"=>84, "eightythree"=>83, "eightytwo"=>82, "eightyone"=>81, "eighty"=>80, "seventynine"=>79, "seventyeight"=>78, "seventyseven"=>77, "seventysix"=>76, "seventyfive"=>75, "seventyfour"=>74, "seventythree"=>73, "seventytwo"=>72, "seventyone"=>71, "seventy"=>70, "sixtynine"=>69, "sixtyeight"=>68, "sixtyseven"=>67, "sixtysix"=>66, "sixtyfive"=>65, "sixtyfour"=>64, "sixtythree"=>63, "sixtytwo"=>62, "sixtyone"=>61, "sixty"=>60, "fiftynine"=>59, "fiftyeight"=>58, "fiftyseven"=>57, "fiftysix"=>56, "fiftyfive"=>55, "fiftyfour"=>54, "fiftythree"=>53, "fiftytwo"=>52, "fiftyone"=>51, "fifty"=>50, "fortynine"=>49, "fortyeight"=>48, "fortyseven"=>47, "fortysix"=>46, "fortyfive"=>45, "fortyfour"=>44, "fortythree"=>43, "fortytwo"=>42, "fortyone"=>41, "forty"=>40, "thirtynine"=>39, "thirtyeight"=>38, "thirtyseven"=>37, "thirtysix"=>36, "thirtyfive"=>35, "thirtyfour"=>34, "thirtythree"=>33, "thirtytwo"=>32, "thirtyone"=>31, "thirty"=>30, "twentynine"=>29, "twentyeight"=>28, "twentyseven"=>27, "twentysix"=>26, "twentyfive"=>25, "twentyfour"=>24, "twentythree"=>23, "twentytwo"=>22, "twentyone"=>21, "twenty"=>20, "nineteen"=>19, "eighteen"=>18, "seventeen"=>17, "sixteen"=>16, "fifteen"=>15, "fourteen"=>14, "thirteen"=>13, "twelve"=>12, "eleven"=>11, "ten"=>10, "nine"=>9, "eight"=>8, "seven"=>7, "six"=>6, "five"=>5, "four"=>4, "three"=>3, "two"=>2, "one"=>1, "zero"=>0}
20
+ if t.include? "hundred"
21
+ s = t.split("hundred")
22
+ total = text_to_small_number(s[0]) * 100
23
+ total += text_to_small_number(s[1]) unless s.length == 1
24
+ return total
25
+ end
26
+
27
+ raise ArgumentError.new() if smallestnumbers[t] == nil
28
+ return smallestnumbers[t]
35
29
  end
36
30
 
37
31
 
@@ -44,17 +38,31 @@ def text_to_number(t)
44
38
 
45
39
  total = 0
46
40
  segment(t).each do |seg|
47
- total += seg[0].to_i * text_to_place_value(seg[1])
41
+ total += text_to_small_number(seg[0]) * seg[1]
48
42
  end
49
43
  negative ? -total : total
50
44
  end
51
45
 
52
46
  def segment(t)
53
- smallnumbers = {"ninetynine"=>99, "ninetyeight"=>98, "ninetyseven"=>97, "ninetysix"=>96, "ninetyfive"=>95, "ninetyfour"=>94, "ninetythree"=>93, "ninetytwo"=>92, "ninetyone"=>91, "ninety"=>90, "eightynine"=>89, "eightyeight"=>88, "eightyseven"=>87, "eightysix"=>86, "eightyfive"=>85, "eightyfour"=>84, "eightythree"=>83, "eightytwo"=>82, "eightyone"=>81, "eighty"=>80, "seventynine"=>79, "seventyeight"=>78, "seventyseven"=>77, "seventysix"=>76, "seventyfive"=>75, "seventyfour"=>74, "seventythree"=>73, "seventytwo"=>72, "seventyone"=>71, "seventy"=>70, "sixtynine"=>69, "sixtyeight"=>68, "sixtyseven"=>67, "sixtysix"=>66, "sixtyfive"=>65, "sixtyfour"=>64, "sixtythree"=>63, "sixtytwo"=>62, "sixtyone"=>61, "sixty"=>60, "fiftynine"=>59, "fiftyeight"=>58, "fiftyseven"=>57, "fiftysix"=>56, "fiftyfive"=>55, "fiftyfour"=>54, "fiftythree"=>53, "fiftytwo"=>52, "fiftyone"=>51, "fifty"=>50, "fortynine"=>49, "fortyeight"=>48, "fortyseven"=>47, "fortysix"=>46, "fortyfive"=>45, "fortyfour"=>44, "fortythree"=>43, "fortytwo"=>42, "fortyone"=>41, "forty"=>40, "thirtynine"=>39, "thirtyeight"=>38, "thirtyseven"=>37, "thirtysix"=>36, "thirtyfive"=>35, "thirtyfour"=>34, "thirtythree"=>33, "thirtytwo"=>32, "thirtyone"=>31, "thirty"=>30, "twentynine"=>29, "twentyeight"=>28, "twentyseven"=>27, "twentysix"=>26, "twentyfive"=>25, "twentyfour"=>24, "twentythree"=>23, "twentytwo"=>22, "twentyone"=>21, "twenty"=>20, "nineteen"=>19, "eighteen"=>18, "seventeen"=>17, "sixteen"=>16, "fifteen"=>15, "fourteen"=>14, "thirteen"=>13, "twelve"=>12, "eleven"=>11, "ten"=>10, "nine"=>9, "eight"=>8, "seven"=>7, "six"=>6, "five"=>5, "four"=>4, "three"=>3, "two"=>2, "one"=>1, "zero"=>0}
54
- t_old = t.clone
55
- smallnumbers.each do |key, value|
47
+ placevalues = {
48
+ "thous" => 1000, # "thous" because "and" is removed
49
+ "million" => 10**6,
50
+ "billion" => 10**9,
51
+ "trillion" => 10**12,
52
+ "quadrillion" => 10**15,
53
+ "quintillion" => 10**18,
54
+ "sextillion" => 10**21,
55
+ "septillion" => 10**24,
56
+ "octillion" => 10**27,
57
+ "nonillion" => 10**30,
58
+ "decillion" => 10**33,
59
+ }
60
+
61
+ placevalues.each do |key, value|
56
62
  t.gsub! key,"&#{value.to_s}&"
57
63
  end
58
- raise ArgumentError.new("Not a number") if t_old == t
59
- t.split("&")[1..].each_slice(2).map{|a,b| [a,b]}
64
+
65
+ s = t.split("&").each_slice(2).map{|a,b| [a,b.to_i]}
66
+ s[-1][1] = 1 if s[-1][1] == 0
67
+ s
60
68
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dot_number
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - bowstones