-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAlexaKorean.py
319 lines (266 loc) · 9.86 KB
/
AlexaKorean.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
314
315
316
317
318
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals
from functools import partial
import re
def concat(dlst): # flatten
return [x for lst in dlst for x in lst]
def partition(fun, col):
if not col:
return []
ret = []
it = iter(col)
x = next(it)
acc = [x]
last = fun(x)
for x in it:
curr = fun(x)
if curr == last:
acc.append(x)
else:
ret.append(acc)
acc = [x]
last = curr
ret.append(acc)
return ret
class AlexaKorean:
initials = [
# ㄱ ㄲ ㄴ ㄷ ㄸ
("giyeok","g","g"), ("ssanggiyeok","gˈ","g\""), ("nieun","n","n"), ("digeut","d","d"), ("ssangdigeut","dˈ","d\""),
# ㄹ ㅁ ㅂ ㅃ ㅅ
("rieul","ɹ","r\\"), ("mieum","m","m"), ("bieup","b","b"), ("ssangbieup","pˈ","p\""), ("siot","s","s"),
# ㅆ ㅇ ㅈ ㅉ ㅊ
("ssangsiot","sˈ","s\""), ("ieung","",""), ("jieut","d͡ʒ","dZ"), ("ssangjieut","d͡ʒˈ","dZ\""), ("chieut","t͡ʃ","tS"),
#ㅋ ㅌ ㅍ ㅎ
("kieuk","k","k"), ("tieut","t","t"), ("pieup","p","p"), ("hieut","h","h")
]
medials = [
# ㅏ ㅐ ㅑ ㅒ ㅓ
("a","ɑ","A"), ("ae","ɛ","E"), ("ya","jɑ","jA"), ("yae","jɛ","jE"), ("eo","ʌ","V"),
# ㅔ ㅕ ㅖ ㅗ ㅘ
("e","ɛ","E"), ("yeo","jʌ","jV"), ("ye","jɛ","jE"), ("o","oʊ","oU"), ("wa","wɑ","wA"),
# ㅙ ㅚ ㅛ ㅜ ㅝ
("wae","wɛ","wE"), ("oe","ə","@"), ("yo","joʊ","joU"), ("u","ʊ","U"), ("wo","wɚ", "w@`"),
# ㅞ ㅟ ㅠ ㅡ ㅢ
("we","wɛ","wE"), ("wi","wi","wi"), ("yu","ju","jU"), ("eu","",""), ("ui","ʊ","U"),
#ㅣ
("i","i","i")
]
# ("ae","æ","{"), ("eu","ʊ","U"), ("i","ɪ","I")
finals = [
# none ㄱ ㄲ ㄳ ㄴ
("none","",""), ("giyeok","g","g"), ("ssanggiyeok","k","k"), ("giyeok-siot","g","g"), ("nieun","n","n"),
# ㄵ ㄶ ㄷ ㄹ ㄺ
("nieun-jieut","nd͡ʒ","ndZ"), ("nieun-hieut","n","n"), ("digeut","d","d"), ("rieul","l","l"), ("rieul-giyeok","k","k"),
# ㄻ ㄼ ㄽ ㄾ ㄿ
("rieul-mieum","m","m"), ("rieul-bieup","l","l"), ("rieul-siot","l","l"), "rieul-tieut", "rieul-pieup",
# ㅀ ㅁ ㅂ ㅄ ㅅ
"rieul-hieut", ("mieum","m","m"), ("bieup","b","b"), ("bieup-siot","b","b"), ("siot","s","s"),
# ㅆ ㅇ ㅈ ㅊ ㅋ
("ssangsiot","d","d"), ("ieung","ŋ","N"), ("jieut","d͡ʒ","dZ"), ("chieut","t͡ʃ","tS"), ("kieuk","k","k"),
# ㅌ ㅍ ㅎ
("tieut","t","t"), ("pieup","p","p"), ("hieut","d","d")
]
IPA = 1
XSAMPA = 2
@staticmethod
def notation_name(notation):
if notation == AlexaKorean.IPA:
return "ipa"
if notation == AlexaKorean.XSAMPA:
return "x-sampa"
return ""
@staticmethod
def is_korean(c):
return c >= "가" and c <= "힣"
@staticmethod
def is_upper(c):
return c >= 'A' and c <= 'Z'
@staticmethod
def parse_characters_by_type(s):
par = partition(AlexaKorean.is_korean, s)
par = concat([partition(AlexaKorean.is_upper, a) for a in par])
return ["".join(x) for x in par]
@staticmethod
def parse_korean_character_by_jamo(ch):
c = ord(ch)
c -= 44032
final = c % 28
c //= 28
medial = c % 21
c //= 21
initial = c
return (initial, medial, final)
@staticmethod
def phonological_transform(syls):
return syls
@staticmethod
def read_syllables(syls, notation):
if not (notation == AlexaKorean.IPA or notation == AlexaKorean.XSAMPA):
return []
return "".join([AlexaKorean.read_syllable(syl, notation)
for syl in syls])
@staticmethod
def read_syllable(syl, notation):
return "%s%s%s." % (AlexaKorean.initials[syl[0]][notation],
AlexaKorean.medials[syl[1]][notation],
AlexaKorean.finals[syl[2]][notation])
@staticmethod
def speak(s, notation = IPA):
for proc in [JamoProcessor(), DigitsProcessor(), NumberProcessor(),
PostpositionProcessor()]:
s = proc.pattern().sub(proc.transform(), s)
splitted = AlexaKorean.parse_characters_by_type(s)
return "".join(map(partial(AlexaKorean._speak, notation = notation),
splitted))
@staticmethod
def _speak(seg, notation):
if AlexaKorean.is_korean(seg):
lst = [AlexaKorean.parse_korean_character_by_jamo(x) for x in seg]
ph = AlexaKorean.read_syllables(
AlexaKorean.phonological_transform(lst),
notation)
return '<phoneme alphabet="%s" ph="%s">%s</phoneme>' % \
(AlexaKorean.notation_name(notation), ph, seg)
elif AlexaKorean.is_upper(seg) and len(seg) > 1:
return '<say-as interpret-as="characters">%s</say-as>' % seg
else:
return seg
class AlexaKoreanProcessor:
def pattern(self):
raise NotImplementError
def transform(self):
raise NotImplementError
class JamoProcessor(AlexaKoreanProcessor):
jamo_name_table = {
'ㄱ': '기역', 'ㄲ': '쌍기역', 'ㄳ': '기역시옷', 'ㄴ': '니은', 'ㄵ': '니은지읒',
'ㄶ': '니은히읗', 'ㄷ': '디귿', 'ㄸ': '쌍디귿', 'ㄹ': '리을', 'ㄺ': '리을기역',
'ㄻ': '리을미음', 'ㄼ': '리을비읍', 'ㄽ': '리을시옷', 'ㄾ': '리을티읕', 'ㄿ': '리을피읖',
'ㅀ': '리을히읗', 'ㅁ': '미음', 'ㅂ': '비읍', 'ㅃ': '쌍비읍', 'ㅄ': '비읍시옷',
'ㅅ': '시옷', 'ㅆ': '쌍시옷', 'ㅇ': '이응', 'ㅈ': '지읒', 'ㅉ': '쌍지읒',
'ㅊ': '치읓', 'ㅋ': '키읔', 'ㅌ': '티읕', 'ㅍ': '피읖', 'ㅎ': '히읗',
'ㅏ': '아', 'ㅐ': '애', 'ㅑ': '야', 'ㅒ': '얘', 'ㅓ': '어',
'ㅔ': '에', 'ㅕ': '여', 'ㅖ': '예', 'ㅗ': '오', 'ㅘ': '오아',
'ㅙ': '오애', 'ㅚ': '오이', 'ㅛ': '요', 'ㅜ': '우', 'ㅝ': '우어',
'ㅞ': '우에', 'ㅟ': '우이', 'ㅠ': '유', 'ㅡ': '으', 'ㅢ': '으이',
'ㅣ': '이'
}
_pattern = re.compile('[ㄱ-ㅎㅏ-ㅣ]')
def pattern(self):
return JamoProcessor._pattern
def transform(self):
return lambda match: JamoProcessor.jamo_name_table[match.group(0)]
class DigitsProcessor(AlexaKoreanProcessor):
digits = {
'0': '영', '1': '일', '2': '이', '3': '삼', '4': '사',
'5': '오', '6': '육', '7': '칠', '8': '팔', '9': '구',
'-': '빼기'
}
_pattern = re.compile('{[0-9]+(?:-[0-9]+)*}')
def pattern(self):
return DigitsProcessor._pattern
def transform(self):
return lambda match: "".join(
map(lambda c: DigitsProcessor.digits[c], match.group(0)[1:-1])
)
class NumberProcessor(AlexaKoreanProcessor):
digits = {
'0': '영', '1': '일', '2': '이', '3': '삼', '4': '사',
'5': '오', '6': '육', '7': '칠', '8': '팔', '9': '구',
'.': '점'
}
digits_wo_zero = {
'1': '', '2': '이', '3': '삼', '4': '사', '5': '오',
'6': '육', '7': '칠', '8': '팔', '9': '구'
}
_pattern = re.compile(
'(?P<minus>-)?(?P<integral>[0-9]+)(?P<decimal>\.[0-9]+)?')
def pattern(self):
return NumberProcessor._pattern
def transform(self):
def lbd(match):
ret = []
if match.group('minus'):
ret.append('마이너스')
int_part = match.group('integral')[::-1]
if int_part == "0":
ret.append("영")
else:
int = []
delim = self.delim_gen()
for c in int_part:
delim
int.append(next(delim)(c))
int.reverse()
ret.append("".join(int).rstrip())
if match.group('decimal'):
ret = ret + map(lambda c: NumberProcessor.digits[c],
match.group('decimal'))
return "".join(ret)
return lbd
def delim_gen(self):
delims = ["", "만 ", "억 ", "조 ", "경 ", "해 ", "자 ", "양 ", "구 "]
while True:
dl = delims.pop(0)
yield lambda x: "일" + dl if x == '1' else \
(NumberProcessor.digits_wo_zero[x] + dl if x != '0' else dl)
yield lambda x: \
NumberProcessor.digits_wo_zero[x] + "십" if x != '0' else ""
yield lambda x: \
NumberProcessor.digits_wo_zero[x] + "백" if x != '0' else ""
yield lambda x: \
NumberProcessor.digits_wo_zero[x] + "천" if x != '0' else ""
class PostpositionProcessor(AlexaKoreanProcessor):
_HAVE_FINAL_EXCEPT_RIEUL = 1
_HAVE_FINAL_RIEUL = 2
_HAVE_NO_FINALS = 3
_UNKNOWN = 4
_depend_on_final = {
("와", _HAVE_FINAL_EXCEPT_RIEUL): "와", ("와", _HAVE_FINAL_RIEUL): "와",
("와", _HAVE_NO_FINALS): "과",
("과", _HAVE_FINAL_EXCEPT_RIEUL): "와", ("과", _HAVE_FINAL_RIEUL): "와",
("과", _HAVE_NO_FINALS): "과",
("으로", _HAVE_FINAL_EXCEPT_RIEUL): "으로",
("으로", _HAVE_FINAL_RIEUL): "로", ("으로", _HAVE_NO_FINALS): "로",
("로", _HAVE_FINAL_EXCEPT_RIEUL): "으로",
("로", _HAVE_FINAL_RIEUL): "로", ("로", _HAVE_NO_FINALS): "로",
("은", _HAVE_FINAL_EXCEPT_RIEUL): "은", ("은", _HAVE_FINAL_RIEUL): "은",
("은", _HAVE_NO_FINALS): "는",
("는", _HAVE_FINAL_EXCEPT_RIEUL): "은", ("는", _HAVE_FINAL_RIEUL): "은",
("는", _HAVE_NO_FINALS): "는",
("을", _HAVE_FINAL_EXCEPT_RIEUL): "을", ("을", _HAVE_FINAL_RIEUL): "을",
("을", _HAVE_NO_FINALS): "를",
("를", _HAVE_FINAL_EXCEPT_RIEUL): "을", ("를", _HAVE_FINAL_RIEUL): "을",
("를", _HAVE_NO_FINALS): "를",
("이", _HAVE_FINAL_EXCEPT_RIEUL): "이", ("이", _HAVE_FINAL_RIEUL): "이",
("이", _HAVE_NO_FINALS): "가",
("가", _HAVE_FINAL_EXCEPT_RIEUL): "이", ("가", _HAVE_FINAL_RIEUL): "이",
("가", _HAVE_NO_FINALS): "가"
}
_pattern = re.compile('.{([와과로은는을를이가]|으로)}')
def pattern(self):
return PostpositionProcessor._pattern
def transform(self):
def lbd(match):
type = PostpositionProcessor._UNKNOWN
matched = match.group(0)
target = match.group(1)
prec = matched[0]
if prec >= "가" and prec <= "힣":
if (ord(prec) - 44032) % 28 == 0:
type = PostpositionProcessor._HAVE_NO_FINALS
elif (ord(prec) - 44032) % 28 == 8:
type = PostpositionProcessor._HAVE_FINAL_RIEUL
else:
type = PostpositionProcessor._HAVE_FINAL_EXCEPT_RIEUL
elif prec == "ㄹ":
type = PostpositionProcessor._HAVE_FINAL_RIEUL
elif prec >= "ㄱ" and prec <= "ㅎ":
type = PostpositionProcessor._HAVE_FINAL_EXCEPT_RIEUL
elif prec >= "ㅏ" and prec <= "ㅣ":
type = PostpositionProcessor._HAVE_NO_FINALS
if type == PostpositionProcessor._UNKNOWN:
return prec + target
return prec + PostpositionProcessor._depend_on_final[(target, type)]
return lbd
#print(AlexaKorean.speak("짜장면"))