forked from go-ego/gse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgse.go
103 lines (85 loc) · 2.78 KB
/
gse.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
// 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 Go efficient text segmentation, Go 语言高性能分词
*/
package gse
import (
"github.com/go-ego/gse/hmm"
)
const (
version = "v0.40.1.260, Snake River!"
// minTokenFrequency = 2 // 仅从字典文件中读取大于等于此频率的分词
)
func init() {
hmm.LoadModel()
}
// GetVersion get the gse version
func GetVersion() string {
return version
}
// Prob type hmm model struct
type Prob struct {
B, E, M, S map[rune]float64
}
// Cut cuts a str into words using accurate mode.
// Parameter hmm controls whether to use the HMM(Hidden Markov Model)
// or use the user's model.
func (seg *Segmenter) Cut(str string, hmm ...bool) []string {
if len(hmm) <= 0 || (len(hmm) > 0 && hmm[0] == false) {
return seg.Slice([]byte(str))
// return seg.cutDAGNoHMM(str)
}
return seg.cutDAG(str)
}
// CutSearch cuts str into words using search engine mode.
func (seg *Segmenter) CutSearch(str string, hmm ...bool) []string {
if len(hmm) <= 0 || (len(hmm) > 0 && hmm[0] == false) {
return seg.Slice([]byte(str), true)
}
return seg.cutForSearch(str, hmm...)
}
// CutAll cuts a str into words using full mode.
func (seg *Segmenter) CutAll(str string) []string {
return seg.cutAll(str)
}
// Slice use modeSegment segment retrun []string
// using search mode if searchMode is true
func (seg *Segmenter) Slice(bytes []byte, searchMode ...bool) []string {
segs := seg.ModeSegment(bytes, searchMode...)
return ToSlice(segs, searchMode...)
}
// Slice use modeSegment segment retrun string
// using search mode if searchMode is true
func (seg *Segmenter) String(bytes []byte, searchMode ...bool) string {
segs := seg.ModeSegment(bytes, searchMode...)
return ToString(segs, searchMode...)
}
// LoadModel load the hmm model
//
// Use the user's model:
// seg.LoadModel(B, E, M, S map[rune]float64)
func (seg *Segmenter) LoadModel(prob ...map[rune]float64) {
hmm.LoadModel(prob...)
}
// HMMCut cut sentence string use HMM with Viterbi
func (seg *Segmenter) HMMCut(str string) []string {
// hmm.LoadModel(prob...)
return hmm.Cut(str)
}
// HMMCutMod cut sentence string use HMM with Viterbi
func (seg *Segmenter) HMMCutMod(str string, prob ...map[rune]float64) []string {
hmm.LoadModel(prob...)
return hmm.Cut(str)
}