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.
- checksums.yaml +4 -4
- data/lib/text_to_number.rb +31 -24
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e7daa1c8d8683ead6bc30fd78876fd251c6637a824840f62b32bb668cb09a57e
|
4
|
+
data.tar.gz: bf65fa5913d2fc9010be77ec1b898cf012cc731ca070ee3f3400e3f542646cb8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c4ff2344bb749c66e8823a5c7e675d66954e8fc313d558c0e21b85fc3ce2b12104233f95b8bbb263465df0f5fad52094260477ea98d72e039aa5b10a92589544
|
7
|
+
data.tar.gz: 820f1839c1be3b9bd0957b214b3d21e044ce58331064454b56489b90faa09f5f9d6ce8faaaa435920873e74cecc8c643decb78e9fabd34e0a682644b4b073996
|
data/lib/text_to_number.rb
CHANGED
@@ -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
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
"
|
20
|
-
"
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
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]
|
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
|
-
|
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
|
-
|
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("&")
|
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
|