law_number_switch 0.0.18 → 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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/law_number_switch.rb +97 -96
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e0e43127b0a2d5b9252145ab651b8acc71cae0ac
4
- data.tar.gz: 3247190ec7e7d55c4010de2c607715a5f75f2754
3
+ metadata.gz: 28c0b6b97eda0982e91db732e9a7b0ac93ed5c9e
4
+ data.tar.gz: 1682a3e9b1fe46d0d5f614631b2a25e50280c90e
5
5
  SHA512:
6
- metadata.gz: 6abb6327c13e175b899f4a5822ea41c331e87367856fc2896813c8d094a81241552eff85312f511eaeddc896f0a849a563f24278c208575aa1804543d317e0c0
7
- data.tar.gz: 1c29d6db81d517cd0b1c26e98b9304c1e64cb7e625b658d23b3fe41da33c1efbf1f0465a03b52f2073d9e67aedcf0abdb90e12157bfcded930b246e90a843a6a
6
+ metadata.gz: d39fde132c266766e78800886b9e52d6f297b33fe1c0cc1b8e3036e481e72377163bbf9b115cc231683cac771ed99db196e017ae955593f74332d4709a9dcd1d
7
+ data.tar.gz: af3a9ddc290e279dcf2884a6af26449b612b30d3d92de34d03d17a01c96d0e0c0dc1b4628a595d25bc0825d19ffb2305503a313eb2d48128c6c6ea07c31e2bd4
@@ -4,109 +4,133 @@ module LawNumberSwitch
4
4
  CN_UNIT_BIG = ["", "萬", "億", "兆" ]
5
5
 
6
6
  def to_cn_number
7
- string = self.to_cn_number_oneten
8
- if string =~ /一十/
9
- string.gsub(/一十/, '十')
10
- else
11
- string
7
+ self.to_cn_number_oneten.gsub(/一十/, '十')
8
+ end
9
+
10
+ def to_cn_number_capital
11
+ self.to_cn_number.tr('一二三四五六七八九十百千', '壹貳參肆伍陸柒捌玖拾佰仟')
12
+ end
13
+
14
+ def to_cn_number_only
15
+ output = ''
16
+ self.split('').each do |num|
17
+ output += num.to_cn_number_oneten
12
18
  end
19
+ output
13
20
  end
14
21
 
15
22
  def to_cn_number_oneten
16
- if self =~ /-/
17
- dash_words
18
- else
19
- int_words
23
+ output = ''
24
+ string = ''
25
+ self.scan(/./).each do |char|
26
+ if char =~ /\d/ || char == '-'
27
+ string += char
28
+ else
29
+ char = string.to_cn_number_judge + char unless string.empty?
30
+ output += char
31
+ string = ''
32
+ end
20
33
  end
34
+ output += string.to_cn_number_judge unless string.empty?
35
+ output
21
36
  end
22
37
 
23
- def int_words
24
- n_s = self.to_i.abs
25
- return "零" if n_s == 0
26
-
27
- num_arr = n_s.to_s.split('').reverse
28
- rst_arr = []
38
+ def to_cn_number_judge
39
+ self =~ /-/ ? dash_words : int_words
40
+ end
29
41
 
30
- writable = -1
42
+ def int_words
43
+ return '零' if self == '0'
44
+ return self if self.length > 16
45
+ num_arr = self.split('').reverse
46
+ cn_num_arr = []
47
+ write = -1
31
48
  #1: 都需要寫;
32
49
  #0: 寫一個零,後面的千不用寫;
33
50
  #-1:不需要寫千,也不需要寫零;
34
-
35
51
  num_arr.each_with_index do |value, index|
36
- writable = -1 if index%4 == 0
37
- unit_big = (index%4 == 0) ? CN_UNIT_BIG[index/4] : ""
52
+ write = -1 if index % 4 == 0
53
+ unit_big = (index % 4 == 0) ? CN_UNIT_BIG[index / 4] : ""
38
54
  number = CN_NUMBER[value.to_i]
39
-
40
55
  if value.to_i == 0
