forked from caiguanhao/sogoudict
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sogoudict.go
307 lines (257 loc) · 5.65 KB
/
sogoudict.go
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// Read content of a Sogou Pinyin Dictionary File (.scel).
package sogoudict
import (
"bytes"
"encoding/binary"
"errors"
"io"
"os"
"sort"
"unicode/utf16"
"unicode/utf8"
)
var (
// not a .scel file at all
ErrInvalidDict = errors.New("not a valid sogou dict")
// .scel file might be corrupted
ErrCorruptedDict = errors.New("dict file might be corrupted")
)
var (
_SogouDictID = []byte{64, 21, 0, 0, 68, 67, 83, 1, 1, 0, 0, 0}
_PinyinTableID = []byte{157, 1, 0, 0}
_Uint16 = binary.LittleEndian.Uint16
)
const (
_NameOffset, _NameSize = 304, 520
_CategoryOffset, _CategorySize = 824, 520
_DescriptionOffset, _DescriptionSize = 1344, 2048
_ExamplesOffset, _ExamplesSize = 3392, 2048
_SogouDictIDOffset, _SogouDictIDSize = 0, 12
_PinYinTableIDOffset, _PinYinTableIDSize = 5440, 4
_PinYinTableOffset, _PinYinTableSize = 5444, 4324
_ItemsOffset = 9768
)
type SogouDictItem struct {
weight int
Abbr []string `json:"abbr"` // abbreviated pinyin of each Chinese character
Pinyin []string `json:"pinyin"` // pinyin of each Chinese character
Text string `json:"text"` // the Chinese word
}
type SogouDict struct {
// basic info
Name string `json:"name"`
Category string `json:"category"`
Description string `json:"description"`
Examples string `json:"examples"`
// content of the .scel file, sorted by the frequency of word usage
Items []SogouDictItem `json:"items"`
}
type byWeight []SogouDictItem
func (a byWeight) Len() int { return len(a) }
func (a byWeight) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byWeight) Less(i, j int) bool { return a[i].weight < a[j].weight }
func convertUTF16ToUTF8(in []byte) []byte {
ret := &bytes.Buffer{}
buf := make([]byte, 4)
for i := 0; i < len(in); i += 2 {
r := utf16.Decode([]uint16{uint16(in[i]) + (uint16(in[i+1]) << 8)})
n := utf8.EncodeRune(buf, r[0])
ret.Write(buf[:n])
}
return ret.Bytes()
}
func convert(in []byte) (out string) {
out = string(bytes.Trim(convertUTF16ToUTF8(in), "\x00"))
return
}
func check(rs io.ReadSeeker, offset, size int64, expected []byte) bool {
var err error
actual := make([]byte, size)
_, err = rs.Seek(offset, 0)
if err != nil {
return false
}
_, err = rs.Read(actual)
if err != nil {
return false
}
if !bytes.Equal(actual, expected) {
return false
}
return true
}
func getInfo(rs io.ReadSeeker, offset, size int64) (out string, err error) {
info := make([]byte, size)
_, err = rs.Seek(offset, 0)
if err != nil {
return
}
_, err = rs.Read(info)
if err != nil {
return
}
out = convert(info)
return
}
func getItems(rs io.ReadSeeker) (items []SogouDictItem, err error) {
if !check(rs, _PinYinTableIDOffset, _PinYinTableIDSize, _PinyinTableID) {
err = ErrCorruptedDict
return
}
integer := make([]byte, 2)
table := make(map[uint16]string)
for {
var pos int64
pos, err = rs.Seek(0, 1)
if err != nil {
return
}
if pos >= _ItemsOffset {
break
}
_, err = rs.Read(integer)
if err != nil {
return
}
index := _Uint16(integer)
_, err = rs.Read(integer)
if err != nil {
return
}
var py []byte
pyLen := int(_Uint16(integer))
for pyLen > 0 {
_, err = rs.Read(integer)
if err != nil {
return
}
for _, b := range integer {
if b == 0 {
continue
}
py = append(py, b)
}
pyLen -= len(integer)
}
table[index] = string(py)
}
_, err = rs.Seek(_ItemsOffset, 0)
if err != nil {
return
}
for {
_, err = rs.Read(integer)
if err != nil {
if err == io.EOF {
err = nil
break
}
return
}
count := int(_Uint16(integer))
_, err = rs.Read(integer)
if err != nil {
return
}
pinyinLen := int(_Uint16(integer))
var pinyin []string
var abbr []string
for pinyinLen > 0 {
_, err = rs.Read(integer)
if err != nil {
return
}
py := table[_Uint16(integer)]
if len(py) == 0 {
//err = ErrCorruptedDict
//return
continue
}
pinyin = append(pinyin, py)
abbr = append(abbr, py[0:1])
pinyinLen -= len(integer)
}
for i := 0; i < count; i++ {
_, err = rs.Read(integer)
if err != nil {
if err == io.EOF {
err = nil
break
}
return
}
word := make([]byte, _Uint16(integer))
_, err = rs.Read(word)
if err != nil {
if err == io.EOF {
err = nil
break
}
return
}
_, err = rs.Read(integer)
if err != nil {
if err == io.EOF {
err = nil
break
}
return
}
weight := make([]byte, _Uint16(integer))
_, err = rs.Read(weight)
if err != nil {
if err == io.EOF {
err = nil
break
}
return
}
items = append(items, SogouDictItem{
weight: int(_Uint16(weight)),
Abbr: abbr,
Pinyin: pinyin,
Text: convert(word),
})
}
}
sort.Sort(byWeight(items))
return
}
// Read content from a Reader.
func Parse(rs io.ReadSeeker) (dict SogouDict, err error) {
if !check(rs, _SogouDictIDOffset, _SogouDictIDSize, _SogouDictID) {
err = ErrInvalidDict
return
}
dict.Name, err = getInfo(rs, _NameOffset, _NameSize)
if err != nil {
return
}
dict.Category, err = getInfo(rs, _CategoryOffset, _CategorySize)
if err != nil {
return
}
dict.Description, err = getInfo(rs, _DescriptionOffset, _DescriptionSize)
if err != nil {
return
}
dict.Examples, err = getInfo(rs, _ExamplesOffset, _ExamplesSize)
if err != nil {
return
}
dict.Items, err = getItems(rs)
return
}
// Read content from a file.
func ParseFile(file string) (dict SogouDict, err error) {
var f *os.File
f, err = os.Open(file)
if err != nil {
return
}
defer f.Close()
var rs io.ReadSeeker
rs = f
dict, err = Parse(rs)
return
}