numeja 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 520ab7906a5d82346ed2ee353f58cae06fd9b1c0
4
- data.tar.gz: b8c3922301bd727522750c49ee8d83cdf5991da5
3
+ metadata.gz: 9f6217c15351374c091ab0e636f7e921a1782cdc
4
+ data.tar.gz: 7925c6b89c8043c8c0a68b51d37132e95c9316e4
5
5
  SHA512:
6
- metadata.gz: '097c234b903715bfd8ca46d8e2b9b7582f23f05606834c5ef2114d70ae2bd6b8502cd89c56c4b562ee10623fb46f7a3eb501a0aa19a010ef549cfcd84722f32d'
7
- data.tar.gz: 139724b38040b64f8742876a713e678435e2acc2da6695049c31c68044e2bc0741448499233f5d015889c60296116b52a291c1660d314e7593130ae46ed1b2a9
6
+ metadata.gz: fab32b436ef7926fadc16896729069c401825e2ea61f973f87e43012415167dabb05f1a542837b56882ea9cc0417e068b9a25b9333693c69cd712dcdd6e0aa52
7
+ data.tar.gz: 32066e7eb61577c3a7d2cfbc1938295ee417b61a9dc804a5895f4e4dceabff2d8761c7267ef4b2bb568b977d7df6b18ebcb8a09599d9209cc8a707274d18333b
data/.gitignore CHANGED
@@ -6,4 +6,5 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
- /vendor/
9
+ /vendor/
10
+ Gemfile.lock
data/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  Parse numbers in japanese numerals from String
4
4
 
5
+ 漢数字とかをInteger or Floatに変換するGem
6
+
5
7
  ## Installation
6
8
 
7
9
  Add this line to your application's Gemfile:
@@ -16,7 +18,7 @@ And then execute:
16
18
 
17
19
  Or install it yourself as:
18
20
 
19
- $ gem install ima
21
+ $ gem install numeja
20
22
 
21
23
  ## Usage
22
24
 
@@ -32,5 +34,7 @@ Numeja.numerize('505') # => 505
32
34
  Numeja.numerize('九八七六五四三二一零') # => 9_876_543_210
33
35
  Numeja.numerize('九分八厘七毛六糸五忽四微三繊二沙零塵一埃') # => 0.9876543201
34
36
  Numeja.numerize('九割八分七厘六毛五糸四忽三微二繊零沙一塵') # => 0.9876543201
37
+ Numeja.numerize('二分の一') # => 0.5
38
+ Numeja.numerize('三分の一') # => 0.33333333333...
35
39
 
