forked from jasonzzw/sego
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsegmenter.go
More file actions
514 lines (451 loc) · 12.6 KB
/
Copy pathsegmenter.go
File metadata and controls
514 lines (451 loc) · 12.6 KB
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
//Go中文分词
package sego
import (
"bufio"
"fmt"
"log"
"math"
"os"
"strconv"
"strings"
"unicode"
"unicode/utf8"
)
const (
minTokenFrequency = 2 // 仅从字典文件中读取大于等于此频率的分词
)
// 分词器结构体
type Segmenter struct {
dict *Dictionary
Phrase bool
}
// 该结构体用于记录Viterbi算法中某字元处的向前分词跳转信息
type jumper struct {
minDistance float32
token *Token
}
// 返回分词器使用的词典
func (seg *Segmenter) Dictionary() *Dictionary {
return seg.dict
}
// 从文件中载入词典
//
// 可以载入多个词典文件,文件名用","分隔,排在前面的词典优先载入分词,比如
// "用户词典.txt,通用词典.txt"
// 当一个分词既出现在用户词典也出现在通用词典中,则优先使用用户词典。
//
// 词典的格式为(每个分词一行):
// 分词文本 频率 词性
func (seg *Segmenter) LoadDictionary(files string) {
seg.dict = NewDictionary()
for _, file := range strings.Split(files, ",") {
log.Printf("loading sego dictionary %s", file)
dictFile, err := os.Open(file)
defer dictFile.Close()
if err != nil {
log.Fatalf("cannot load sego dictionary \"%s\" \n", file)
}
reader := bufio.NewReader(dictFile)
var text string
var freqText string
var frequency int
var pos string
// 逐行读入分词
for {
line, err := reader.ReadString('\n')
if err != nil {
break
}
size, _ := fmt.Sscanf(line, "%s %s %s\n", &text, &freqText, &pos)
if size == 0 {
// 文件结束
break
} else if size < 2 {
// 无效行
continue
} else if size == 2 {
// 没有词性标注时设为空字符串
pos = ""
}
// 解析词频
frequency, err = strconv.Atoi(freqText)
if err != nil {
continue
}
// 过滤频率太小的词
if frequency < minTokenFrequency {
continue
}
// 将分词添加到字典中
words := splitTextToWords([]byte(text), seg.Phrase)
token := Token{text: words, frequency: frequency, pos: pos}
seg.dict.addToken(token, seg.Phrase)
}
}
// 计算每个分词的路径值,路径值含义见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)))
}
log.Println("sego dictionary loading complete")
}
// For segmenting English Word
func (seg *Segmenter) LoadEnglishDictionary(files string) {
seg.dict = NewDictionary()
for _, file := range strings.Split(files, ",") {
log.Printf("loading sego dictionary %s", file)
dictFile, err := os.Open(file)
defer dictFile.Close()
if err != nil {
log.Fatalf("cannot load sego dictionary \"%s\" \n", file)
}
reader := bufio.NewReader(dictFile)
var text string
var freqText string
var frequency int
var pos string
// 逐行读入分词
for {
line, err := reader.ReadString('\n')
if err != nil {
break
}
size, _ := fmt.Sscanf(line, "%s %s %s\n", &text, &freqText, &pos)
if size == 0 {
// 文件结束
break
} else if size < 2 {
// 无效行
continue
} else if size == 2 {
// 没有词性标注时设为空字符串
pos = ""
}
// 解析词频
frequency, err = strconv.Atoi(freqText)
if err != nil {
continue
}
// 过滤频率太小的词
if frequency < minTokenFrequency {
continue
}
text = strings.ToLower(text)
// 将分词添加到字典中
words := splitEnglishTextToWords([]byte(text), seg.Phrase)
token := Token{text: words, frequency: frequency, pos: pos}
seg.dict.addToken(token, seg.Phrase)
}
}
// 计算每个分词的路径值,路径值含义见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)))
}
log.Println("sego dictionary loading complete")
}
func (seg *Segmenter) LoadPreLoadDictionary(preDict map[string]string) {
seg.dict = NewDictionary()
var text string
var freqText string
var pos string
for cand, v := range preDict {
size, _ := fmt.Sscanf(v, "%s %s %s\n", &text, &freqText, &pos)
if size < 2 {
// 无效行
continue
} else if size == 2 {
// 没有词性标注时设为空字符串
pos = ""
}
// 解析词频
frequency, err := strconv.Atoi(freqText)
if err != nil {
continue
}
// 过滤频率太小的词
if frequency < minTokenFrequency {
continue
}
// 将分词添加到字典中
words := splitTextToWords([]byte(cand), seg.Phrase)
token := Token{text: words, frequency: frequency, pos: pos}
seg.dict.addToken(token, seg.Phrase)
}
// 计算每个分词的路径值,路径值含义见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)))
}
log.Println("sego dictionary loading complete")
}
// 对文本分词
//
// 输入参数:
// bytes UTF8文本的字节数组
//
// 输出:
// []Segment 划分的分词
func (seg *Segmenter) Segment(bytes []byte, joint string) []string {
return seg.internalSegment(bytes, joint, false, "")
}
func (seg *Segmenter) SegmentExclude(bytes []byte, joint string, exclude string) []string {
return seg.internalSegment(bytes, joint, false, exclude)
}
func (seg *Segmenter) SegmentEnglish(bytes []byte, joint string) []string {
return seg.internalEnglishSegment(bytes, joint, false)
}
func (seg *Segmenter) internalEnglishSegment(bytes []byte, joint string, searchMode bool) []string {
// 处理特殊情况
if len(bytes) == 0 {
return []string{}
}
// 划分字元
text := splitEnglishTextToWords(bytes, seg.Phrase)
return seg.segmentWords(text, joint, searchMode, "")
}
func (seg *Segmenter) internalSegment(bytes []byte, joint string, searchMode bool, exclude string) []string {
// 处理特殊情况
if len(bytes) == 0 {
return []string{}
}
// 划分字元
text := splitTextToWords(bytes, seg.Phrase)
return seg.segmentWords(text, joint, searchMode, exclude)
}
func (seg *Segmenter) segmentWords(text []Text, joint string, searchMode bool, exclude string) []string {
// 搜索模式下该分词已无继续划分可能的情况
if searchMode && len(text) == 1 {
return []string{}
}
// jumpers定义了每个字元处的向前跳转信息,包括这个跳转对应的分词,
// 以及从文本段开始到该字元的最短路径值
jumpers := make([]jumper, len(text))
tokens := make([]*Token, seg.dict.maxTokenLength)
for current := 0; current < len(text); current++ {
// 找到前一个字元处的最短路径,以便计算后续路径值
var baseDistance float32
if current == 0 {
// 当本字元在文本首部时,基础距离应该是零
baseDistance = 0
} else {
baseDistance = jumpers[current-1].minDistance
}
// 寻找所有以当前字元开头的分词
numTokens := 0
if exclude == "" {
numTokens = seg.dict.lookupTokens(
text[current:minInt(current+seg.dict.maxTokenLength, len(text))], tokens, seg.Phrase)
} else {
numTokens = seg.dict.lookupTokensExcept(
text[current:minInt(current+seg.dict.maxTokenLength, len(text))], tokens, seg.Phrase, exclude)
}
// 对所有可能的分词,更新分词结束字元处的跳转信息
//fmt.Printf("new_seg: %s, len=%d\n", textSliceToBytes(text), len(text))
for iToken := 0; iToken < numTokens; iToken++ {
location := current + len(tokens[iToken].text) - 1
if !searchMode || current != 0 || location != len(text)-1 {
updateJumper(&jumpers[location], baseDistance, tokens[iToken])
}
}
// 当前字元没有对应分词时补加一个伪分词
if numTokens == 0 || len(tokens[0].text) > 1 {
updateJumper(&jumpers[current], baseDistance,
&Token{text: []Text{text[current]}, frequency: 1, distance: 32, pos: "x"})
}
}
// 从后向前扫描第一遍得到需要添加的分词数目
numSeg := 0
for index := len(text) - 1; index >= 0; {
location := index - len(jumpers[index].token.text) + 1
numSeg++
index = location - 1
}
// 从后向前扫描第二遍添加分词到最终结果
outputStrings := make([]string, numSeg)
for index := len(text) - 1; index >= 0; {
location := index - len(jumpers[index].token.text) + 1
numSeg--
if joint == "" {
outputStrings[numSeg] = jumpers[index].token.Text()
} else {
outputStrings[numSeg] = jumpers[index].token.TextOfPhrase(joint)
}
index = location - 1
}
return outputStrings
}
// 更新跳转信息:
// 1. 当该位置从未被访问过时(jumper.minDistance为零的情况),或者
// 2. 当该位置的当前最短路径大于新的最短路径时
// 将当前位置的最短路径值更新为baseDistance加上新分词的概率
func updateJumper(jumper *jumper, baseDistance float32, token *Token) {
newDistance := baseDistance + token.distance
if jumper.minDistance == 0 || jumper.minDistance > newDistance {
jumper.minDistance = newDistance
jumper.token = token
}
}
// 取两整数较小值
func minInt(a, b int) int {
if a > b {
return b
}
return a
}
// 取两整数较大值
func maxInt(a, b int) int {
if a > b {
return a
}
return b
}
func notSeparable(prev *rune, cur rune, text Text, pos int) bool {
if prev == nil {
return false
}
if pos >= len(text) {
return false
}
r, _ := utf8.DecodeRune(text[pos:])
if ((cur == '.' || cur == '/') && unicode.IsNumber(*prev) && unicode.IsNumber(r)) ||
(cur == '\'' && unicode.IsLetter(*prev) && unicode.IsLetter(r)) {
return true
}
return false
}
// 将文本划分成字元
func splitTextToWords(text Text, phrase bool) []Text {
if phrase {
//if phrase
output := []Text{}
rs := []rune(string(text))
start := 0
inWord := false
for i, r := range rs {
if r == '-' {
if inWord {
output = append(output, []byte(string(rs[start:i])))
inWord = false
}
//skip
} else {
if !inWord {
start = i
inWord = true
}
//already in word, do nosplitEnglishTextToWordsthing
}
}
if inWord {
output = append(output, []byte(string(rs[start:len(rs)])))
}
return output
}
output := make([]Text, 0, len(text)/3)
current := 0
inAlphanumeric := true
alphanumericStart := 0
var prev *rune
for current < len(text) {
r, size := utf8.DecodeRune(text[current:])
if size <= 2 && (unicode.IsLetter(r) || unicode.IsNumber(r) ||
notSeparable(prev, r, text, current+size)) {
// 当前是拉丁字母或数字(非中日韩文字)
if !inAlphanumeric {
alphanumericStart = current
inAlphanumeric = true
}
} else {
if inAlphanumeric {
inAlphanumeric = false
if current != 0 {
output = append(output, toLower(text[alphanumericStart:current]))
}
}
output = append(output, text[current:current+size])
}
current += size
prev = &r
}
// 处理最后一个字元是英文的情况
if inAlphanumeric {
if current != 0 {
output = append(output, toLower(text[alphanumericStart:current]))
}
}
return output
}
func splitEnglishTextToWords(text Text, phrase bool) []Text {
if phrase {
//if phrase
output := []Text{}
rs := []rune(string(text))
start := 0
inWord := false
for i, r := range rs {
if r == '-' {
if inWord {
output = append(output, []byte(string(rs[start:i])))
inWord = false
}
//skip
} else {
if !inWord {
start = i
inWord = true
}
//already in word, do nothing
}
}
if inWord {
output = append(output, []byte(string(rs[start:len(rs)])))
}
return output
}
output := make([]Text, 0, len(text)/3)
current := 0
inAlphanumeric := true
alphanumericStart := 0
for current < len(text) {
r, size := utf8.DecodeRune(text[current:])
if size <= 2 && (unicode.IsNumber(r)) {
// 当前是拉丁字母或数字(非中日韩文字)
if !inAlphanumeric {
alphanumericStart = current
inAlphanumeric = true
}
} else {
if inAlphanumeric {
inAlphanumeric = false
if current != 0 {
output = append(output, toLower(text[alphanumericStart:current]))
}
}
output = append(output, text[current:current+size])
}
current += size
}
// 处理最后一个字元是英文的情况
if inAlphanumeric {
if current != 0 {
output = append(output, toLower(text[alphanumericStart:current]))
}
}
return output
}
// 将英文词转化为小写
func toLower(text []byte) []byte {
output := make([]byte, len(text))
for i, t := range text {
if t >= 'A' && t <= 'Z' {
output[i] = t - 'A' + 'a'
} else {
output[i] = t
}
}
return output
}