-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkana.py
executable file
·291 lines (216 loc) · 7.06 KB
/
kana.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
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
283
284
285
286
287
288
289
290
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from __future__ import unicode_literals
from jcconv import kata2hira, hira2kata
from itertools import chain
from printable import PrintableDict, PrintableList
__by_vowels = PrintableDict(**{
u'ア': u'ワラヤャマハナタサカアァ',
u'イ': u'リミヒニちシキイィ',
u'ウ': u'ルユュムフヌツスクウゥ',
u'エ': u'レメヘネテセケエェ',
u'オ': u'ヲロヨョモホノトソコオォ',
})
__to_dakuten = PrintableDict(**{
u'か': u'が',
u'き': u'ぎ',
u'く': u'ぐ',
u'け': u'げ',
u'こ': u'ご',
u'さ': u'ざ',
u'し': u'じ',
u'す': u'ず',
u'せ': u'ぜ',
u'そ': u'ぞ',
u'た': u'だ',
u'ち': u'ぢ',
u'つ': u'づ',
u'て': u'で',
u'と': u'ど',
u'は': u'ばぱ',
u'ひ': u'びぴ',
u'ふ': u'ぶぷ',
u'へ': u'べぺ',
u'ほ': u'ぼぽ',
})
__to_mini = PrintableDict(**{
u'く': u'っ',
u'つ': u'っ',
u'や': u'ゃ',
u'よ': u'ょ',
u'ゆ': u'ゅ',
u'わ': u'ゎ',
u'か': u'ゕ',
u'け': u'ゖ',
u'あ': u'ぁ',
u'い': u'ぃ',
u'う': u'ぅ',
u'え': u'ぇ',
u'お': u'ぉ',
})
EXTENDABLE_MINIS = (
u'つ',
u'く',
)
__by_dakuten = PrintableDict()
for vowel, letters in __to_dakuten.iteritems():
for letter in letters:
__by_dakuten[letter] = vowel
__to_vowels = PrintableDict()
for vowel, letters in __by_vowels.iteritems():
for letter in letters:
__to_vowels[letter] = vowel
def codepoint_range(start, end):
for val in range(start, end):
try:
yield unichr(val)
except ValueError:
# Sometimes certain codepoints can't be used on a machine
pass
def char_set(value):
if isinstance(value, list) or isinstance(value, tuple):
return codepoint_range(*value)
else:
return [value]
def unipairs(lst):
return PrintableList(reduce(lambda a, b: chain(a, b), map(char_set, lst)))
__KATAKANA = (
# Katakana: http://en.wikipedia.org/wiki/Katakana
(0x30A0, 0x30FF + 1),
(0x31F0, 0x31FF + 1),
(0x3200, 0x32FF + 1),
(0xFF00, 0xFFEF + 1),
)
__HIRAGANA = (
# Hiragana: http://en.wikipedia.org/wiki/Hiragana
(0x3040, 0x309F + 1),
(0x1B000, 0x1B0FF + 1),
)
__KANJI = (
(0x4e00, 0x9faf + 1),
)
__BONUS_KANA = (
u'〜',
)
KATAKANA = unipairs(__KATAKANA)
HIRAGANA = unipairs(__HIRAGANA)
KANA = PrintableList(KATAKANA + HIRAGANA + unipairs(__BONUS_KANA))
KANJI = unipairs(__KANJI)
def __is_katakana(char):
return char in KATAKANA
def is_katakana(string):
for char in string:
if not __is_katakana(char):
return False
return True
def __is_hiragana(char):
return char in HIRAGANA
def is_hiragana(string):
for char in string:
if not __is_hiragana(char):
return False
return True
def __is_kana(char):
return char in KANA
def is_kana(string):
for char in string:
if not __is_kana(char):
return False
return True
def __is_kanji(char):
return char in KANJI
def is_kanji(string):
for char in string:
if not __is_kanji(char):
return False
return True
def kana_minus_dakuten(char):
if is_katakana(char):
hira = kata2hira(char)
hira = __by_dakuten.get(hira, hira)
return hira2kata(hira)
else:
return __by_dakuten.get(char, char)
def kana_plus_dakuten(char):
yield char
is_kata = is_katakana(char)
if is_kata:
char = kata2hira(char)
for char in __to_dakuten.get(char, ''):
yield hira2kata(char) if is_kata else char
def kana_plus_mini(char):
yield char
is_kata = is_katakana(char)
if is_kata:
char = kata2hira(char)
for char in __to_mini.get(char, ''):
yield hira2kata(char) if is_kata else char
def extend_dakuten_reading(string):
if len(string) == 0:
yield ''
return
char = string[0]
for mult in kana_plus_dakuten(char):
yield mult + string[1:]
def extend_mini_reading(string):
if len(string) == 0:
yield ''
return
char = string[-1]
if char not in EXTENDABLE_MINIS:
yield string
return
for substr in kana_plus_mini(char):
yield string[:-1] + substr
def char_to_base_vowel(char):
char = kana_minus_dakuten(char)
translated = __to_vowels.get(char, False) or __to_vowels.get(hira2kata(char), False)
if translated is False:
raise Exception(u"Can't convert")
return translated
def all_to_hiragana(string):
out = u''
for index, char in enumerate(string):
if char == u'ー' or char == u'|':
char = char_to_base_vowel(out[-1])
char = kata2hira(char)
out += char
return out
if __name__ == u'__main__':
from tester import *
test_equal(kana_minus_dakuten(u'は'), u'は', u"No Dakuten failure")
test_equal(kana_minus_dakuten(u'ば'), u'は', u"No Dakuten failure")
test_equal(kana_minus_dakuten(u'ぱ'), u'は', u"No Dakuten failure")
test_equal(kana_minus_dakuten(u'ジ'), u'シ', u"Katakana failure")
test_equal(kana_minus_dakuten(u'本'), u'本', u"Kanji changed")
test_true(is_katakana(u'ハ'), u"Katakana check wrong")
test_true(is_katakana(u'ー'), u"Katakana check wrong")
test_true(is_katakana(u'ジ'), u"Katakana check wrong")
test_true(is_katakana(u'ッ'), u"Katakana check wrong")
test_true(not is_katakana(u'本'), u"Katakana Kanji check wrong")
test_true(not is_katakana(u'っ'), u"Katakana small hiragana check wrong")
test_true(not is_katakana(u'は'), u"Katakana hiragana wrong")
test_true(is_hiragana(u'っ'), u"Hiragana check wrong")
test_true(is_hiragana(u'つ'), u"Hiragana check wrong")
test_true(is_hiragana(u'を'), u"Hiragana check wrong")
test_true(not is_hiragana(u'本'), u"Hiragana Kanji check wrong")
test_true(not is_hiragana(u'ッ'), u"Hiragana small katakana check wrong")
test_true(not is_hiragana(u'ハ'), u"Hiragana katakana check wrong")
test_true(is_kana(u'っ'), u"Kana check wrong")
test_true(is_kana(u'つ'), u"Kana check wrong")
test_true(is_kana(u'を'), u"Kana check wrong")
test_true(is_kana(u'ッ'), u"Kana check wrong")
test_true(is_kana(u'ハ'), u"Kana check wrong")
test_true(is_kana(u'〜・'), u"Kana special check wrong")
test_true(not is_kana(u'本'), u"Kana check wrong")
test_equal(kana_minus_dakuten(u'は'), u'は')
test_equal(kana_minus_dakuten(u'ば'), u'は')
test_equal(kana_minus_dakuten(u'バ'), u'ハ')
test_equal(kana_minus_dakuten(u'本'), u'本')
test_equal(''.join(kana_plus_dakuten(u'は')), u'はばぱ')
test_equal(''.join(kana_plus_dakuten(u'本')), u'本')
test_equal(''.join(kana_plus_dakuten(u'シ')), u'シジ')
test_list_equal(extend_dakuten_reading(u'しゃし'), [u'しゃし', u'じゃし'])
test_list_equal(extend_mini_reading(u'し'), [u'し'])
test_list_equal(extend_mini_reading(u'いつ'), [u'いつ', u'いっ'])
test_equal(all_to_hiragana(u'ジータ'), u'じいた')