36
40
  ```
@@ -1,4 +1,6 @@
1
1
  require 'numeja/version'
2
+ require 'delegate'
3
+ require 'numeja/number'
2
4
 
3
5
  module Numeja
4
6
  JA_大字 = {
@@ -46,16 +48,16 @@ module Numeja
46
48
  }.freeze
47
49
 
48
50
  JA_下数 = {
49
- '十' => 10**1,
50
- '百' => 10**2,
51
- '千' => 10**3
51
+ '十' => 10 ** 1,
52
+ '百' => 10 ** 2,
53
+ '千' => 10 ** 3
52
54
  }.freeze
53
55
 
54
56
  JA_上数 = {
55
- '万' => 10**4,
56
- '億' => 10**8,
57
- '兆' => 10**12,
58
- '京' => 10**16,
57
+ '万' => 10 ** 4,
58
+ '億' => 10 ** 8,
59
+ '兆' => 10 ** 12,
60
+ '京' => 10 ** 16,
59
61
  # '垓': 10 ** 20,
60
62
  # '秭': 10 ** 24,
61
63
  # '𥝱': 10 ** 24,
@@ -73,29 +75,29 @@ module Numeja
73
75
  }.freeze
74
76
 
75
77
  JA_小数 = {
76
- '分' => 10**-1,
77
- '厘' => 10**-2,
78
- '毛' => 10**-3,
79
- '糸' => 10**-4,
80
- '忽' => 10**-5,
81
- '微' => 10**-6,
82
- '繊' => 10**-7,
83
- '沙' => 10**-8,
84
- '塵' => 10**-9,
85
- '埃' => 10**-10
78
+ '分' => 10 ** -1,
79
+ '厘' => 10 ** -2,
80
+ '毛' => 10 ** -3,
81
+ '糸' => 10 ** -4,
82
+ '忽' => 10 ** -5,
83
+ '微' => 10 ** -6,
84
+ '繊' => 10 ** -7,
85
+ '沙' => 10 ** -8,
86
+ '塵' => 10 ** -9,
87
+ '埃' => 10 ** -10
86
88
  }.freeze
87
89
 
88
90
  JA_割合用小数 = {
89
- '割' => 10**-1,
90
- '分' => 10**-2,
91
- '厘' => 10**-3,
92
- '毛' => 10**-4,
93
- '糸' => 10**-5,
94
- '忽' => 10**-6,
95
- '微' => 10**-7,
96
- '繊' => 10**-8,
97
- '沙' => 10**-9,
98
- '塵' => 10**-10
91
+ '割' => 10 ** -1,
92
+ '分' => 10 ** -2,
93
+ '厘' => 10 ** -3,
94
+ '毛' => 10 ** -4,
95
+ '糸' => 10 ** -5,
96
+ '忽' => 10 ** -6,
97
+ '微' => 10 ** -7,
98
+ '繊' => 10 ** -8,
99
+ '沙' => 10 ** -9,
100
+ '塵' => 10 ** -10
99
101
  }.freeze
100
102
  REGEXP_旧字 = Regexp.new(Numeja::JA_大字.keys.join('|').to_s, 'g').freeze
101
103
  REGEXP_数字 = Regexp.new(Numeja::JA_数字.keys.join('|').to_s, 'g').freeze
@@ -119,128 +121,17 @@ module Numeja
119
121
  # Numeja.numerize('玖阡捌陌漆拾陸億伍仟肆佰參十貳万壹千弐百卅4') => 987_654_321_234
120
122
  # Numeja.numerize('505') => 505
121
123
  # Numeja.numerize('九八七六五四三二一零') => 9_876_543_210
122
- # Numeja.numerize('九分八厘七毛六糸五忽四微三繊二沙零塵一埃') => 0.9876543201)
123
- # Numeja.numerize('九割八分七厘六毛五糸四忽三微二繊零沙一塵') => 0.9876543201)
124
+ # Numeja.numerize('九分八厘七毛六糸五忽四微三繊二沙零塵一埃') => 0.9876543201
125
+ # Numeja.numerize('九割八分七厘六毛五糸四忽三微二繊零沙一塵') => 0.9876543201
126
+ # Numeja.numerize('二分の一') => 0.5
127
+ # Numeja.numerize('三分の一') => 0.33333333333...
124
128
  def numerize(str)
125
- nomarized = nomarize(str)
126
- units = parse_large_numbers(nomarized)
127
- units.reduce(0) do |t, unit|
128
- large_prefix = JA_上数[unit[:digit]]
129
- if large_prefix.nil?
130
- integer_part, decimal_part = split_digit(unit[:unit])
131
- int = integer_part.nil? ? 0 : parse_integer_part(integer_part)
132
- decimal = decimal_part.nil? ? 0 : parse_decimal_part(decimal_part)
133
- t += int + decimal
134
- else
135
- integer = parse_integer_part(unit[:unit])
136
- t += integer * large_prefix
137
- end
138
- t
139
- end
140
- end
141
-
142
- private
143
-
144
- # Return numerals parts
145
- #
146
- # @return [Array<String>]
147
- #
148
- # @api private
149
- # @since 0.0.1
150
- def split_digit(str)
151
- i = str.index(/.分|.割/)
152
- if i.nil?
153
- [str]
154
- elsif i.zero?
155
- [nil, str]
129
+ if str.index('分の').nil?
130
+ Numeja::Number.new(str)
156
131
  else
157
- [str[0..i - 1], str[i..-1]]
158
- end
159
- end
160
-
161
- # Return nomarized Kansuuji
162
- #
163
- # @return [::String]
164
- #
165
- # @api private
166
- # @since 0.0.1
167
- def nomarize(str)
168
- str
169
- .tr('0-9', '0-9')
170
- .gsub(REGEXP_旧字, JA_大字)
171
- .gsub(REGEXP_数字, JA_数字)
172
- end
173
-
174
- # Return nomarized Kansuuji
175
- #
176
- # @return [::String]
177
- #
178
- # @api private
179
- # @since 0.0.1
180
- def parse_integer_part(input)
181
- total = 0
182
- tmp = 0
183
- return input.to_i if input =~ /^[0-9]+$/
184
- input.gsub(REGEXP_数字, JA_数字).each_char do |s|
185
- unit_prefix = JA_下数[s]
186
- unless unit_prefix.nil?
187
- if tmp > 0
188
- total += tmp * unit_prefix
189
- tmp = 0
190
- else
191
- total += unit_prefix
192
- end
193
- end
194
- tmp += s.to_i
195
- end
196
- total + tmp
197
- end
198
-
199
- # Return parsed decimal part
200
- #
201
- # @return [Integer, Float]
202
- #
203
- # @api private
204
- # @since 0.0.1
205
- def parse_decimal_part(str)
206
- map = if str.index('割')
207
- Numeja::JA_割合用小数
208
- else
209
- Numeja::JA_小数
210
- end
211
- keys = map.keys()
212
- keys.reduce(0) do |t, ja_digit|
213
- index = str.index(ja_digit)
214
- break t if index.nil?
215
- unit = str.slice!(0, index)
216
- str.slice!(0, 1)
217
- t += (unit.to_f * map[ja_digit])
218
- t
219
- end
220
- end
221
-
222
- # Return large number parts
223
- #
224
- # @return [::Hash]
225
- #
226
- # @api private
227
- # @since 0.0.1
228
- def parse_large_numbers(str)
229
- # (?<year>(.*)億)(?<oku>(.*)万)
230
- splited = Numeja::JA_上数.keys.reverse.map do |ja_digit|
231
- index = str.index(ja_digit)
232
- next if index.nil?
233
- unit = str.slice!(0, index)
234
- digit = str.slice!(0, 1)
235
- {
236
- digit: digit,
237
- unit: unit
238
- }
132
+ denominator, molecule = str.split('分の').map { |s| Numeja::Number.new(s) }
133
+ molecule.to_f / denominator.to_f
239
134
  end
240
- splited.push(
241
- digit: '',
242
- unit: str
243
- ).compact
244
135
  end
245
136
  end
246
137
  end
@@ -0,0 +1,133 @@
1
+ module Numeja
2
+ # Number class
3
+ # @class private
4
+ # @since 0.0.2
5
+ class Number < DelegateClass(Integer)
6
+ def initialize(str)
7
+ @str = str
8
+ super(parse)
9
+ end
10
+
11
+ private
12
+
13
+ def parse
14
+ nomarized = nomarize(@str)
15
+ units = parse_large_numbers(nomarized)
16
+ @result = units.reduce(0) do |t, unit|
17
+ large_prefix = JA_上数[unit[:digit]]
18
+ if large_prefix.nil?
19
+ integer_part, decimal_part = split_digit(unit[:unit])
20
+ int = integer_part.nil? ? 0 : parse_integer_part(integer_part)
21
+ decimal = decimal_part.nil? ? 0 : parse_decimal_part(decimal_part)
22
+ t += int + decimal
23
+ else
24
+ integer = parse_integer_part(unit[:unit])
25
+ t += integer * large_prefix
26
+ end
27
+ t
28
+ end
29
+ end
30
+
31
+ # Return numerals parts
32
+ #
33
+ # @return [Array<String>]
34
+ #
35
+ # @api private
36
+ # @since 0.0.1
37
+ def split_digit(str)
38
+ i = str.index(/.分|.割/)
39
+ if i.nil?
40
+ [str]
41
+ elsif i.zero?
42
+ [nil, str]
43
+ else
44
+ [str[0..i - 1], str[i..-1]]
45
+ end
46
+ end
47
+
48
+ # Return nomarized Kansuuji
49
+ #
50
+ # @return [::String]
51
+ #
52
+ # @api private
53
+ # @since 0.0.1
54
+ def nomarize(str)
55
+ str
56
+ .tr('0-9', '0-9')
57
+ .gsub(REGEXP_旧字, JA_大字)
58
+ .gsub(REGEXP_数字, JA_数字)
59
+ end
60
+
61
+ # Return nomarized Kansuuji
62
+ #
63
+ # @return [::String]
64
+ #
65
+ # @api private
66
+ # @since 0.0.1
67
+ def parse_integer_part(input)
68
+ total = 0
69
+ tmp = 0
70
+ return input.to_i if input =~ /^[0-9]+$/
71
+ input.gsub(REGEXP_数字, JA_数字).each_char do |s|
72
+ unit_prefix = JA_下数[s]
73
+ unless unit_prefix.nil?
74
+ if tmp > 0
75
+ total += tmp * unit_prefix
76
+ tmp = 0
77
+ else
78
+ total += unit_prefix
79
+ end
80
+ end
81
+ tmp += s.to_i
82
+ end
83
+ total + tmp
84
+ end
85
+
86
+ # Return parsed decimal part
87
+ #
88
+ # @return [Integer, Float]
89
+ #
90
+ # @api private
91
+ # @since 0.0.1
92
+ def parse_decimal_part(str)
93
+ map = if str.index('割')
94
+ Numeja::JA_割合用小数
95
+ else
96
+ Numeja::JA_小数
97
+ end
98
+ keys = map.keys()
99
+ keys.reduce(0) do |t, ja_digit|
100
+ index = str.index(ja_digit)
101
+ break t if index.nil?
102
+ unit = str.slice!(0, index)
103
+ str.slice!(0, 1)
104
+ t += (unit.to_f * map[ja_digit])
105
+ t
106
+ end
107
+ end
108
+
109
+ # Return large number parts
110
+ #
111
+ # @return [::Hash]
112
+ #
113
+ # @api private
114
+ # @since 0.0.1
115
+ def parse_large_numbers(str)
116
+ # (?<year>(.*)億)(?<oku>(.*)万)
117
+ splited = Numeja::JA_上数.keys.reverse.map do |ja_digit|
118
+ index = str.index(ja_digit)
119
+ next if index.nil?
120
+ unit = str.slice!(0, index)
121
+ digit = str.slice!(0, 1)
122
+ {
123
+ digit: digit,
124
+ unit: unit
125
+ }
126
+ end
127
+ splited.push(
128
+ digit: '',
129
+ unit: str
130
+ ).compact
131
+ end
132
+ end
133
+ end
@@ -1,3 +1,3 @@
1
1
  module Numeja
2
- VERSION = '0.0.1'.freeze
2
+ VERSION = '0.1.0'.freeze
3
3
  end
@@ -22,6 +22,8 @@ Gem::Specification.new do |spec|
22
22
  spec.require_paths = ['lib']
23
23
 
24
24
  spec.add_development_dependency 'bundler'
25
+ spec.add_development_dependency 'codeclimate-test-reporter'
26
+ spec.add_development_dependency 'coveralls'
25
27
  spec.add_development_dependency 'pry'
26
28
  spec.add_development_dependency 'rake'
27
29
  spec.add_development_dependency 'rubocop'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: numeja
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Akira Takahashi
@@ -24,6 +24,34 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: codeclimate-test-reporter
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: coveralls
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
27
55
  - !ruby/object:Gem::Dependency
28
56
  name: pry
29
57
  requirement: !ruby/object:Gem::Requirement
@@ -121,11 +149,11 @@ files:
121
149
  - ".yardopts"
122
150
  - CODE_OF_CONDUCT.md
123
151
  - Gemfile
124
- - Gemfile.lock
125
152
  - LICENSE.txt
126
153
  - README.md
127
154
  - Rakefile
128
155
  - lib/numeja.rb
156
+ - lib/numeja/number.rb
129
157
  - lib/numeja/version.rb
130
158
  - numeja.gemspec
131
159
  homepage: https://github.com/rike422/numeja
@@ -1,72 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- numeja (0.0.1)
5
-
6
- GEM
7
- remote: https://rubygems.org/
8
- specs:
9
- ast (2.4.0)
10
- coderay (1.1.2)
11
- docile (1.3.0)
12
- json (2.1.0)
13
- method_source (0.9.0)
14
- parallel (1.12.1)
15
- parser (2.5.1.0)
16
- ast (~> 2.4.0)
17
- power_assert (1.1.1)
18
- powerpack (0.1.1)
19
- pry (0.11.3)
20
- coderay (~> 1.1.0)
21
- method_source (~> 0.9.0)
22
- rainbow (3.0.0)
23
- rake (10.5.0)
24
- rr (1.2.1)
25
- rubocop (0.56.0)
26
- parallel (~> 1.10)
27
- parser (>= 2.5)
28
- powerpack (~> 0.1)
29
- rainbow (>= 2.2.2, < 4.0)
30
- ruby-progressbar (~> 1.7)
31
- unicode-display_width (~> 1.0, >= 1.0.1)
32
- ruby-progressbar (1.9.0)
33
- simplecov (0.16.1)
34
- docile (~> 1.1)
35
- json (>= 1.8, < 3)
36
- simplecov-html (~> 0.10.0)
37
- simplecov-html (0.10.2)
38
- test-unit (3.2.8)
39
- power_assert
40
- test-unit-context (0.5.1)
41
- test-unit (>= 2.4.0)
42
- test-unit-full (0.0.5)
43
- test-unit
44
- test-unit-context
45
- test-unit-notify
46
- test-unit-rr
47
- test-unit-runner-tap
48
- test-unit-notify (1.0.4)
49
- test-unit (>= 2.4.9)
50
- test-unit-rr (1.0.5)
51
- rr (>= 1.1.1)
52
- test-unit (>= 2.5.2)
53
- test-unit-runner-tap (1.1.2)
54
- test-unit
55
- unicode-display_width (1.3.2)
56
- yard (0.9.12)
57
-
58
- PLATFORMS
59
- ruby
60
-
61
- DEPENDENCIES
62
- bundler
63
- numeja!
64
- pry
65
- rake
66
- rubocop
67
- simplecov
68
- test-unit-full
69
- yard
70
-
71
- BUNDLED WITH
72
- 1.16.1