~ubuntu-branches/ubuntu/trusty/sunpinyin/trusty-proposed

« back to all changes in this revision

Viewing changes to python/importer/importer.py

  • Committer: Bazaar Package Importer
  • Author(s): Zhengpeng Hou
  • Date: 2010-09-06 12:23:46 UTC
  • Revision ID: james.westby@ubuntu.com-20100906122346-yamofztk2j5p85fs
Tags: upstream-2.0.2
ImportĀ upstreamĀ versionĀ 2.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
import os, sys
 
3
import struct
 
4
import sqlite3 as sqlite
 
5
from pinyin_data import valid_syllables, decode_syllable, initials, finals
 
6
 
 
7
def get_userdict_path ():
 
8
    homedir = os.environ.get("HOME")
 
9
 
 
10
    if sys.platform == "darwin":
 
11
        return homedir+"/Library/Application Support/SunPinyin/userdict"
 
12
 
 
13
    # FIXME: not sure how to get the ibus version or wrapper type (xim or ibus)
 
14
    if os.path.exists (homedir+"/.cache/ibus/sunpinyin"):
 
15
        return homedir+"/.cache/ibus/sunpinyin/userdict"
 
16
        
 
17
    if os.path.exists (homedir+"/.ibus/sunpinyin"):
 
18
        return homedir+"/.ibus/sunpinyin/userdict"
 
19
    
 
20
    if os.path.exists (homedir+"/.sunpinyin"):
 
21
        return homedir+"/.sunpinyin/userdict"
 
22
 
 
23
    raise "Can not detect sunpinyin's userdict!"
 
24
 
 
25
def get_sysdict_path ():
 
26
    if sys.platform == "darwin":
 
27
        homedir = os.environ.get("HOME")
 
28
        sysdict_path = "/Library/Input Methods/SunPinyin.app/Contents/Resources/pydict_sc.bin"
 
29
        if os.path.exists (homedir + sysdict_path):
 
30
            return homedir + sysdict_path
 
31
        else:
 
32
            return sysdict_path
 
33
 
 
34
    return "/usr/lib/sunpinyin/data/pydict_sc.bin"
 
35
 
 
36
def load_system_dict ():
 
37
    sysdict_path = get_sysdict_path ()
 
38
    f = open (sysdict_path, "rb")
 
39
    
 
40
    f.seek(8)
 
41
    word_offset = struct.unpack ('I', f.read(4))[0]
 
42
    f.seek (word_offset)
 
43
 
 
44
    words = set()
 
45
    str = f.read()
 
46
    
 
47
    for w in str.decode('UTF-32').split('\0'):
 
48
        if w:
 
49
            words.add (w)
 
50
    
 
51
    f.close()
 
52
    return words
 
53
 
 
54
def import_to_sunpinyin_user_dict (records, userdict_path=''):
 
55
    userdict_path = userdict_path if userdict_path else get_userdict_path()
 
56
    db = sqlite.connect (userdict_path)
 
57
 
 
58
    sysdict = load_system_dict()
 
59
 
 
60
    sqlstring = """
 
61
            CREATE TABLE IF NOT EXISTS dict(
 
62
            id INTEGER PRIMARY KEY, len INTEGER,
 
63
            i0 INTEGER, i1 INTEGER, i2 INTEGER, i3 INTEGER, i4 INTEGER, i5 INTEGER,
 
64
            f0 INTEGER, f1 INTEGER, f2 INTEGER, f3 INTEGER, f4 INTEGER, f5 INTEGER,
 
65
            utf8str TEXT, UNIQUE (utf8str));
 
66
            """
 
67
    db.executescript (sqlstring)
 
68
 
 
69
    batch_count = 0
 
70
 
 
71
    for (pystr, utf8str) in records:
 
72
        try:
 
73
            syllables = [valid_syllables[s] for s in pystr.split("'")]
 
74
        except:
 
75
            print "[%s] has un-recognized syllables, ignoring this record!" % pystr
 
76
            continue
 
77
 
 
78
        if len (syllables) < 2 or len (syllables) > 6:
 
79
            print "[%s] is too long or too short for sunpinyin userdict" % utf8str
 
80
            continue
 
81
 
 
82
        if utf8str in sysdict:
 
83
            #print "[%s] is already in sunpinyin's sysdict" % utf8str
 
84
            continue
 
85
 
 
86
        record = [0]*14
 
87
        record[0] = len (syllables)
 
88
        record[13] = utf8str
 
89
 
 
90
        c = 1
 
91
        for s in syllables:
 
92
            i, f = s>>12, (s&0x00ff0)>>4
 
93
            if i and not f:
 
94
                break; 
 
95
            record[c] = i
 
96
            record[c+1] = f
 
97
            c += 2
 
98
        else:
 
99
            sqlstring = """
 
100
                    INSERT INTO dict (len, i0, f0, i1, f1, i2, f2, i3, f3, i4, f4, i5, f5, utf8str)
 
101
                    VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
 
102
                    """
 
103
            try:
 
104
                db.execute (sqlstring, record)
 
105
                #print "[%s] is imported into sunpinyin's userdict" % utf8str
 
106
 
 
107
                batch_count += 1
 
108
                if batch_count == 100:
 
109
                    db.commit ()
 
110
                    batch_count = 0
 
111
 
 
112
            except:
 
113
                #print "[%s] is already in sunpinyin's userdict" % utf8str
 
114
                pass
 
115
 
 
116
    db.commit()
 
117
    db.close()
 
118
 
 
119
def export_sunpinyin_user_dict (userdict_path=''):
 
120
    userdict_path = userdict_path if userdict_path else get_userdict_path()
 
121
    db = sqlite.connect (userdict_path)
 
122
 
 
123
    sqlstring = "SELECT * FROM dict"
 
124
    result = list (db.execute (sqlstring).fetchall ())
 
125
 
 
126
    for record in result:
 
127
        id   = record[0]
 
128
        l    = record[1]
 
129
        i    = record[2:8]
 
130
        f    = record[8:14]
 
131
        str  = record[-1]
 
132
        syls = [initials[i[x]] + finals[f[x]] for x in range(l)]
 
133
        print str.encode ('UTF-8'), id, "'".join(syls) 
 
134
        
 
135
if __name__ == "__main__":
 
136
    export_sunpinyin_user_dict ()