Tizen 2.1 base
[platform/core/uifw/ise-engine-sunpinyin.git] / python / importer / import_sogou_celldict.py
1 #!/usr/bin/python
2
3 # thanks for the reverse engineering efforts of following projects/peoples:
4 # http://code.google.com/p/imewlconverter
5 # http://code.google.com/p/ibus-cloud-pinyin
6 # http://forum.ubuntu.org.cn/viewtopic.php?f=8&t=250136&start=0
7
8 from importer import import_to_sunpinyin_user_dict
9 import struct
10 import os, sys
11
12 def read_utf16_str (f, offset=-1, len=2):
13     if offset >= 0:
14         f.seek(offset)
15     str = f.read(len)
16     return str.decode('UTF-16LE')
17
18 def read_uint16 (f):
19     return struct.unpack ('<H', f.read(2))[0]
20
21 def get_word_from_sogou_cell_dict (fname):
22     f = open (fname, 'rb')
23     file_size = os.path.getsize (fname)
24     
25     hz_offset = 0
26     mask = struct.unpack ('B', f.read(128)[4])[0]
27     if mask == 0x44:
28         hz_offset = 0x2628
29     elif mask == 0x45:
30         hz_offset = 0x26c4
31     else:
32         sys.exit(1)
33     
34     title   = read_utf16_str (f, 0x130, 0x338  - 0x130)
35     type    = read_utf16_str (f, 0x338, 0x540  - 0x338)
36     desc    = read_utf16_str (f, 0x540, 0xd40  - 0x540)
37     samples = read_utf16_str (f, 0xd40, 0x1540 - 0xd40)
38     
39     py_map = {}
40     f.seek(0x1540+4)
41     
42     while 1:
43         py_code = read_uint16 (f)
44         py_len  = read_uint16 (f)
45         py_str  = read_utf16_str (f, -1, py_len)
46     
47         if py_code not in py_map:
48             py_map[py_code] = py_str
49     
50         if py_str == 'zuo':
51             break
52     
53     f.seek(hz_offset)
54     while f.tell() != file_size:
55         word_count   = read_uint16 (f)
56         pinyin_count = read_uint16 (f) / 2
57     
58         py_set = []
59         for i in range(pinyin_count):
60             py_id = read_uint16(f)
61             py_set.append(py_map[py_id])
62         py_str = "'".join (py_set)
63     
64         for i in range(word_count):
65             word_len = read_uint16(f)
66             word_str = read_utf16_str (f, -1, word_len)
67             f.read(12)  # simply ignore word frequence info
68             yield py_str, word_str
69
70     f.close()
71
72 def main ():
73     if len (sys.argv) != 2:
74         print "Please specify the Sogou PinYin Cell dict file!"
75         exit (1)
76
77     generator = get_word_from_sogou_cell_dict (sys.argv[1])
78     import_to_sunpinyin_user_dict (generator)
79
80 if __name__ == "__main__":
81     main()