parent
4630aa1949
commit
6a115bada2
@ -0,0 +1,37 @@
|
||||
package gse
|
||||
|
||||
import (
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/go-ego/gse"
|
||||
"github.com/go-ego/gse/hmm/pos"
|
||||
)
|
||||
|
||||
var (
|
||||
seg gse.Segmenter
|
||||
posSeg pos.Segmenter
|
||||
)
|
||||
|
||||
func init() {
|
||||
err := seg.LoadDictEmbed()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
err = seg.LoadStopEmbed()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func SegWords(text string) (words []string) {
|
||||
wds := seg.Cut(text)
|
||||
for _, wd := range wds {
|
||||
// 跳过单字、空格、标点、数字
|
||||
if utf8.RuneCountInString(wd) == 1 || unicode.IsSpace(rune(wd[0])) || unicode.IsPunct(rune(wd[0])) || unicode.IsDigit(rune(wd[0])) {
|
||||
continue
|
||||
}
|
||||
words = append(words, wd)
|
||||
}
|
||||
return
|
||||
}
|
@ -1,56 +0,0 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"unicode"
|
||||
|
||||
"github.com/yanyiwu/gojieba"
|
||||
)
|
||||
|
||||
var jieba *gojieba.Jieba
|
||||
|
||||
type Jieba struct {
|
||||
jieba *gojieba.Jieba
|
||||
}
|
||||
|
||||
func NewJieba(dictDir ...string) *Jieba {
|
||||
defaultDir := "dict"
|
||||
if len(dictDir) > 0 {
|
||||
defaultDir = dictDir[0]
|
||||
}
|
||||
|
||||
dicts := []string{
|
||||
defaultDir + "/jieba.dict.utf8",
|
||||
defaultDir + "/hmm_model.utf8",
|
||||
defaultDir + "/user.dict.utf8",
|
||||
defaultDir + "/idf.utf8",
|
||||
defaultDir + "/stop_words.utf8",
|
||||
}
|
||||
if jieba == nil {
|
||||
jieba = gojieba.NewJieba(dicts...)
|
||||
}
|
||||
return &Jieba{
|
||||
jieba: jieba,
|
||||
}
|
||||
}
|
||||
|
||||
func (j *Jieba) AddWord(words ...string) {
|
||||
for _, word := range words {
|
||||
j.jieba.AddWord(word)
|
||||
}
|
||||
}
|
||||
|
||||
func (j *Jieba) SegWords(text string, length ...int) (words []string) {
|
||||
topk := 10
|
||||
if len(length) > 0 {
|
||||
topk = length[0]
|
||||
}
|
||||
wds := j.jieba.Extract(text, topk)
|
||||
for _, wd := range wds {
|
||||
// 不是标点且不是空格也不是数字
|
||||
if unicode.IsSpace(rune(wd[0])) || unicode.IsPunct(rune(wd[0])) || unicode.IsDigit(rune(wd[0])) {
|
||||
continue
|
||||
}
|
||||
words = append(words, wd)
|
||||
}
|
||||
return
|
||||
}
|
Loading…
Reference in new issue