41
- unit = (writable == 1) ? "零" : ""
42
- writable = (writable == 1) ? 0 : -1
56
+ unit = (write == 1) ? "零" : ""
57
+ write = (write == 1) ? 0 : -1
43
58
  else
44
- unit = CN_UNIT[index%4]
45
- writable = 1
59
+ unit = CN_UNIT[index % 4]
60
+ write = 1
46
61
  end
62
+ cn_num_arr << number + unit + unit_big
63
+ end
64
+ string = cn_num_arr.reverse.join
47
65
 
48
- rst_arr << number + unit + unit_big
66
+ CN_UNIT_BIG.each_with_index do |char, index|
67
+ position = string.index(char)
68
+ if index > 0 && position != nil && string[position - 1] == CN_UNIT_BIG[index + 1]
69
+ string[position] = ''
70
+ # case: successive units
71
+ end
49
72
  end
50
- rst_arr.reverse.join
73
+ string
51
74
  end
52
75
 
53
76
  def dash_words
54
- n_arr = []
55
- front = self.scan(/\d*-/)
56
- behind = self.scan(/-\d*$/)[0]
57
-
58
- front.each do |f|
59
- f_n = f.int_words + "之"
60
- n_arr << f_n
77
+ return '負' if self == '-'
78
+ output = ''
79
+ self.split('-').each do |part|
80
+ output += part.to_cn_number_oneten + '之'
61
81
  end
62
-
63
- b_n = behind.int_words
64
- n_arr << b_n
65
-
66
- n_arr.join
82
+ self[0] == '-' ? output = output.sub(/之/, '負') : output
83
+ self[-1] == '-' ? output : output.chop
67
84
  end
68
85
 
69
86
  CN_NUMS = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"]
70
87
  CN_DECS = ["十", "百", "千", "萬", "億", "兆", "拾", "佰", '仟']
71
- CN_NUMS_MAP = {'〇' => 0, '一' => 1, '二' => 2, '兩' => 2, '三' => 3, '四' => 4, '五' => 5,
72
- '六' => 6, '七' => 7, '八' => 8, '九' => 9, '零' => 0, '壹' => 1, '貳' => 2,
73
- '參' => 3, '肆' => 4, '伍' => 5, '陸' => 6, '柒' => 7, '捌' => 8, '玖' => 9, '○' => 0}
88
+ CN_NUMS_MAP = {'〇' => 0, '○' => 0, '一' => 1, '二' => 2, '兩' => 2, '三' => 3, '四' => 4,
89
+ '五' => 5, '六' => 6, '七' => 7, '八' => 8, '九' => 9, '零' => 0, '壹' => 1, '貳' => 2,
90
+ '參' => 3, '肆' => 4, '伍' => 5, '陸' => 6, '柒' => 7, '捌' => 8, '玖' => 9}
74
91
  CN_DECS_MAP = {'個' => 1, '十' => 10, '百' => 100, '千' => 1000, '萬' => 10000,
75
92
  '億' => 100000000, '兆' => 100000000000, '拾' => 10, '佰' => 100, '仟' => 1000}
76
93
 
77
94
  def to_number
78
95
  output = ''
79
96
  string = ''
80
- self.scan(/./) do |c|
81
- if CN_NUMS_MAP[c] || CN_DECS_MAP[c] || c == '之'
82
- string += c
97
+ self.scan(/./) do |char|
98
+ if CN_NUMS_MAP[char] || CN_DECS_MAP[char] || char == '之'
99
+ string += char
83
100
  else
84
- c = string.to_number_case + c unless string.empty?
85
- output += c
101
+ char = string.to_number_judge + char unless string.empty?
102
+ output += char
86
103
  string = ''
87
104
  end
88
105
  end
89
- output += string.to_number_case unless string.empty?
106
+ output += string.to_number_judge unless string.empty?
90
107
  output
91
108
  end
92
109
 
93
- def to_number_case
94
- string = self
110
+ def to_number_only
111
+ number_only = ''
112
+ self.scan(/./) do |char|
113
+ if char == '之'
114
+ number_only += "-"
115
+ else
116
+ number_only += CN_NUMS_MAP[char].to_s
117
+ end
118
+ end
119
+ number_only
120
+ end
121
+
122
+ def to_number_judge
95
123
  return '之' if self == '之'
