-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathseg.go
64 lines (56 loc) · 1.12 KB
/
seg.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
package jiagu
import (
"compress/gzip"
"fmt"
"io"
"github.com/bububa/jiagu/segment"
)
var seg *segment.Segment
// Segment get seg singleton
func Segment() *segment.Segment {
if seg == nil {
vocabR, err := dictFS.Open(fmt.Sprintf("dict/%s", VOCAB_DICT))
if err != nil {
panic(err)
}
defer vocabR.Close()
modelR, err := modelFS.Open(fmt.Sprintf("model/%s", CWS_MODEL))
if err != nil {
panic(err)
}
defer modelR.Close()
gw, err := gzip.NewReader(modelR)
if err != nil {
panic(err)
}
seg, err = segment.NewFromReader(vocabR, gw)
if err != nil {
panic(err)
}
}
return seg
}
// Seg 分词
func Seg(sentence string) []string {
model := Segment()
return model.Seg(sentence, segment.Default_SegMode)
}
// LoadUserDict 加载用户词典
func LoadUserDict(r io.Reader) {
model := Segment()
model.LoadUserDict(r)
}
// AddVocabs 添加用户词典word
func AddVocabs(words []string) {
model := Segment()
for _, w := range words {
model.AddVocab(w, 1)
}
}
// DelVocabs 删除用户词典word
func DelVocabs(words []string) {
model := Segment()
for _, w := range words {
model.DelVocab(w, 1)
}
}