-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhan2zen.py
188 lines (143 loc) · 5.71 KB
/
han2zen.py
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
# -*- coding: utf-8 -*-
# python3のみ対応
import sys
ASCII = 1
DIGIT = 2
KANA = 4
ALL = ASCII | DIGIT | KANA
# list of ZENKAKU characters
z_ascii = ["a", "b", "c", "d", "e", "f", "g", "h", "i",
"j", "k", "l", "m", "n", "o", "p", "q", "r",
"s", "t", "u", "v", "w", "x", "y", "z",
"A", "B", "C", "D", "E", "F", "G", "H", "I",
"J", "K", "L", "M", "N", "O", "P", "Q", "R",
"S", "T", "U", "V", "W", "X", "Y", "Z",
"!", "”", "#", "$", "%", "&", "’", "(", ")",
"*", "+", ",", "-", ".", "/", ":", ";", "<",
"=", ">", "?", "@", "[", "¥", "]", "^", "_",
"‘", "{", "|", "}", "~", " "]
z_digit = ["0", "1", "2", "3", "4",
"5", "6", "7", "8", "9"]
z_kana = ["ア", "イ", "ウ", "エ", "オ",
"カ", "キ", "ク", "ケ", "コ",
"サ", "シ", "ス", "セ", "ソ",
"タ", "チ", "ツ", "テ", "ト",
"ナ", "ニ", "ヌ", "ネ", "ノ",
"ハ", "ヒ", "フ", "ヘ", "ホ",
"マ", "ミ", "ム", "メ", "モ",
"ヤ", "ユ", "ヨ",
"ラ", "リ", "ル", "レ", "ロ",
"ワ", "ヲ", "ン",
"ァ", "ィ", "ゥ", "ェ", "ォ",
"ッ", "ャ", "ュ", "ョ", "ヴ",
"ガ", "ギ", "グ", "ゲ", "ゴ",
"ザ", "ジ", "ズ", "ゼ", "ゾ",
"ダ", "ヂ", "ヅ", "デ", "ド",
"バ", "ビ", "ブ", "ベ", "ボ",
"パ", "ピ", "プ", "ペ", "ポ",
"。", "、", "・", "゛", "゜", "「", "」", "ー"]
# list of HANKAKU characters
h_ascii = ["a", "b", "c", "d", "e", "f", "g", "h", "i",
"j", "k", "l", "m", "n", "o", "p", "q", "r",
"s", "t", "", "v", "w", "x", "y", "z",
"A", "B", "C", "D", "E", "F", "G", "H", "I",
"J", "K", "L", "M", "N", "O", "P", "Q", "R",
"S", "T", "", "V", "W", "X", "Y", "Z",
"!", u'"', "#", "$", "%", "&", "'", "(", ")",
"*", "+", ",", "-", ".", "/", ":", ";", "<",
"=", ">", "?", "@", "[", "\\", "]", "^", "_",
"`", "{", "|", "}", "~", " "]
h_digit = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
h_kana = ["ア", "イ", "ウ", "エ", "オ",
"カ", "キ", "ク", "ケ", "コ",
"サ", "シ", "ス", "セ", "ソ",
"タ", "チ", "ツ", "テ", "ト",
"ナ", "ニ", "ヌ", "ネ", "ノ",
"ハ", "ヒ", "フ", "ヘ", "ホ",
"マ", "ミ", "ム", "メ", "モ",
"ヤ", "ユ", "ヨ",
"ラ", "リ", "ル", "レ", "ロ",
"ワ", "ヲ", "ン",
"ァ", "ィ", "ゥ", "ェ", "ォ",
"ッ", "ャ", "ュ", "ョ", "ヴ",
"ガ", "ギ", "グ", "ゲ", "ゴ",
"ザ", "ジ", "ズ", "ゼ", "ゾ",
"ダ", "ヂ", "ヅ", "デ", "ド",
"バ", "ビ", "ブ", "ベ", "ボ",
"パ", "ピ", "プ", "ペ", "ポ",
"。", "、", "・", "゙", "゚", "「", "」", "ー"]
# maps of ascii
zh_ascii = {}
hz_ascii = {}
for i in range(len(z_ascii)):
zh_ascii[z_ascii[i]] = h_ascii[i]
hz_ascii[h_ascii[i]] = z_ascii[i]
del z_ascii, h_ascii
# maps of digit
zh_digit = {}
hz_digit = {}
for i in range(len(z_digit)):
zh_digit[z_digit[i]] = h_digit[i]
hz_digit[h_digit[i]] = z_digit[i]
del z_digit, h_digit
# maps of KANA
zh_kana = {}
hz_kana = {}
for i in range(len(z_kana)):
zh_kana[z_kana[i]] = h_kana[i]
hz_kana[h_kana[i]] = z_kana[i]
del z_kana, h_kana
def Zen2Han(text="", mode=ALL, ignore=()):
result = list()
for c in text:
if c in ignore:
result.append(c)
elif (mode in (1, 3, 5, 7)) and (c in zh_ascii):
result.append(zh_ascii[c])
elif (mode in (2, 3, 6, 7)) and (c in zh_digit):
result.append(zh_digit[c])
elif (mode in range(4, 8)) and (c in zh_kana):
result.append(zh_kana[c])
else:
result.append(c)
return "".join(result)
def Han2Zen(text, mode=ALL, ignore=()):
result = list()
i = 0
size = len(text)
while i < size:
if i < size - 1:
if text[i:i+2] in ignore:
result.append(text[i:i+2])
i += 2
continue
elif (mode in range(4, 8)) and (text[i:i+2] in hz_kana):
result.append(hz_kana[text[i:i+2]])
i += 2
continue
if text[i:i+1] in ignore:
result.append(text[i:i+1])
elif (mode in (1, 3, 5, 7)) and (text[i:i+1] in hz_ascii):
result.append(hz_ascii[text[i:i+1]])
elif (mode in (2, 3, 6, 7)) and (text[i:i+1] in hz_digit):
result.append(hz_digit[text[i:i+1]])
elif (mode in range(4, 8)) and (text[i:i+1] in hz_kana):
result.append(hz_kana[text[i:i+1]])
else:
result.append(text[i:i+1])
i += 1
return "".join(result)
if __name__ == "__main__":
for line in sys.stdin:
line = Zen2Han(line, ASCII | DIGIT )
line = Han2Zen(line, KANA)
print(line, end='')
'''
teststr = "abcDEF123456アガサダナバビプペ"
print("original:", teststr)
print("Han2Zen : ascii only:", Han2Zen(teststr, ASCII))
print("Han2Zen : ascii and kana:", Han2Zen(teststr, ASCII|KANA))
print("Zen2Han : digit only:", Zen2Han(teststr, DIGIT))
print("Zen2Han : digit and kana:", Zen2Han(teststr, DIGIT|KANA))
print("Zen2Han : digit and kana, except '5':", Zen2Han(teststr, DIGIT|KANA, ("5")))
'''