-
Notifications
You must be signed in to change notification settings - Fork 0
/
ext_korean.py
313 lines (259 loc) · 7.84 KB
/
ext_korean.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
"""
Utilities for dealing with Korean
"""
from jamo import h2j, j2hcj
def isJamo(c):
assert len(c) == 1
# HANGUL JAMO: (U+1100-U+11FF)
# HANGUL COMPATIBILITY JAMO: (U+3130-U+318F)
if ord(c) >= 0x1100 and ord(c) <= 0x11FF:
return True
elif ord(c) >= 0x3130 and ord(c) <= 0x318F:
return True
else:
return False
def strIsHangul(s):
for c in s:
if not isHangul(c):
return False
return True
def strIsHangulOrJamo(s):
for c in s:
if not isHangul(c) and not isJamo(c):
return False
return True
def isHangul(c):
return ord(c) >= 0xAC00 and ord(c) <= 0xD7A3
def jamoTail(c):
assert len(c) == 1
return int((ord(c) - 44032) % 28)
def jamoVowel(c):
assert len(c) == 1
return 1 + (((ord(c) - 44032 - jamoTail(c)) % 588) // 28)
def jamoLead(c):
assert len(c) == 1
return 1 + ((ord(c) - 44032) // 588)
def assembleHangul(lead, vowel, tail):
return chr(tail + (vowel-1)*28 + (lead-1)*588 + 44032)
def removeFinalJamo(c):
assert len(c) == 1
lead, vowel = jamoLead(c), jamoVowel(c)
return assembleHangul(lead, vowel, 0)
def normalizeToCompatJamo(s):
out = ''
for c in s:
if isJamo(c):
out += j2hcj(c)
else:
out += c
assert len(s) == len(out)
return out
'''
('닌', '니ㄴ')
'''
def splitOffFinalJamo(c):
assert len(c) == 1
assert isHangul(c)
# important to check if there even is a tail!
# otherwise, the following call won't work
if jamoTail(c) > 0:
# get the compatibility Jamo
finalJamo = j2hcj(h2j(c)[-1])
lead, vowel = jamoLead(c), jamoVowel(c)
return assembleHangul(lead, vowel, 0) + finalJamo
else:
return c # null final: nothing to split off
'''
('군', '굴ㄴ')
'''
def R_irregularOperation(c):
assert len(c) == 1
assert isHangul(c)
# important to check if there even is a tail!
# otherwise, the following call won't work
if jamoTail(c) > 0:
# get the compatibility Jamo
finalJamo = j2hcj(h2j(c)[-1])
lead, vowel = jamoLead(c), jamoVowel(c)
# assemble with R final (R-irregular)
return assembleHangul(lead, vowel, 8) + finalJamo
else:
return c # null final: nothing to split off
'''
('래', '렇어')
('랬', '렇었')
'''
def H_irregularOperation(c):
assert len(c) == 1
assert isHangul(c)
# important to check if there even is a tail!
# otherwise, the following call won't work
if jamoVowel(c) == 2:
lead, vowel, tail = jamoLead(c), jamoVowel(c), jamoTail(c)
# lead null consonant is 12
# 애 -> 엏+어
# 앴 -> 엏+었
return assembleHangul(lead, 5, 27) + assembleHangul(12, 5, tail)
else:
return c # null final: nothing to split off
'''
('라', '르아')
('러', '르어')
('떴', '뜨었')
('빴', '쁘았')
'''
def EU_irregularOperation(c):
assert len(c) == 1
assert isHangul(c)
# important to check if there even is a tail!
# otherwise, the following call won't work
# only for 아,어
if jamoVowel(c) == 1 or jamoVowel(c) == 5:
# get the compatibility Jamo
finalJamo = j2hcj(h2j(c)[-1])
lead, vowel, tail = jamoLead(c), jamoVowel(c), jamoTail(c)
# lead null consonant is 12
# 아->으+아, or 았->으+았
# 어->으+어, or 었->으+었
return assembleHangul(lead, 19, 0) + assembleHangul(12, vowel, tail)
else:
return c # null final: nothing to split off
'''
하다-forms
('했', '하았') [('했', '하았')]
'''
def AE_irregularOperation_1(c):
assert len(c) == 1
assert isHangul(c)
# important to check if there even is a tail!
# otherwise, the following call won't work
# only for 애
if jamoVowel(c) == 2:
# get the compatibility Jamo
finalJamo = j2hcj(h2j(c)[-1])
lead, tail = jamoLead(c), jamoTail(c)
# lead null consonant is 12
# 애->아+아, or 앴->아+았
return assembleHangul(lead, 1, 0) + assembleHangul(12, 1, tail)
else:
return c # null final: nothing to split off
'''
애=>애+어, or 앴=>애+었
'''
'''
def AE_irregularOperation_2(c):
assert len(c) == 1
# important to check if there even is a tail!
# otherwise, the following call won't work
# only for 애
if jamoVowel(c) == 2:
# get the compatibility Jamo
finalJamo = j2hcj(h2j(c)[-1])
lead, tail = jamoLead(c), jamoTail(c)
# lead null consonant is 12
# 애->아+아, or 앴->아+았
return assembleHangul(lead, 1, 0) + assembleHangul(12, 1, tail)
else:
return c # null final: nothing to split off
'''
# http://www.koreanwikiproject.com/wiki/Vowel_harmony
HARMONY_YANG = 0
HARMONY_YIN = 1
HARMONY_NEUTRAL = 2
def vowelJarmony(c):
assert len(c) == 1
assert isHangul(c)
vowel = jamoVowel(c)
# YANG
# ㅏ (a) ㅑ (ya) ㅗ (o) ㅛ (yo)
# ㅐ (ae) ㅘ (wa) ㅚ (oe) ㅙ (wae)
if vowel == 1 \
or vowel == 3 \
or vowel == 9 \
or vowel == 13 \
or vowel == 2 \
or vowel == 10 \
or vowel == 12 \
or vowel == 11:
return HARMONY_YANG
# YIN
# ㅓ (eo) ㅕ (yeo) ㅜ (u) ㅠ (yu)
# ㅔ (e) ㅝ (wo) ㅟ (wi) ㅞ (we)
elif vowel == 5 \
or vowel == 7 \
or vowel == 14 \
or vowel == 18 \
or vowel == 6 \
or vowel == 15 \
or vowel == 17 \
or vowel == 16:
return HARMONY_YIN
# otherwise...
return HARMONY_NEUTRAL
'''
Depending on harmony of vowel, add 아 or 어
('빼', '빼어')
('뺐', '빼었')
...
'''
'''
def addEoOrAOperation(c):
assert len(c) == 1
assert isHangul(c)
if vowelHarmony(c) == HARMONY_YANG:
# get the compatibility Jamo
finalJamo = j2hcj(h2j(c)[-1])
lead, tail = jamoLead(c), jamoTail(c)
# lead null consonant is 12
# 애->아+아, or 앴->아+았
assert None, 'TODO'
#return assembleHangul(lead, 1, 0) + assembleHangul(12, 1, tail)
else:
return c # null final: nothing to split off
'''
'''
('잤', '자았')
'''
def duplicateVowel(c):
assert len(c) == 1
assert isHangul(c)
lead, vowel, tail = jamoLead(c), jamoVowel(c), jamoTail(c)
# lead null consonant is 12
return assembleHangul(lead, vowel, 0) + assembleHangul(12, vowel, tail)
# http://www.sayjack.com/blog/2010/06/23/korean-verb-and-adjective-conjugation/
'''
This is for regular contractions
('쳐', '치어')
('렸', '리었')
'''
def decompressVowel(c):
assert len(c) == 1
assert isHangul(c)
# if jamoTail(c) == 0:
lead, vowel, tail = jamoLead(c), jamoVowel(c), jamoTail(c)
# lead null consonant is 12
if vowel == 7: # 여
# 여=>이+어, or 였=>이+었
# print(assembleHangul(lead, 21, 0) + assembleHangul(12, 5, tail))
return assembleHangul(lead, 21, 0) + assembleHangul(12, 5, tail)
elif vowel == 2: # 애
# 애=>애+어, or 앴=>애+었
# return assembleHangul(lead, 2, 0) + assembleHangul(12, 5, tail)
# new rule:
# 애=>어+어, or 앴=>어+었
return assembleHangul(lead, 5, 0) + assembleHangul(12, 5, tail)
elif vowel == 15: # 워
# 워=>우+어, or 웠=>우+었
return assembleHangul(lead, 14, 0) + assembleHangul(12, 5, tail)
elif vowel == 10: # 와
# 와=>오+아, or 왔=>오+았
return assembleHangul(lead, 9, 0) + assembleHangul(12, 1, tail)
elif vowel == 11: # 왜
# 왜=>외+어, or 왰=>외+었
return assembleHangul(lead, 12, 0) + assembleHangul(12, 5, tail)
elif vowel == 6: # 에
# 에->이+어, or 엤=>이+었
return assembleHangul(lead, 21, 0) + assembleHangul(12, 5, tail)
else:
# no action possible
return c