-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcustomize-for-YKY.py
executable file
·181 lines (147 loc) · 4.21 KB
/
customize-for-YKY.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
# -*- coding: UTF-8 -*-
# modify "exact-Google-pinyins.txt" to YKY's custom pinyins
# Format of input file:
# =====================
# "字pinyin" or "字字pinyin"
# Output file same format.
# **** Old consonants & vowels from Google pinyin ****
consonants = ['b','ch','d','dy','f','g','gw','gy','h','hm','hy','j','jy','k','kw','ky','l','ly','m','n','ng','ny','p','s','sy','t','ty','w','y']
vowels = ['a','aai','aak','aam','aan','aang','aap','aat','aau','ai','ak','am','an','ang','ap','at','au','e','ei','ek','eng','eu','eui','euk','eun','eung','eut','i','ik','im','in','ing','ip','it','iu','o','oi','ok','on','ong','ot','ou','u','ui','uk','un','ung','ut','yu','yun','yut']
# new consonants = ['b','ch','d','f','g','gw','gy','h','hm','j','k','kw','l','m','n','ng','p','s','t','ty','w','y']
# new vowels = ['a','aai','aak','aam','aan','aang','aap','aat','aau','ai','ak','am','an','ang','ao','ap','at','au','e','ei','ek','eng','er','erk','eu','eui','euk','eun','eung','eut','i','ik','im','in','ing','ip','it','iu','o','oi','ok','ol''on','ong','ot','ou','u','ud','ue','uen','ui','uk','un','ung','ur','ut','yu','yun','yut']
replace_consonants = {
"n" : ["n", "l"],
}
replace_finals = {
"aai" : ["ai","aai"],
"aak" : ["ak","aak"],
"aam" : ["am","aam"],
"aan" : ["an","aan"],
"aang" : ["ang","aang"],
"aap" : ["ap","aap"],
"aat" : ["at","aat"],
"aau" : ["au","aau"],
"ou" : ["o","ol", "ou"],
"at" : ["at", "ud"],
"au" : ["au", "ao"],
}
replace_entire = {
"nei" : ["ni", "nei"],
"mut" : ["mut", "mud"],
"ngoi" : ["ngoi", "oi", "ao"],
"gwok" : ["gwok", "gok"],
"gwo" : ["go", "gor", "gou"],
"deu" : ["der","deu"],
"geu" : ["gur","geu"],
"heu" : ["her","heu"],
"teu" : ["ter","teu"],
"cheui" : ["chui"],
"deui" : ["due"],
"geui" : ["gue"],
"heui" : ["hue"],
"jeui" : ["jui"],
"keui" : ["kue"],
"leui" : ["lui"],
"neui" : ["nui"],
"seui" : ["sui"],
"teui" : ["tue"],
"yeui" : ["yue"],
"cheuk" : ["cherk","cheuk"],
"deuk" : ["derk","deuk"],
"geuk" : ["gue","geuk"],
"jeuk" : ["juk","jeuk"],
"keuk" : ["kue","keuk"],
"leuk" : ["lue","leuk"],
"seuk" : ["shue","seuk"],
"yeuk" : ["yue", "yerk", "yeuk"],
"cheun" : ["chun","cheun"],
"deun" : ["dun","deun"],
"jeun" : ["jun","jeun"],
"leun" : ["lun","leun"],
"seun" : ["shun","seun"],
"teun" : ["tun","teun"],
"yeun" : ["yun","yeun"],
"cheut" : ["chut"],
"deut" : ["due"],
"jeut" : ["jue"],
"leut" : ["lud"],
"seut" : ["shut", "shue"],
"chyu" : ["chu"],
"chyun" : ["chuen"],
"chyut" : ["chut"],
"dyut" : ["due"],
"dyun" : ["duen"],
"gyut" : ["gue"],
"gyun" : ["guen"],
"hyut" : ["hue"],
"hyun" : ["huen"],
"jyut" : ["chut"],
"jyun" : ["juen"],
"jyu" : ["juk", "ju", "chu" ],
"kyut" : ["kue"],
"kyun" : ["kuen"],
"lyut" : ["lue"],
"lyun" : ["luen"],
"nyun" : ["luen"],
"syut" : ["shue"],
"syun" : ["suen"],
"syu" : ["shu"],
"tyut" : ["tue"],
}
# Note: if a consonant is 2-chars, it must be recognized first
def k_n(pinyin):
c = pinyin[0]
cc = pinyin[0:2]
k = ""
n = ""
if cc in consonants:
k = cc
n = pinyin[2:]
elif c in consonants:
k = c
n = pinyin[1:]
else:
n = pinyin
return (k, n)
import codecs
# open input file
f1 = codecs.open("web/exact-Google-pinyins.txt", encoding="UTF-8", mode="r")
# output file
fo = codecs.open("web/YKY-custom-pinyins-tmp.txt", encoding="UTF-8", mode='w')
for line in f1:
c = ord(line[1])
if c > 255: # ignore phrases
continue
print (line[:-1], end=' ')
char = line[0]
pinyin = line[1:-1]
if pinyin in replace_entire:
for r in replace_entire[pinyin]:
fo.write(char + r + '\n')
continue
fo.write(char + pinyin + '\n')
f1.close()
fo.close()
print("**** Phase 2 ****")
f1 = codecs.open("web/YKY-custom-pinyins-tmp.txt", encoding="UTF-8", mode="r")
fo = codecs.open("web/YKY-custom-pinyins.txt", encoding="UTF-8", mode='w')
for line in f1:
print (line[:-1], end=' ')
char = line[0]
pinyin = line[1:-1]
(k, n) = k_n(pinyin)
if k in replace_consonants:
for k2 in replace_consonants[k]:
if n in replace_finals:
for n2 in replace_finals[n]:
fo.write(char + k2 + n2 + '\n')
else:
fo.write(char + k2 + n + '\n')
continue
elif n in replace_finals:
for n2 in replace_finals[n]:
fo.write(char + k + n2 + '\n')
continue
fo.write(char + pinyin + '\n')
f1.close()
fo.close()