-
Notifications
You must be signed in to change notification settings - Fork 2
/
jieba.go
executable file
·367 lines (331 loc) · 9.87 KB
/
jieba.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
359
360
361
362
363
364
365
366
367
// Package jieba is the Golang implemention of [Jieba](https://github.com/fxsjy/jieba), Python Chinese text segmentation module.
package jieba
import (
"io"
"math"
"regexp"
"strings"
"github.com/fumiama/jieba/dictionary"
"github.com/fumiama/jieba/finalseg"
"github.com/fumiama/jieba/util"
)
var (
reEng = regexp.MustCompile(`[[:alnum:]]`)
reHanCutAll = regexp.MustCompile(`(\p{Han}+)`)
reSkipCutAll = regexp.MustCompile(`[^[:alnum:]+#\n]`)
reHanDefault = regexp.MustCompile(`([\p{Han}+[:alnum:]+#&\._]+)`)
reSkipDefault = regexp.MustCompile(`(\r\n|\s)`)
)
// Segmenter is a Chinese words segmentation struct.
type Segmenter Dictionary
// Frequency returns a word's frequency and existence
func (seg *Segmenter) Frequency(word string) (float64, bool) {
return (*Dictionary)(seg).Frequency(word)
}
// AddWord adds a new word with frequency to dictionary
func (seg *Segmenter) AddWord(word string, frequency float64) {
(*Dictionary)(seg).AddToken(dictionary.NewToken(word, frequency, ""))
}
// DeleteWord removes a word from dictionary
func (seg *Segmenter) DeleteWord(word string) {
(*Dictionary)(seg).AddToken(dictionary.NewToken(word, 0.0, ""))
}
/*
SuggestFrequency returns a suggested frequncy of a word or a long word
cutted into several short words.
This method is useful when a word in the sentence is not cutted out correctly.
If a word should not be further cutted, for example word "石墨烯" should not be
cutted into "石墨" and "烯", SuggestFrequency("石墨烯") will return the maximu
frequency for this word.
If a word should be further cutted, for example word "今天天气" should be
further cutted into two words "今天" and "天气", SuggestFrequency("今天", "天气")
should return the minimum frequency for word "今天天气".
*/
func (seg *Segmenter) SuggestFrequency(words ...string) float64 {
frequency := 1.0
if len(words) > 1 {
for _, word := range words {
if freq, ok := (*Dictionary)(seg).Frequency(word); ok {
frequency *= freq
}
frequency /= (*Dictionary)(seg).total
}
frequency, _ = math.Modf(frequency * (*Dictionary)(seg).total)
wordFreq := 0.0
if freq, ok := (*Dictionary)(seg).Frequency(strings.Join(words, "")); ok {
wordFreq = freq
}
if wordFreq < frequency {
frequency = wordFreq
}
return frequency
}
word := words[0]
for _, segment := range seg.Cut(word, false) {
if freq, ok := (*Dictionary)(seg).Frequency(segment); ok {
frequency *= freq
}
frequency /= (*Dictionary)(seg).total
}
frequency, _ = math.Modf(frequency * (*Dictionary)(seg).total)
frequency += 1.0
wordFreq := 1.0
if freq, ok := (*Dictionary)(seg).Frequency(word); ok {
wordFreq = freq
}
if wordFreq > frequency {
frequency = wordFreq
}
return frequency
}
// LoadDictionary loads dictionary from given file name. Everytime
// LoadDictionary is called, previously loaded dictionary will be cleard.
func LoadDictionary(file io.Reader) (*Segmenter, error) {
d := &Dictionary{freqMap: make(map[string]float64)}
err := d.loadDictionary(file)
return (*Segmenter)(d), err
}
// LoadDictionaryAt loads dictionary from given file name. Everytime
// LoadDictionaryAt is called, previously loaded dictionary will be cleard.
func LoadDictionaryAt(file string) (*Segmenter, error) {
d := &Dictionary{freqMap: make(map[string]float64)}
err := d.loadDictionaryAt(file)
return (*Segmenter)(d), err
}
// LoadUserDictionary loads a user specified dictionary, it must be called
// after LoadDictionary, and it will not clear any previous loaded dictionary,
// instead it will override exist entries.
func (seg *Segmenter) LoadUserDictionary(file io.Reader) error {
return (*Dictionary)(seg).loadDictionary(file)
}
// LoadUserDictionaryAt loads a user specified dictionary, it must be called
// after LoadDictionary, and it will not clear any previous loaded dictionary,
// instead it will override exist entries.
func (seg *Segmenter) LoadUserDictionaryAt(file string) error {
return (*Dictionary)(seg).loadDictionaryAt(file)
}
func (seg *Segmenter) dag(runes []rune) [][]int {
n := len(runes)
dag := make([][]int, n)
for k := 0; k < n; k++ {
dag[k] = make([]int, 0, 64)
i := k
frag := runes[k : k+1]
for {
freq, ok := (*Dictionary)(seg).Frequency(string(frag))
if !ok {
break
}
if freq > 0.0 {
dag[k] = append(dag[k], i)
}
i++
if i >= n {
break
}
frag = runes[k : i+1]
}
if len(dag[k]) == 0 {
dag[k] = append(dag[k], k)
}
}
return dag
}
type route struct {
frequency float64
index int
}
func (seg *Segmenter) calc(runes []rune) []*route {
dag := seg.dag(runes)
n := len(runes)
rs := make([]*route, n+1)
rs[n] = &route{frequency: 0.0, index: 0}
for idx := n - 1; idx >= 0; idx-- {
for _, i := range dag[idx] {
var r *route
if freq, ok := (*Dictionary)(seg).Frequency(string(runes[idx : i+1])); ok {
r = &route{frequency: math.Log(freq) - (*Dictionary)(seg).logTotal + rs[i+1].frequency, index: i}
} else {
r = &route{frequency: math.Log(1.0) - (*Dictionary)(seg).logTotal + rs[i+1].frequency, index: i}
}
if v := rs[idx]; v == nil {
rs[idx] = r
} else {
if v.frequency < r.frequency || (v.frequency == r.frequency && v.index < r.index) {
rs[idx] = r
}
}
}
}
return rs
}
// ratio words and letters in an article commonly
const (
RatioLetterWord float32 = 1.5
RatioLetterWordFull float32 = 1
)
type cutFunc func(sentence string) []string
func (seg *Segmenter) cutDAG(sentence string) []string {
result := make([]string, 0, int(float32(len(sentence))/RatioLetterWord)+1)
runes := []rune(sentence)
routes := seg.calc(runes)
buf := make([]rune, 0, 256)
for x := 0; x < len(runes); {
y := routes[x].index + 1
frag := runes[x:y]
if y-x == 1 {
buf = append(buf, frag...)
} else {
if len(buf) > 0 {
bufString := string(buf)
if len(buf) == 1 {
result = append(result, bufString)
} else {
if v, ok := (*Dictionary)(seg).Frequency(bufString); !ok || v == 0.0 {
result = append(result, finalseg.Cut(bufString)...)
} else {
for _, elem := range buf {
result = append(result, string(elem))
}
}
}
buf = buf[:0]
}
result = append(result, string(frag))
}
x = y
}
if len(buf) > 0 {
bufString := string(buf)
if len(buf) == 1 {
result = append(result, bufString)
} else {
if v, ok := (*Dictionary)(seg).Frequency(bufString); !ok || v == 0.0 {
result = append(result, finalseg.Cut(bufString)...)
} else {
for _, elem := range buf {
result = append(result, string(elem))
}
}
}
}
return result
}
func (seg *Segmenter) cutDAGNoHMM(sentence string) []string {
result := make([]string, 0, int(float32(len(sentence))/RatioLetterWord)+1)
runes := []rune(sentence)
routes := seg.calc(runes)
buf := make([]rune, 0, 256)
for x := 0; x < len(runes); {
y := routes[x].index + 1
frag := runes[x:y]
if reEng.MatchString(string(frag)) && len(frag) == 1 {
buf = append(buf, frag...)
x = y
continue
}
if len(buf) > 0 {
result = append(result, string(buf))
buf = buf[:0]
}
result = append(result, string(frag))
x = y
}
if len(buf) > 0 {
result = append(result, string(buf))
}
return result
}
// Cut cuts a sentence into words using accurate mode.
// Parameter hmm controls whether to use the Hidden Markov Model.
// Accurate mode attempts to cut the sentence into the most accurate
// segmentations, which is suitable for text analysis.
func (seg *Segmenter) Cut(sentence string, hmm bool) []string {
result := make([]string, 0, int(float32(len(sentence))/RatioLetterWord)+1)
var cut cutFunc
if hmm {
cut = seg.cutDAG
} else {
cut = seg.cutDAGNoHMM
}
for _, block := range util.RegexpSplit(reHanDefault, sentence, -1) {
if len(block) == 0 {
continue
}
if reHanDefault.MatchString(block) {
result = append(result, cut(block)...)
continue
}
for _, subBlock := range util.RegexpSplit(reSkipDefault, block, -1) {
if reSkipDefault.MatchString(subBlock) {
result = append(result, subBlock)
continue
}
for _, r := range subBlock {
result = append(result, string(r))
}
}
}
return result
}
func (seg *Segmenter) cutAll(sentence string) []string {
result := make([]string, 0, int(float32(len(sentence))/RatioLetterWord)+1)
runes := []rune(sentence)
dag := seg.dag(runes)
start := -1
for k := 0; k < len(dag); k++ {
l := dag[k]
if len(l) == 1 && k > start {
result = append(result, string(runes[k:l[0]+1]))
start = l[0]
continue
}
for _, j := range l {
if j > k {
result = append(result, string(runes[k:j+1]))
start = j
}
}
}
return result
}
// CutAll cuts a sentence into words using full mode.
// Full mode gets all the possible words from the sentence.
// Fast but not accurate.
func (seg *Segmenter) CutAll(sentence string) []string {
result := make([]string, 0, int(float32(len(sentence))/RatioLetterWordFull)+1)
for _, block := range util.RegexpSplit(reHanCutAll, sentence, -1) {
if len(block) == 0 {
continue
}
if reHanCutAll.MatchString(block) {
result = append(result, seg.cutAll(block)...)
continue
}
result = append(result, reSkipCutAll.Split(block, -1)...)
}
return result
}
// CutForSearch cuts sentence into words using search engine mode.
// Search engine mode, based on the accurate mode, attempts to cut long words
// into several short words, which can raise the recall rate.
// Suitable for search engines.
func (seg *Segmenter) CutForSearch(sentence string, hmm bool) []string {
result := make([]string, 0, int(float32(len(sentence))/RatioLetterWordFull)+1)
for _, word := range seg.Cut(sentence, hmm) {
runes := []rune(word)
for _, increment := range []int{2, 3} {
if len(runes) <= increment {
continue
}
for i := 0; i < len(runes)-increment+1; i++ {
gram := string(runes[i : i+increment])
if v, ok := (*Dictionary)(seg).Frequency(gram); ok && v > 0.0 {
result = append(result, gram)
}
}
}
result = append(result, word)
}
return result
}