Debian Codegraph @

_tools/pinyin-data/tools/gen_cc_cedict.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 from cc_cedict import phrases_dict
3
4
5 class Counter:
6 def __init__(self):
7 self._data = {}
8
9 def add(self, item):
10 if item not in self._data:
11 self._data[item] = 0
12 self._data[item] += 1
13
14 def most_common(self):
15 items = self._data.items()
16 return sorted(items, key=lambda x: x[1], reverse=True)
17
18
19 def han_to_code(han):
20 return 'U+' + hex(ord(han))[2:].upper()
21
22
23 def main():
24 # { 'han': pinyin_counter }
25 han_counter = {}
26 for hans, pinyin_list in phrases_dict.items():
27 if len(hans) != len(pinyin_list):
28 continue
29 for i, han in enumerate(hans):
30 pinyins = pinyin_list[i]
31 for pinyin in pinyins:
32 if han not in han_counter:
33 han_counter[han] = Counter()
34 counter = han_counter[han]
35 counter.add(pinyin)
36
37 for han, counter in sorted(han_counter.items(), key=lambda x: ord(x[0])):
38 code = han_to_code(han)
39 pinyin = ','.join([x[0] for x in counter.most_common()])
40 if pinyin in ['xx']:
41 continue
42 print('{0}: {1} # {2}'.format(code, pinyin, han))
43
44
45 if __name__ == '__main__':
46 main()