forked from scarletcho/KoG2P
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathg2p.py
473 lines (380 loc) · 11 KB
/
g2p.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# -*- coding: utf-8 -*-
"""
g2p.py
~~~~~~~~~~
This script converts Korean graphemes to romanized phones and then to pronunciation.
(1) graph2phone: convert Korean graphemes to romanized phones
(2) phone2prono: convert romanized phones to pronunciation
(3) graph2phone: convert Korean graphemes to pronunciation
Usage: $ python g2p.py '스물 여덟째 사람'
(NB. Please check 'rulebook_path' before usage.)
Yejin Cho ([email protected])
Jaegu Kang ([email protected])
Hyungwon Yang ([email protected])
Yeonjung Hong ([email protected])
Created: 2016-08-11
Last updated: 2019-01-31 Yejin Cho
* Key updates made:
- G2P Performance test available ($ python g2p.py test)
- G2P verbosity control available
Update by Cardroid6
"""
import datetime as dt
import math
import optparse
import re
from typing import List, Union
# Option
parser = optparse.OptionParser()
parser.add_option(
"-v",
action="store_true",
dest="verbose",
default="False",
help="This option prints the detail information of g2p process.",
)
(options, args) = parser.parse_args()
verbose = options.verbose
class Phone:
def __init__(
self,
cv_type: str,
position: str,
hangul: str,
symbol: str,
):
self._cv_type = cv_type
self._position = position
self._hangul = hangul
self._symbol = symbol
@property
def cv_type(self):
return self._cv_type
@property
def position(self):
return self._position
@property
def hangul(self):
return self._hangul
@property
def symbol(self):
return self._symbol
class RuleBook:
def __init__(self, rules, phns: List[Phone]):
self._rules = []
self._hangul_dict = {}
self._symbol_dict = {}
for rule in phns:
self._rules.append(rule)
@property
def rules(self):
return self._rules
@property
def hangul_dict(self):
return self._hangul_dict
@property
def symbol_dict(self):
return self._symbol_dict
def writefile(body, fname):
with open(fname, "w", encoding="utf-8") as f:
for line in body:
f.write("{}\n".format(line))
def read_phonebook(phone_book_path):
lines = []
with open(phone_book_path, "r", encoding="utf-8") as f:
lines = f.readlines()
phn_list = []
hangul_dict = {}
symbol_dict = {}
for line in lines:
line = line.strip()
if line == "":
continue
tokens = line.split("\t")
# C/V_Type Position Hangul Symbol
cv_type = tokens[0]
position = tokens[1]
hangul = tokens[2]
symbol = tokens[3]
phn = Phone(cv_type, position, hangul, symbol)
phn_list.append(phn)
hangul_dict[phn.hangul] = phn
symbol_dict[phn.symbol] = phn
return phn_list, hangul_dict, symbol_dict
def read_rulebook(rule_book_path):
lines = []
with open(rule_book_path, "r", encoding="utf-8") as f:
lines = f.readlines()
rule_in = []
rule_out = []
for line in lines:
line = line.strip()
if line != "":
if line[0] != "#":
IOlist = line.split("\t")
rule_in.append(IOlist[0])
if IOlist[1]:
rule_out.append(IOlist[1])
else: # If output is empty (i.e. deletion rule)
rule_out.append("")
return rule_in, rule_out
def read_testset(testset_path):
lines = []
with open(testset_path, "r", encoding="utf-8") as f:
lines = f.readlines()
test_in = []
test_out = []
for line in lines:
line = line.strip()
if line != "" and line[0] != "#":
IOlist = line.split("\t")
test_in.append(IOlist[0])
if IOlist[1]:
test_out.append(IOlist[1])
else: # If output is empty (i.e. deletion rule)
test_out.append("")
return test_in, test_out
def is_hangul(charint):
hangul_init = 44032
hangul_fin = 55203
return charint >= hangul_init and charint <= hangul_fin
def check_char_type(var_list):
# 1: whitespace
# 0: hangul
# -1: non-hangul
checked = []
for i in range(len(var_list)):
if var_list[i] == 32: # whitespace
checked.append(1)
elif is_hangul(var_list[i]): # Hangul character
checked.append(0)
else: # Non-hangul character
checked.append(-1)
return checked
def graph2phone(graphs: Union[str, bytes]):
# Encode graphemes as utf8
if isinstance(graphs, bytes):
try:
graphs = graphs.decode("utf-8")
except AttributeError:
pass
integers = []
for i in range(len(graphs)):
integers.append(ord(graphs[i]))
# Romanization (according to Korean Spontaneous Speech corpus; 성인자유발화코퍼스)
phones = ""
ONS = [
"k0",
"kk",
"nn",
"t0",
"tt",
"rr",
"mm",
"p0",
"pp",
"s0",
"ss",
"oh",
"c0",
"cc",
"ch",
"kh",
"th",
"ph",
"h0",
]
NUC = [
"aa",
"qq",
"ya",
"yq",
"vv",
"ee",
"yv",
"ye",
"oo",
"wa",
"wq",
"wo",
"yo",
"uu",
"wv",
"we",
"wi",
"yu",
"xx",
"xi",
"ii",
]
COD = [
"",
"kf",
"kk",
"ks",
"nf",
"nc",
"nh",
"tf",
"ll",
"lk",
"lm",
"lb",
"ls",
"lt",
"lp",
"lh",
"mf",
"pf",
"ps",
"s0",
"ss",
"oh",
"c0",
"ch",
"kh",
"th",
"ph",
"h0",
]
# Pronunciation
idx = check_char_type(integers)
iElement = 0
while iElement < len(integers):
if idx[iElement] == 0: # not space characters
base = 44032
df = int(integers[iElement]) - base
iONS = int(math.floor(df / 588)) + 1
iNUC = int(math.floor((df % 588) / 28)) + 1
iCOD = int((df % 588) % 28) + 1
s1 = "-" + ONS[iONS - 1] # onset
s2 = NUC[iNUC - 1] # nucleus
if COD[iCOD - 1]: # coda
s3 = COD[iCOD - 1]
else:
s3 = ""
tmp = s1 + s2 + s3
phones = phones + tmp
elif idx[iElement] == 1: # space character
tmp = "#"
phones = phones + tmp
phones = re.sub("-(oh)", "-", phones)
iElement += 1
tmp = ""
# 초성 이응 삭제
phones = re.sub("^oh", "", phones)
phones = re.sub("-(oh)", "", phones)
# 받침 이응 'ng'으로 처리 (Velar nasal in coda position)
phones = re.sub("oh-", "ng-", phones)
phones = re.sub("oh([# ]|$)", "ng", phones)
# Remove all characters except Hangul and syllable delimiter (hyphen; '-')
phones = re.sub(r"(\W+)\-", "\\1", phones)
phones = re.sub(r"\W+$", "", phones)
phones = re.sub(r"^\-", "", phones)
return phones
def phone2prono(phones, rule_in, rule_out):
# Apply g2p rules
for pattern, replacement in zip(rule_in, rule_out):
# print pattern
phones = re.sub(pattern, replacement, phones)
prono = phones
return prono
def add_phone_boundary(phones):
# Add a comma (,) after every second alphabets to mark phone boundaries
ipos = 0
newphones = ""
while ipos + 2 <= len(phones):
if phones[ipos] == "-":
newphones = newphones + phones[ipos]
ipos += 1
elif phones[ipos] == " ":
ipos += 1
elif phones[ipos] == "#":
newphones = newphones + phones[ipos]
ipos += 1
newphones = newphones + phones[ipos] + phones[ipos + 1] + ","
ipos += 2
return newphones
def add_space(phones):
ipos = 0
newphones = ""
while ipos < len(phones):
if ipos == 0:
newphones = newphones + phones[ipos] + phones[ipos + 1]
else:
newphones = newphones + " " + phones[ipos] + phones[ipos + 1]
ipos += 2
return newphones
def graph2prono(graphs, rule_in, rule_out):
romanized = graph2phone(graphs)
romanized_bd = add_phone_boundary(romanized)
prono = phone2prono(romanized_bd, rule_in, rule_out)
prono = re.sub(",", " ", prono)
prono = re.sub(" $", "", prono)
prono = re.sub("#", "-", prono)
prono = re.sub("-+", "-", prono)
prono_prev = prono
identical = False
loop_cnt = 1
if verbose:
print("=> Romanized: " + romanized)
print("=> Romanized with boundaries: " + romanized_bd)
print("=> Initial output: " + prono)
while not identical:
prono_new = phone2prono(re.sub(" ", ",", prono_prev + ","), rule_in, rule_out)
prono_new = re.sub(",", " ", prono_new)
prono_new = re.sub(" $", "", prono_new)
if re.sub("-", "", prono_prev) == re.sub("-", "", prono_new):
identical = True
prono_new = re.sub("-", "", prono_new)
if verbose:
print("\n=> Exhaustive rule application completed!")
print("=> Total loop count: " + str(loop_cnt))
print("=> Output: " + prono_new)
else:
if verbose:
print("\n=> Rule applied for more than once")
print("cmp1: " + re.sub("-", "", prono_prev))
print("cmp2: " + re.sub("-", "", prono_new))
loop_cnt += 1
prono_prev = prono_new
return prono_new
def test_g2p(rule, testset):
(testin, testout) = read_testset(testset)
cnt = 0
body = []
for idx in range(0, len(testin)):
print("Test item #: " + str(idx + 1) + "/" + str(len(testin)))
item_in = testin[idx]
item_out = testout[idx]
ans = graph2phone(item_out)
ans = re.sub("-", "", ans)
ans = add_space(ans)
(rule_in, rule_out) = rule
pred = graph2prono(item_in, rule_in, rule_out)
if pred != ans:
print("G2P ERROR: [result] " + pred + "\t\t\t[ans] " + item_in + " [" + item_out + "] " + ans)
cnt += 1
else:
body.append("[result] " + pred + "\t\t\t[ans] " + item_in + " [" + item_out + "] " + ans)
print("Total error item #: " + str(cnt))
writefile(body, "good.txt")
def run_g2p(graph, rule):
(rule_in, rule_out) = rule
return graph2prono(graph, rule_in, rule_out)
def run_test(rule, testset):
print("[ G2P Performance Test ]")
beg = dt.datetime.now()
test_g2p(rule, testset)
end = dt.datetime.now()
print("Total time: ")
print(end - beg)
# Usage:
if __name__ == "__main__":
rule = read_rulebook("rulebook.txt")
if args[0] == "test": # G2P Performance Test
run_test(rule, "testset.txt")
else:
graph = args[0]
prono = run_g2p(graph, rule)
print(prono)