dot_number 0.0.1 → 0.0.2

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 +31 -24
  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: e7daa1c8d8683ead6bc30fd78876fd251c6637a824840f62b32bb668cb09a57e
4
+ data.tar.gz: bf65fa5913d2fc9010be77ec1b898cf012cc731ca070ee3f3400e3f542646cb8
5
5
  SHA512:
6
- metadata.gz: 7d52df9aa6ab1c3ba738c8bfce4eeddac8b0aa1105343fd786fa785787627d464f2c477379b71e134c728f39f37494b6776b26ca196813225554d81f49e8d77b
7
- data.tar.gz: 11e352eb200e6d3a9df55d4bfe1fc3bc8f76cb4e58d83ab13d44c5db72fb1c9507a2e87ce06019e7cad534f618295658d5ad7d1c6e4327051acfa966ce18400d
6
+ metadata.gz: c4ff2344bb749c66e8823a5c7e675d66954e8fc313d558c0e21b85fc3ce2b12104233f95b8bbb263465df0f5fad52094260477ea98d72e039aa5b10a92589544
7
+ data.tar.gz: 820f1839c1be3b9bd0957b214b3d21e044ce58331064454b56489b90faa09f5f9d6ce8faaaa435920873e74cecc8c643decb78e9fabd34e0a682644b4b073996
@@ -12,26 +12,19 @@ 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
+ return smallestnumbers[t]
35
28
  end
36
29
 
37
30
 
@@ -44,17 +37,31 @@ def text_to_number(t)
44
37
 
45
38
  total = 0
46
39
  segment(t).each do |seg|
47
- total += seg[0].to_i * text_to_place_value(seg[1])
40
+ total += text_to_small_number(seg[0]) * seg[1]
48
41
  end
49
42
  negative ? -total : total
50
43
  end
51
44
 
52
45
  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}
46
+ placevalues = {
47
+ "thous" => 1000, # "thous" because "and" is removed
48
+ "million" => 10**6,
49
+ "billion" => 10**9,
50
+ "trillion" => 10**12,
51
+ "quadrillion" => 10**15,
52
+ "quintillion" => 10**18,
53
+ "sextillion" => 10**21,
54
+ "septillion" => 10**24,
55
+ "octillion" => 10**27,
56
+ "nonillion" => 10**30,
57
+ "decillion" => 10**33,
58
+ }
54
59
  t_old = t.clone
55
- smallnumbers.each do |key, value|
60
+ placevalues.each do |key, value|
56
61
  t.gsub! key,"&#{value.to_s}&"
57
62
  end
58
63
  raise ArgumentError.new("Not a number") if t_old == t
59
- t.split("&")[1..].each_slice(2).map{|a,b| [a,b]}
64
+ s = t.split("&").each_slice(2).map{|a,b| [a,b.to_i]}
65
+ s[-1][1] = 1 if s[-1][1] == 0
66
+ s
60
67
  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.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - bowstones