-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoudao2kingsoft.py
70 lines (60 loc) · 2.16 KB
/
youdao2kingsoft.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
#-------------------------------------------------------------------------------
# Name: youdao2kingsoft.py
# Purpose: 有道词典生词本(txt格式)转换成金山糍粑的生词本(txt格式) ;-)
#
# Author: qtxie
#
# Created: 27-04-2012
#-------------------------------------------------------------------------------
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import sys
import os
import codecs
import io
def write_utf16(outfile, content):
outfile.write(content.encode('utf-16le'))
def writeline(outfile, line):
outfile.write(line.encode('utf-16le'))
outfile.write("\x0D\x00\x0A\x00") # trick: '\n'.encode('utf-16le') only ouput a '\x0A\x00'
def youdao_to_kingsoft(youdao_dict, kingsoft_dict):
all_words = {}
current_word = None
youdao_file = io.open(youdao_dict, "r", encoding = "utf-16le")
first = True
for line in youdao_file:
if first:
first = False
line = line[1:]
if line[0].isdigit():
words = line.split()
current_word = words[1]
if len(words) > 2:
phonetic = words[2][1:-1]
else:
phonetic = ''
all_words.setdefault(current_word, []).append(phonetic)
else:
all_words.setdefault(current_word, []).append(line[:-1])
kingsoft_file = open(kingsoft_dict, 'wb')
kingsoft_file.write(codecs.BOM_UTF16_LE)
for word, detail in all_words.items():
write_utf16(kingsoft_file, u'+')
writeline(kingsoft_file, word)
detail.reverse()
phonetic = detail.pop()
for line in detail:
write_utf16(kingsoft_file, u'#')
writeline(kingsoft_file, line)
write_utf16(kingsoft_file, u'&')
writeline(kingsoft_file, phonetic)
writeline(kingsoft_file, u'@1335489278')
writeline(kingsoft_file, u'$1')
kingsoft_file.close()
if __name__ == '__main__':
if len(sys.argv) != 3:
print 'Usage: %s youdao_dict_txt kingsoft_dict_txt' % (os.path.basename(sys.argv[0]))
sys.exit(-1)
youdao_dict = sys.argv[1]
kingsoft_dict = sys.argv[2]
youdao_to_kingsoft(youdao_dict, kingsoft_dict)