-
Notifications
You must be signed in to change notification settings - Fork 1
/
custom.py
272 lines (215 loc) · 6.82 KB
/
custom.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
import collections
def is_chinese(uchar):
"""判断一个unicode是否是汉字"""
return uchar >= u'\u4e00' and uchar <= u'\u9fa5'
def is_can_stay(uchar):
if is_chinese(uchar) and len(uchar) > 1:
return False
if uchar.startswith("?"):
return False
return True
def is_line_can_stay(line):
first = str(line.split("\t")[0])
if not is_can_stay(first):
print("trim", line)
return False
return True
def get_single_char_line(line):
if not is_line_can_stay(line):
return
return line
def simp_file(name, out):
simp_file_fn(name, out, get_single_char_line)
def simp_file_fn(name, out, fn):
mark="..."
lines = []
begin = False
with open(name, "r", encoding="utf8") as f:
for line in f:
if begin:
line = fn(line)
else:
if line.startswith(mark):
begin = True
if line:
lines.append(line)
if not begin:
print("never begin")
return
with open(out, "w+", encoding="utf8") as f:
f.writelines(lines)
def get_flypy_no_end_line():
word_to_py = collections.defaultdict(set)
def fn(line):
args = line.split("\t")
word = args[0]
if not is_chinese(word):
return
py = args[1]
if len(py) > 2:
py = py[:2]
if py in word_to_py[word]:
return
word_to_py[word].add(py)
args[1] = py
return "\t".join(args) + "\n"
return fn
def flypy_no_end(filename):
"""小鹤双拼去掉加形"""
simp_file_fn(filename, filename + ".no_end", get_flypy_no_end_line())
def get_word_to_code(filename):
word_to_code = collections.defaultdict(list)
begin = False
with open(filename, "r", encoding="utf8") as f:
for line in f:
if begin:
args = line.split("\t")
if len(args) < 2:
continue
word = args[0]
code = args[1].strip()
word_to_code[word].append(code)
else:
if line.startswith("..."):
begin = True
return word_to_code
def get_line_to_dict(filename, func):
ret = collections.defaultdict(list)
begin = False
with open(filename, "r", encoding="utf8") as f:
for line in f:
if begin:
args = line.split("\t")
if len(args) < 2:
continue
key, value = func(args)
if key is None:
continue
ret[key].append(value)
else:
if line.startswith("..."):
begin = True
return ret
def get_word_to_code(filename):
def fn(args):
word = args[0]
code = args[1].strip()
return word, code
return get_line_to_dict(filename, fn)
def get_code_to_word(filename):
def fn(args):
word = args[0]
code = args[1].strip()
return code, word
return get_line_to_dict(filename, fn)
FLYPY_WUBI_PREFIX = """
# Rime dict
# encoding: utf-8
# 小鹤双拼加五笔形码
# 如"这"字,双拼码为ve,五笔码为yp,则加形后的码为vey
---
name: flypy_yk.wubi
version: "0.0.1"
sort: original
use_preset_vocabulary: false
...
"""
def to_flypy_wubi(filename="flypy_yk.wubi.dict.yaml"):
word_to_wubi = get_word_to_code("wubi86_jidian.dict.yaml")
word_to_py = get_word_to_code("flypy_yk.base.dict.yaml")
print("wubi", len(word_to_wubi))
print("flypy", len(word_to_py))
lines = [FLYPY_WUBI_PREFIX]
for word, pys in word_to_py.items():
wubis = word_to_wubi.get(word)
if not wubis:
continue
for py in pys:
wubi = max(wubis, key=len)
lines.append(word + "\t" + py + wubi[:1] + "\n")
lines.append(word + "\t" + py + wubi[:2] + "\n")
with open(filename, "w", encoding="utf8") as f:
f.writelines(lines)
def code_word_swap(filename):
def fn(line):
args = line.split("\t")
if len(args) < 2:
return
code = args[0].strip()
word = args[1].strip()
return word + "\t" + code + "\n"
simp_file_fn(filename, filename + ".swap", fn)
def add_low_weight(filename):
def fn(line):
args = line.split("\t")
if len(args) < 2:
return
code = args[1].strip()
word = args[0].strip()
return f"{word}\t{code}\t{41848}\n"
simp_file_fn(filename, "low."+filename, fn)
def merge(to_name, *filenames):
begin = False
lines = []
for filename in filenames:
with open(filename, "r", encoding="utf8") as f:
for line in f:
if begin:
args = line.split("\t")
if len(args) < 2:
continue
word = args[0]
code = args[1].strip()
weight = len(lines) + 1
lines.append(f"{word}\t{code}\t{weight}\n")
else:
if line.startswith("..."):
begin = True
with open(to_name, "w", encoding="utf8") as f:
f.writelines(lines)
def check_same_code_with_other(a, *args):
code_to_word1 = get_code_to_word(a)
# code_to_word2 = get_code_to_word(b)
code_to_word2 = {}
for i in args:
code_to_word2.update(get_code_to_word(i))
same_keys = code_to_word1.keys() & code_to_word2.keys()
for k in same_keys:
print(k, code_to_word1[k])
print(k, code_to_word2[k])
def check_same_code(a):
code_to_word1 = get_code_to_word(a)
for k, v in code_to_word1.items():
if len(v) == 4:
print(k, v)
def wubilevel2(filename):
def fn(args):
word = args[0]
code = args[1].strip()
if len(code) != 2:
return None, None
return code, word
code_to_word = get_line_to_dict(filename, fn)
l = set([i[0] for i in code_to_word.values()])
print("".join(l))
def main():
# simp_file("wubi86_jidian.dict.yaml.org", "wubi86_jidian.dict.yaml")
# simp_file("double_pinyin_flypy.dict.yaml.org", "double_pinyin_flypy.dict.yaml")
# code_word_swap("x.yaml")
# flypy_no_end("x.yaml.swap")
# to_flypy_wubi("flypy_yk.wubi.dict.yaml")
# add_low_weight("flypy_yk.wubi.dict.yaml")
# merge("to.txt",
# "wubi/wubi86_ms.dict.yaml",
# "flypy_yk/flypy_yk.base.dict.yaml",
# "flypy_yk/flypy_yk.wubi.dict.yaml",
# )
# check_same_code_with_other("wubi/wubi86_ms.dict.yaml",
# "flypy_yk/flypy_yk.base.dict.yaml",
# "flypy_yk/flypy_yk.wubi.dict.yaml",
# )
# check_same_code("wubi/wubi86_ms.dict.yaml")
wubilevel2("wubi/wubi86_ms.dict.yaml")
print("done")
if __name__ == "__main__":
main()