forked from mozillazg/phrase-pinyin-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge.py
58 lines (44 loc) · 1.2 KB
/
merge.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
# -*- coding: utf-8 -*-
import sys
import codecs
def parse(lines):
"""
:yield: hanzi, others
"""
for line in lines:
line = line.strip()
if line.startswith('#') or not line:
continue
hanzi, others = line.split(':', 1)
yield hanzi.strip(), others.strip()
def merge(pinyin_d_list):
"""
:rtype: dict
"""
final_d = {}
for overwrite_d in pinyin_d_list:
final_d.update(overwrite_d)
return final_d
def sort(pinyin_d):
"""
:rtype: list
"""
return sorted(pinyin_d.items(), key=lambda x: x[0])
def output(pinyin_s):
print('# version: 0.12.0')
print('# source: https://github.com/mozillazg/phrase-pinyin-data')
for hanzi, pinyin in pinyin_s:
hanzi = hanzi.split('_')[0]
print('{hanzi}: {pinyin}'.format(hanzi=hanzi, pinyin=pinyin))
def main(files):
pinyin_d_list = []
for name in files:
with codecs.open(name, 'r', 'utf-8-sig') as fp:
d = {}
for h, p in parse(fp):
d.setdefault(h, p)
pinyin_d_list.append(d)
pinyin_d = merge(pinyin_d_list)
output(sort(pinyin_d))
if __name__ == '__main__':
main(sys.argv[1:])