Ai
1 Star 1 Fork 0

kaycn/blevek

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
index.go 3.02 KB
一键复制 编辑 原始数据 按行查看 历史
kaycn 提交于 2020-06-06 11:20 +08:00 . leveldb
package blevejieba
import (
"github.com/blevesearch/bleve"
_ "github.com/blevesearch/bleve/config"
"github.com/blevesearch/bleve/mapping"
_ "github.com/blevesearch/blevex/leveldb"
"github.com/yanyiwu/gojieba"
)
type Options struct {
dictPath, hmmPath, userDictPath, idfPath, stopDictPath string
isSearch bool
}
func NewOptions() *Options {
return &Options{
dictPath: gojieba.DICT_PATH,
hmmPath: gojieba.HMM_PATH,
userDictPath: gojieba.USER_DICT_PATH,
idfPath: gojieba.IDF_PATH,
stopDictPath: gojieba.STOP_WORDS_PATH,
isSearch: false,
}
}
func (o *Options) WithJiebaDictPath(p string) *Options {
o.dictPath = p
return o
}
func (o *Options) WithHMMPath(p string) *Options {
o.hmmPath = p
return o
}
func (o *Options) WithUserDictPath(p string) *Options {
o.userDictPath = p
return o
}
func (o *Options) WithIDFDictPath(p string) *Options {
o.idfPath = p
return o
}
func (o *Options) WithStopDictPath(p string) *Options {
o.stopDictPath = p
return o
}
func (o *Options) WithSearch(search bool) *Options {
o.isSearch = search
return o
}
func (o *Options) customTokenizerConfig() map[string]interface{} {
return map[string]interface{}{
"type": Name,
isSearchKey: o.isSearch,
dictPathKey: o.dictPath,
hmmPathKey: o.hmmPath,
userDictPathKey: o.userDictPath,
idfDictPathKey: o.idfPath,
stopDictPathKey: o.stopDictPath,
}
}
func (o *Options) customAnalyzerConfig() map[string]interface{} {
return map[string]interface{}{
"type": Name,
}
}
func (o *Options) customTokenMapConfig() map[string]interface{} {
return map[string]interface{}{
"type": Name,
stopDictPathKey: o.stopDictPath,
}
}
func NewMemIndexWithGoJieba(opt *Options) (bleve.Index, error) {
mappingImpl, err := NewGoJiebaIndexMapping(opt)
if err != nil {
return nil, err
}
index, err := bleve.NewMemOnly(mappingImpl)
if err != nil {
return nil, err
}
return index, nil
}
func NewStoreIndexWithGoJieba(store string,opt *Options) (bleve.Index, error) {
mappingImpl, err := NewGoJiebaIndexMapping(opt)
if err != nil {
return nil, err
}
index, err := bleve.New(store,mappingImpl)
if err != nil {
return nil, err
}
return index, nil
}
func OpenStoreIndexWithGoJieba(store string,opt *Options) (bleve.Index, error) {
_, err := NewGoJiebaIndexMapping(opt)
if err != nil {
return nil, err
}
index, err := bleve.Open(store)
if err != nil {
return nil, err
}
return index, nil
}
func NewGoJiebaIndexMapping(opt *Options) (mapping.IndexMapping, error) {
bleve.Config.DefaultKVStore="leveldb"
indexMapping := bleve.NewIndexMapping()
err := indexMapping.AddCustomTokenizer(Name,
opt.customTokenizerConfig(),
)
if err != nil {
return nil, err
}
err = indexMapping.AddCustomTokenMap(Name, opt.customTokenMapConfig())
if err != nil {
return nil, err
}
err = indexMapping.AddCustomAnalyzer(Name,
opt.customAnalyzerConfig(),
)
if err != nil {
return nil, err
}
indexMapping.DefaultAnalyzer = Name
return indexMapping, nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/lkaycn/blevek.git
git@gitee.com:lkaycn/blevek.git
lkaycn
blevek
blevek
v1.0.9

搜索帮助