96
- no_unit = 0
97
- string = string.gsub(/(?:○|〇)/, "零").gsub(/(?:零|○|〇)(?:十|拾)/, "一十")
124
+ non_unit = 0
125
+ string = self.gsub(/(?:○|〇)/, "零").gsub(/(?:零|○|〇)(?:十|拾)/, "一十")
98
126
  .gsub(/廿/, "二十").gsub(/卅/, "三十")
99
- CN_DECS.each do |d|
100
- no_ten = d + '十'
101
- one_ten = d + '一' + '十'
102
- if string =~ Regexp.new(no_ten)
103
- string = self.gsub(Regexp.new(no_ten), one_ten)
104
- end
105
- if string !~ Regexp.new(d)
106
- no_unit += 1
107
- end
127
+ CN_DECS.each do |dec|
128
+ no_ten = dec + '十'
129
+ one_ten = dec + '一' + '十'
130
+ string = string.gsub(Regexp.new(no_ten), one_ten)
131
+ non_unit += 1 if string !~ Regexp.new(dec)
108
132
  end
109
- if no_unit == CN_DECS.length
133
+ if non_unit == CN_DECS.length
110
134
  string.to_number_only
111
135
  elsif string =~ /之/
112
136
  string.dash_number
@@ -115,24 +139,10 @@ module LawNumberSwitch
115
139
  end
116
140
  end
117
141
 
118
- def to_number_only
119
- number_only = ''
120
- self.scan(/./) do |c|
121
- if c == '之'
122
- number_only += "-"
123
- else
124
- number_only += CN_NUMS_MAP[c].to_s
125
- end
126
- end
127
- number_only
128
- end
129
-
130
142
  def int_number
131
143
  num_str = ''
132
- last = nil
133
- self.scan(/./) do |c|
134
- num_str += c if c != '零'
135
- last = c
144
+ self.scan(/./) do |char|
145
+ num_str += char if char != '零'
136
146
  end
137
147
  if num_str =~ /^十/
138
148
  num_str = '一' + num_str
@@ -141,16 +151,15 @@ module LawNumberSwitch
141
151
  sums = []
142
152
  temp_sum = 0
143
153
  last_num = 0
144
- num_str.scan(/./) do |ch|
145
- if num = CN_NUMS_MAP[ch]
154
+ num_str.scan(/./) do |char|
155
+ if num = CN_NUMS_MAP[char]
146
156
  last_num = num
147
- elsif dec = CN_DECS_MAP[ch]
157
+ elsif dec = CN_DECS_MAP[char]
148
158
  if dec < 10000
149
159
  temp_sum += last_num * dec
150
160
  else
151
- #find back for the one that exceeds current dec
152
161
  sums.each_with_index do |x, i|
153
- if x < dec * 10 #10 is here for situation like 兩億億
162
+ if x < dec * 10 # case: double units
154
163
  sums[i] = x * dec
155
164
  else
156
165
  break
@@ -168,26 +177,18 @@ module LawNumberSwitch
168
177
  sums << temp_sum + last_num
169
178
 
170
179
  sum = 0
171
- sums.each do |x|
172
- sum += x
180
+ sums.each do |num|
181
+ sum += num
173
182
  end
174
183
  sum.to_s
175
184
  end
176
185
 
177
186
  def dash_number
178
- w_arr = []
179
- front = self.scan(/[^之]*之/)
180
- behind = self.scan(/[^之]*.$/)[0]
181
-
182
- front.each do |f|
183
- f = f.chop
184
- f_w = f.int_number + '-'
185
- w_arr << f_w
187
+ output = ''
188
+ self.split('之').each do |part|
189
+ output += part.to_number + '-'
186
190
  end
187
-
188
- b_w = behind.int_number
189
- w_arr << b_w
190
- w_arr.join
191
+ self[-1] == '之' ? output : output.chop
191
192
  end
192
193
  end
193
194
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: law_number_switch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.18
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - GaussJay