This repository was archived by the owner on Mar 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplusplus.py
More file actions
282 lines (249 loc) · 8.91 KB
/
plusplus.py
File metadata and controls
282 lines (249 loc) · 8.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#-*- coding:utf-8 -*-
import unicodedata
class PlusPlus(object): # For compatibility issue
@classmethod
def plusone(cls, raw_src):
return plusone(raw_src)
hanja_normal = set([u'零', u'一', u'二', u'三', u'四', u'五', u'六', u'七', u'八', u'九', u'十', u'百'])
hanja_difficult = set([u'壹', u'貳', u'參', u'拾'])
roma_normal = set([u'i', u'v', 'x', 'l'])
roma_capital_normal = set([u'I', u'V', u'X', 'L'])
roma_special = set([u'ⅰ', u'ⅱ', u'ⅲ', u'ⅳ', u'ⅴ', u'ⅵ', u'ⅶ', u'ⅷ', u'ⅸ', u'ⅹ'])
roma_capital_special = set([u'Ⅰ', u'Ⅱ', u'Ⅲ', u'Ⅳ', u'Ⅴ', u'Ⅵ', u'Ⅶ', u'Ⅷ', u'Ⅸ', u'Ⅹ'])
def find_hanja(src):
start, end, position = -1, -1, -1
result = []
mode = 'hanja_normal'
for char in src:
position += 1
if char in hanja_normal or char in hanja_difficult:
if end == position:
if char in hanja_difficult:
mode = 'hanja_difficult'
end = position + 1
else:
result.append([start, end, mode])
mode = 'hanja_difficult' if char in hanja_difficult else 'hanja_normal'
start, end = position, position + 1
if start != -1:
result.append([start, end, mode])
else:
result = None
return result
def find_roma(src):
start, end, position = -1, -1, -1
mode = 'roma_normal'
for char in src:
position += 1
if ('a' <= char <= 'z' or 'A' <= char <= 'Z') \
and char not in roma_normal \
and char not in roma_capital_normal:
start, end, mode = -1, -1, None
break
if char in roma_normal or char in roma_capital_normal:
if char in roma_capital_normal:
mode = 'roma_capital_normal'
if end == position:
end = position + 1
else:
start, end = position, position + 1
position = -1
for char in src:
position += 1
if char in roma_special and start < position:
start, end, mode = position, position + 1, 'roma_special'
if char in roma_capital_special and start < position:
start, end, mode = position, position + 1, 'roma_capital_special'
if start == -1:
mode = None
return start, end, mode
def find_arabia(src):
start, end, position = -1, -1, -1
mode = 'arabia'
for char in src:
position += 1
if '0' <= char <= '9':
if end == position:
end = position + 1
else:
start, end = position, position + 1
if start == -1:
mode = None
return start, end, mode
def find_value(src):
result_hanja = find_hanja(src)
if result_hanja is None:
result_hanja = (-1, -1, None)
else:
result_hanja = result_hanja[-1]
result_roma = find_roma(src)
result_arabia = find_arabia(src)
result = (-1, -1, None)
if result[0] < result_hanja[0] and result_hanja[2] != None:
result = result_hanja
if result[0] < result_roma[0] and result_roma[2] != None:
result = result_roma
if result[0] < result_arabia[0] and result_arabia[2] != None:
result = result_arabia
return result
def process_value(src, mode):
result = src
if mode == 'arabia':
length = 0
if src[0] == '0':
length = len(src)
result = str(int(src) + 1)
if length > len(result):
result = '0' * (length - len(result)) + result
if mode == 'hanja_normal' or mode == 'hanja_difficult':
if mode == 'hanja_normal':
hanja_map = { 0: u'零'
, 1: u'一'
, 2: u'二'
, 3: u'三'
, 4: u'四'
, 5: u'五'
, 6: u'六'
, 7: u'七'
, 8: u'八'
, 9: u'九'
, 10: u'十'
,100: u'百'}
elif mode == 'hanja_difficult':
hanja_map = { 0: u'零'
, 1: u'壹'
, 2: u'貳'
, 3: u'參'
, 4: u'四'
, 5: u'五'
, 6: u'六'
, 7: u'七'
, 8: u'八'
, 9: u'九'
, 10: u'拾'
,100: u'百'}
hanja_rev_map = dict()
for hanja in hanja_map:
hanja_rev_map[hanja_map[hanja]] = hanja
mode = 'non_unit'
for char in src:
if hanja_rev_map[char] >= 10:
mode = 'has_unit'
if len(src) == 1:
mode = 'has_unit'
if mode == 'has_unit':
value = 0
cur_value = 0
for char in src:
tmp_value = hanja_rev_map[char]
if tmp_value < 10:
cur_value = tmp_value
else:
if cur_value == 1:
mode = 'has_unit_include_one'
if cur_value == 0:
cur_value = 1
value += cur_value * tmp_value
cur_value = 0
value += tmp_value if tmp_value < 10 else 0
value += 1
result = u''
for mod in [100, 10, 1]:
cur_value = value / mod
value %= mod
if cur_value != 0:
if mod == 1:
result += hanja_map[cur_value]
elif mode == 'has_unit' and cur_value == 1:
result += hanja_map[mod]
else:
result += hanja_map[cur_value] + hanja_map[mod]
else:
value = 0
for char in src:
tmp_value = hanja_rev_map[char]
value = value * 10 + tmp_value
value += 1
result = u''
for mod in [100, 10, 1]:
cur_value = value / mod
value %= mod
if result == u'' and cur_value == 0:
continue
result += hanja_map[cur_value]
if mode == 'roma_normal' or mode == 'roma_capital_normal':
src = src.upper()
value = [0, 0, 0]
for char in src:
if char == 'I': value[0] += 1
if char == 'X':
if value[0] > 0: value[0] = -value[0]
value[1] += 1
if char == 'V':
if value[0] > 0: value[0] = -value[0]
value[0] += 5
if char == 'L':
if value[1] > 0: value[1] = -value[1]
value[1] += 5
value = value[0] + value[1] * 10 + value[2] * 100 + 1
if value >= 90: # Current version do not support more than or equal 90
result = src
else:
result = u''
while value:
if value >= 50:
result += 'L'
value -= 50
continue
if value >= 40:
result += 'XL'
value -= 40
continue
if value >= 10:
result += 'X'
value -= 10
continue
if value >= 9:
result += 'IX'
value -= 9
continue
if value >= 5:
result += 'V'
value -= 5
continue
if value >= 4:
result += 'IV'
value -= 4
continue
if value >= 1:
result += 'I'
value -= 1
continue
if mode == 'roma_normal':
result = result.lower()
if mode == 'roma_special' or mode == 'roma_capital_special':
# 특수문자의 경우 1~10까지만 처리.
if mode == 'roma_special':
roma = [u'ⅰ', u'ⅱ', u'ⅲ', u'ⅳ', u'ⅴ', u'ⅵ', u'ⅶ', u'ⅷ', u'ⅸ', u'ⅹ']
else:
roma = [u'Ⅰ', u'Ⅱ', u'Ⅲ', u'Ⅳ', u'Ⅴ', u'Ⅵ', u'Ⅶ', u'Ⅷ', u'Ⅸ', u'Ⅹ']
for i in xrange(0, 9):
if src == roma[i]:
result = roma[i + 1]
return result
def plusone(raw_src, encoding='utf-8'):
if type(raw_src) is str:
src = raw_src.decode(encoding)
else:
src = raw_src
encoding = None
src = unicodedata.normalize('NFC', src)
result = find_value(src)
if result[2] != None:
processed_value = process_value(src[result[0]:result[1]], result[2])
dest = src[:result[0]] + processed_value + src[result[1]:]
else:
dest = src
if encoding is not None:
dest = dest.encode(encoding)
return dest