-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.py
58 lines (46 loc) · 1.37 KB
/
gen.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
#!/usr/bin/env python3
import sys
compendium = {
'all': {},
}
opencc_json_template = """
{
"name": "XXX IVD collection",
"segmentation": {
"type": "mmseg",
"dict": {
"type": "text",
"file": "IVD-XXX.txt"
}
},
"conversion_chain": [{
"dict": {
"type": "text",
"file": "IVD-XXX.txt"
}
}]
}
""".strip()
with open(sys.argv[1], 'r') as file:
for line in file:
if line == '#EOF':
break
if line.startswith('#'):
continue
ch_seq, collection_name, id = line.split('; ')
ch_seq = [chr(int(ch, 16)) for ch in ch_seq.split(' ')]
if not collection_name in compendium:
compendium[collection_name] = {}
if not ch_seq[0] in compendium[collection_name]:
compendium[collection_name][ch_seq[0]] = []
if not ch_seq[0] in compendium['all']:
compendium['all'][ch_seq[0]] = []
compendium[collection_name][ch_seq[0]].append(''.join(ch_seq))
if not ''.join(ch_seq) in compendium['all'][ch_seq[0]]:
compendium['all'][ch_seq[0]].append(''.join(ch_seq))
for collection_name in compendium:
with open('IVD-' + collection_name + '.json', 'w') as file:
file.write(opencc_json_template.replace('XXX', collection_name))
with open('IVD-' + collection_name + '.txt', 'w') as file:
for ch_seiji in compendium[collection_name]:
file.write(ch_seiji + '\t' + ' '.join([ch_seiji] + compendium[collection_name][ch_seiji]) + '\n')