forked from huichen/sego
-
Notifications
You must be signed in to change notification settings - Fork 1
/
segmenter.go
321 lines (279 loc) · 8.45 KB
/
segmenter.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
//Go中文分词
package sego // import "code.sajari.com/sego"
import (
"bufio"
"fmt"
"io"
"math"
"os"
"strconv"
"strings"
"unicode"
"unicode/utf8"
"code.sajari.com/sego/data"
)
// Only read participles greater than or equal to this frequency from the dictionary file
const minTokenFrequency = 2
// 分词器结构体
type Segmenter struct {
dict *Dictionary
}
// 该结构体用于记录Viterbi算法中某字元处的向前分词跳转信息
type jumper struct {
minDistance float32
token *Token
}
// Dictionary returns the dictionary
func (seg *Segmenter) Dictionary() *Dictionary {
return seg.dict
}
// LoadDictionary loads a dictionary from a file
//
// Multiple dictionary files can be loaded, with filenames separated by ",".
// "User Dictionary.txt, Common Dictionary.txt"
// When a participle appears in both the user dictionary and the general dictionary, the user dictionary is used preferentially.
//
// The format of the dictionary is (one line per participle):
// Word segmentation text Frequency Part of speech
func (seg *Segmenter) LoadDictionary(files ...string) error {
seg.dict = NewDictionary()
for _, file := range files {
dictFile, err := os.Open(file)
defer dictFile.Close()
if err != nil {
return fmt.Errorf("could not open %q: %v", file, err)
}
seg.tokenizeDictionary(dictFile)
}
seg.processDictionary()
return nil
}
// LoadDictionaryFromReader loads a dictionary from an io.Reader
//
// The format of the dictionary is (one line per participle):
// Word segmentation text Frequency Part of speech
func (seg *Segmenter) LoadDictionaryFromReader(r io.Reader) {
seg.dict = NewDictionary()
seg.tokenizeDictionary(r)
seg.processDictionary()
}
// LoadDefaultDictionary loads the default dictionary stored in data
func (seg *Segmenter) LoadDefaultDictionary() {
seg.LoadDictionaryFromReader(data.MustDictionary())
}
func (seg *Segmenter) tokenizeDictionary(r io.Reader) {
scanner := bufio.NewScanner(r)
for scanner.Scan() {
line := strings.Split(scanner.Text(), " ")
if len(line) < 2 {
// invalid line
continue
}
text := line[0]
freqText := line[1]
pos := "" // part of speech tag
if len(line) > 2 {
pos = line[2]
}
// Analyze word frequency
frequency, err := strconv.Atoi(freqText)
if err != nil {
continue
}
// Filter words that are too small
if frequency < minTokenFrequency {
continue
}
// Add participles to the dictionary
words := splitTextToWords([]byte(text))
token := Token{text: words, frequency: frequency, pos: pos}
seg.dict.addToken(token)
}
}
func (seg *Segmenter) processDictionary() {
// Calculate the path value of each participle.
// For the meaning of the path value, see the annotation of the Token structure
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)))
}
// Make a careful division of each participle for the search engine pattern.
// For usage of this pattern, see Token structure comments.
for i := range seg.dict.tokens {
token := &seg.dict.tokens[i]
segments := seg.segmentWords(token.text, true)
// Calculate the number of subparticiples that need to be added
numTokensToAdd := 0
for iToken := 0; iToken < len(segments); iToken++ {
if len(segments[iToken].token.text) > 0 {
numTokensToAdd++
}
}
token.segments = make([]*Segment, numTokensToAdd)
// Add child segmentation
iSegmentsToAdd := 0
for iToken := 0; iToken < len(segments); iToken++ {
if len(segments[iToken].token.text) > 0 {
token.segments[iSegmentsToAdd] = &segments[iToken]
iSegmentsToAdd++
}
}
}
}
// DefaultSegmenter creates a new Segmenter with the default dictionary loaded
func DefaultSegmenter() *Segmenter {
var seg Segmenter
seg.LoadDefaultDictionary()
return &seg
}
// 对文本分词
//
// 输入参数:
// bytes UTF8文本的字节数组
//
// 输出:
// []Segment 划分的分词
func (seg *Segmenter) Segment(bytes []byte) []Segment {
return seg.internalSegment(bytes, false)
}
func (seg *Segmenter) InternalSegment(bytes []byte, searchMode bool) []Segment {
return seg.internalSegment(bytes, searchMode)
}
func (seg *Segmenter) internalSegment(bytes []byte, searchMode bool) []Segment {
// 处理特殊情况
if len(bytes) == 0 {
return []Segment{}
}
// 划分字元
text := splitTextToWords(bytes)
return seg.segmentWords(text, searchMode)
}
func (seg *Segmenter) segmentWords(text []Text, searchMode bool) []Segment {
// 搜索模式下该分词已无继续划分可能的情况
if searchMode && len(text) == 1 {
return []Segment{}
}
// 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 := seg.dict.lookupTokens(
text[current:minInt(current+seg.dict.maxTokenLength, len(text))], tokens)
// 对所有可能的分词,更新分词结束字元处的跳转信息
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
}
// 从后向前扫描第二遍添加分词到最终结果
outputSegments := make([]Segment, numSeg)
for index := len(text) - 1; index >= 0; {
location := index - len(jumpers[index].token.text) + 1
numSeg--
outputSegments[numSeg].token = jumpers[index].token
index = location - 1
}
// 计算各个分词的字节位置
bytePosition := 0
for iSeg := 0; iSeg < len(outputSegments); iSeg++ {
outputSegments[iSeg].start = bytePosition
bytePosition += textSliceByteLength(outputSegments[iSeg].token.text)
outputSegments[iSeg].end = bytePosition
}
return outputSegments
}
// 更新跳转信息:
// 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 splitTextToWords(text Text) []Text {
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.IsLetter(r) || 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
}