gse配置修改

This commit is contained in:
2026-04-28 14:15:31 +08:00
parent 960dca6a50
commit 4960021748

View File

@@ -2,6 +2,8 @@ package utils
import (
"context"
"os"
"path/filepath"
"sort"
"sync"
@@ -54,7 +56,24 @@ func newGseTool() (tool *gseTool, err error) {
// 2. 初始化 TF-IDF 提取器
tfidf := &extracker.TagExtracter{}
tfidf.WithGse(seg)
// 尝试从默认路径加载 IDF 字典
idfPath := getIdfDictPath()
if idfPath != "" {
// 如果找到自定义路径,使用 LoadDict 方法加载
err = tfidf.LoadDict(idfPath)
if err != nil {
glog.Warningf(context.Background(), "加载自定义 IDF 字典失败 [%s]: %v将使用默认字典", idfPath, err)
// 回退到默认加载方式
err = tfidf.LoadIdf()
} else {
glog.Infof(context.Background(), "成功加载自定义 IDF 字典: %s", idfPath)
}
} else {
// 使用默认的 IDF 字典
err = tfidf.LoadIdf()
}
if err != nil {
return
}
@@ -71,6 +90,29 @@ func newGseTool() (tool *gseTool, err error) {
return
}
// getIdfDictPath 获取 IDF 字典文件路径
func getIdfDictPath() string {
// 1. 尝试从容器内的默认挂载路径加载Docker 卷映射)
containerPath := "/app/dict/zh/idf.txt"
if _, err := os.Stat(containerPath); err == nil {
return containerPath
}
// 2. 尝试从当前工作目录的 dict/zh/idf.txt 加载
workDir, err := os.Getwd()
if err != nil {
return ""
}
localPath := filepath.Join(workDir, "dict", "zh", "idf.txt")
if _, err := os.Stat(localPath); err == nil {
return localPath
}
// 3. 如果没有找到自定义路径,返回空字符串,使用默认字典
return ""
}
// Cut 分词(关键词提取唯一正确模式:精确模式 + HMM
func (k *gseTool) Cut(text string) []string {
return k.seg.Cut(text, true)