law_number_switch 0.0.18 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/law_number_switch.rb +97 -96
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 28c0b6b97eda0982e91db732e9a7b0ac93ed5c9e
|
4
|
+
data.tar.gz: 1682a3e9b1fe46d0d5f614631b2a25e50280c90e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d39fde132c266766e78800886b9e52d6f297b33fe1c0cc1b8e3036e481e72377163bbf9b115cc231683cac771ed99db196e017ae955593f74332d4709a9dcd1d
|
7
|
+
data.tar.gz: af3a9ddc290e279dcf2884a6af26449b612b30d3d92de34d03d17a01c96d0e0c0dc1b4628a595d25bc0825d19ffb2305503a313eb2d48128c6c6ea07c31e2bd4
|
data/lib/law_number_switch.rb
CHANGED
@@ -4,109 +4,133 @@ module LawNumberSwitch
|
|
4
4
|
CN_UNIT_BIG = ["", "萬", "億", "兆" ]
|
5
5
|
|
6
6
|
def to_cn_number
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
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
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
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
|
24
|
-
|
25
|
-
|
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
|
-
|
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
|
-
|
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 = (
|
42
|
-
|
56
|
+
unit = (write == 1) ? "零" : ""
|
57
|
+
write = (write == 1) ? 0 : -1
|
43
58
|
else
|
44
|
-
unit = CN_UNIT[index%4]
|
45
|
-
|
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
|
-
|
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
|
-
|
73
|
+
string
|
51
74
|
end
|
52
75
|
|
53
76
|
def dash_words
|
54
|
-
|
55
|
-
|
56
|
-
|
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
|
-
|
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,
|
72
|
-
'六' => 6, '七' => 7, '八' => 8, '九' => 9, '零' => 0, '壹' => 1, '貳' => 2,
|
73
|
-
'參' => 3, '肆' => 4, '伍' => 5, '陸' => 6, '柒' => 7, '捌' => 8, '玖' => 9
|
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 |
|
81
|
-
if CN_NUMS_MAP[
|
82
|
-
string +=
|
97
|
+
self.scan(/./) do |char|
|
98
|
+
if CN_NUMS_MAP[char] || CN_DECS_MAP[char] || char == '之'
|
99
|
+
string += char
|
83
100
|
else
|
84
|
-
|
85
|
-
output +=
|
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.
|
106
|
+
output += string.to_number_judge unless string.empty?
|
90
107
|
output
|
91
108
|
end
|
92
109
|
|
93
|
-
def
|
94
|
-
|
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
|
-
|
97
|
-
string =
|
124
|
+
non_unit = 0
|
125
|
+
string = self.gsub(/(?:○|〇)/, "零").gsub(/(?:零|○|〇)(?:十|拾)/, "一十")
|
98
126
|
.gsub(/廿/, "二十").gsub(/卅/, "三十")
|
99
|
-
CN_DECS.each do |
|
100
|
-
no_ten =
|
101
|
-
one_ten =
|
102
|
-
|
103
|
-
|
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
|
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
|
-
|
133
|
-
|
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 |
|
145
|
-
if num = CN_NUMS_MAP[
|
154
|
+
num_str.scan(/./) do |char|
|
155
|
+
if num = CN_NUMS_MAP[char]
|
146
156
|
last_num = num
|
147
|
-
elsif dec = CN_DECS_MAP[
|
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 #
|
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 |
|
172
|
-
sum +=
|
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
|
-
|
179
|
-
|
180
|
-
|
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
|
|