forked from go-ego/gse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdict_util.go
358 lines (312 loc) · 8.33 KB
/
dict_util.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// Copyright 2016 ego authors
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package gse
import (
"bufio"
"encoding/json"
"fmt"
"io"
"log"
"math"
"os"
"path"
"runtime"
"strconv"
"strings"
"unicode"
)
var (
// LoadNoFreq load not have freq dict word
LoadNoFreq bool
// MinTokenFreq load min freq token
MinTokenFreq = 2
)
// Dictionary 返回分词器使用的词典
func (seg *Segmenter) Dictionary() *Dictionary {
return seg.dict
}
// AddToken add new text to token
func (seg *Segmenter) AddToken(text string, frequency int, pos ...string) {
var po string
if len(pos) > 0 {
po = pos[0]
}
words := splitTextToWords([]byte(text))
token := Token{text: words, frequency: frequency, pos: po}
seg.dict.addToken(token)
}
// AddTokenForce add new text to token and force
func (seg *Segmenter) AddTokenForce(text string, frequency int, pos ...string) {
seg.AddToken(text, frequency, pos...)
seg.CalcToken()
}
// LoadDict load the dictionary from the file
//
// The format of the dictionary is (one for each participle):
// participle text, frequency, part of speech
//
// Can load multiple dictionary files, the file name separated by ","
// the front of the dictionary preferentially load the participle,
// such as: "user_dictionary.txt,common_dictionary.txt"
// When a participle appears both in the user dictionary and
// in the `common dictionary`, the `user dictionary` is given priority.
//
// 从文件中载入词典
//
// 可以载入多个词典文件,文件名用 "," 分隔,排在前面的词典优先载入分词,比如:
// "用户词典.txt,通用词典.txt"
// 当一个分词既出现在用户词典也出现在 `通用词典` 中,则优先使用 `用户词典`。
//
// 词典的格式为(每个分词一行):
// 分词文本 频率 词性
func (seg *Segmenter) LoadDict(files ...string) error {
seg.dict = NewDict()
var (
dictDir = path.Join(path.Dir(getCurrentFilePath()), "data")
dictPath string
// load bool
)
if len(files) > 0 {
dictFiles := DictPaths(dictDir, files[0])
if len(dictFiles) > 0 {
// load = true
// files = dictFiles
for i := 0; i < len(dictFiles); i++ {
err := seg.Read(dictFiles[i])
if err != nil {
return err
}
}
}
}
if len(files) == 0 {
dictPath = path.Join(dictDir, "dict/dictionary.txt")
// files = []string{dictPath}
err := seg.Read(dictPath)
if err != nil {
return err
}
}
// if files[0] != "" && files[0] != "en" && !load {
// for _, file := range strings.Split(files[0], ",") {
// // for _, file := range files {
// err := seg.Read(file)
// if err != nil {
// return err
// }
// }
// }
seg.CalcToken()
log.Println("Gse dictionary loaded finished.")
return nil
}
// getCurrentFilePath get current file path
func getCurrentFilePath() string {
_, filePath, _, _ := runtime.Caller(1)
return filePath
}
// Read read the dict flie
func (seg *Segmenter) Read(file string) error {
log.Printf("Load the gse dictionary: \"%s\" ", file)
isJson := false
if strings.Contains(file, ".json") {
isJson = true
}
dictFile, err := os.Open(file)
if err != nil {
log.Printf("Could not load dictionaries: \"%s\", %v \n", file, err)
return err
}
defer dictFile.Close()
reader := bufio.NewReader(dictFile)
var (
text string
freqText string
frequency int
pos string
)
// 逐行读入分词
line := 0
size := 0
tokenJson := &TokenJson{}
for {
line++
data, err := reader.ReadBytes('\n')
if err != nil {
if err == io.EOF {
break
} else {
log.Printf("File '%v' line \"%v\" read error: %v, skip",
file, line, err.Error())
continue
}
}
size = len(data)
if isJson {
if err := json.Unmarshal(data, tokenJson); err != nil {
log.Printf("File '%v' line \"%v\" unmarshal error: %v, skip",
file, line, err.Error())
continue
}
text, freqText, pos = tokenJson.Text, tokenJson.Frequency, tokenJson.Pos
} else {
fsize, fsErr := fmt.Sscanln(string(data), &text, &freqText, &pos)
//fsize, fsErr := fmt.Fscanln(reader, &text, &freqText, &pos)
if fsErr != nil {
if fsize > 0 {
log.Printf("File '%v' line \"%v\" read error: %v, skip",
file, line, fsErr.Error())
} else {
log.Printf("File '%v' line \"%v\" is empty, read error: %v, skip",
file, line, fsErr.Error())
}
}
size = fsize
}
if size == 0 {
// 文件结束或错误行
// break
continue
} else if size < 2 {
if !LoadNoFreq {
// 无效行
continue
} else {
freqText = "2"
}
} else if size == 2 {
// 没有词性标注时设为空字符串
pos = ""
}
// 解析词频
frequency, err = strconv.Atoi(freqText)
if err != nil {
continue
}
// 过滤频率太小的词
if frequency < MinTokenFreq {
continue
}
// 过滤, 降低词频
if len([]rune(text)) < 2 {
// continue
frequency = 2
}
// 将分词添加到字典中
words := splitTextToWords([]byte(text))
token := Token{text: words, frequency: frequency, pos: pos}
seg.dict.addToken(token)
}
return nil
}
// DictPaths get the dict's paths
func DictPaths(dictDir, filePath string) (files []string) {
var dictPath string
if filePath == "en" {
return
}
if filePath == "zh" {
dictPath = path.Join(dictDir, "dict/dictionary.txt")
files = []string{dictPath}
return
}
if filePath == "jp" {
dictPath = path.Join(dictDir, "dict/jp/dict.txt")
files = []string{dictPath}
return
}
// if strings.Contains(filePath, ",") {
fileName := strings.Split(filePath, ",")
for i := 0; i < len(fileName); i++ {
if fileName[i] == "jp" {
dictPath = path.Join(dictDir, "dict/jp/dict.txt")
}
if fileName[i] == "zh" {
dictPath = path.Join(dictDir, "dict/dictionary.txt")
}
// if str[i] == "ti" {
// }
dictName := fileName[i] != "en" && fileName[i] != "zh" &&
fileName[i] != "jp" && fileName[i] != "ti"
if dictName {
dictPath = fileName[i]
}
if dictPath != "" {
files = append(files, dictPath)
}
}
// }
log.Println("Dict files path: ", files)
return
}
// IsJp is jp char return true
func IsJp(segText string) bool {
for _, r := range segText {
jp := unicode.Is(unicode.Scripts["Hiragana"], r) ||
unicode.Is(unicode.Scripts["Katakana"], r)
if jp {
return true
}
}
return false
}
// CalcToken calc the segmenter token
func (seg *Segmenter) CalcToken() {
// 计算每个分词的路径值,路径值含义见 Token 结构体的注释
logTotalFrequency := float32(math.Log2(float64(seg.dict.totalFrequency)))
for i := range seg.dict.tokens {
token := &seg.dict.tokens[i]
token.distance = logTotalFrequency - float32(math.Log2(float64(token.frequency)))
}
// 对每个分词进行细致划分,用于搜索引擎模式,
// 该模式用法见 Token 结构体的注释。
for i := range seg.dict.tokens {
token := &seg.dict.tokens[i]
segments := seg.segmentWords(token.text, true)
// 计算需要添加的子分词数目
numTokensToAdd := 0
for iToken := 0; iToken < len(segments); iToken++ {
// if len(segments[iToken].token.text) > 1 {
// 略去字元长度为一的分词
// TODO: 这值得进一步推敲,特别是当字典中有英文复合词的时候
if len(segments[iToken].token.text) > 0 {
hasJp := false
if len(segments[iToken].token.text) == 1 {
segText := string(segments[iToken].token.text[0])
hasJp = IsJp(segText)
}
if !hasJp {
numTokensToAdd++
}
}
}
token.segments = make([]*Segment, numTokensToAdd)
// 添加子分词
iSegmentsToAdd := 0
for iToken := 0; iToken < len(segments); iToken++ {
// if len(segments[iToken].token.text) > 1 {
if len(segments[iToken].token.text) > 0 {
hasJp := false
if len(segments[iToken].token.text) == 1 {
segText := string(segments[iToken].token.text[0])
hasJp = IsJp(segText)
}
if !hasJp {
token.segments[iSegmentsToAdd] = &segments[iToken]
iSegmentsToAdd++
}
}
}
}
}