Commit eda2ac43 by CaIon

fix: 敏感词库为空时,不检测

parent 38305d77
...@@ -23,7 +23,13 @@ func SensitiveWordsToString() string { ...@@ -23,7 +23,13 @@ func SensitiveWordsToString() string {
} }
func SensitiveWordsFromString(s string) { func SensitiveWordsFromString(s string) {
SensitiveWords = strings.Split(s, "\n") sw := strings.Split(s, "\n")
for _, w := range sw {
w = strings.TrimSpace(w)
if w != "" {
SensitiveWords = append(SensitiveWords, w)
}
}
} }
func ShouldCheckPromptSensitive() bool { func ShouldCheckPromptSensitive() bool {
......
...@@ -10,6 +10,9 @@ import ( ...@@ -10,6 +10,9 @@ import (
// SensitiveWordContains 是否包含敏感词,返回是否包含敏感词和敏感词列表 // SensitiveWordContains 是否包含敏感词,返回是否包含敏感词和敏感词列表
func SensitiveWordContains(text string) (bool, []string) { func SensitiveWordContains(text string) (bool, []string) {
if len(constant.SensitiveWords) == 0 {
return false, nil
}
checkText := strings.ToLower(text) checkText := strings.ToLower(text)
// 构建一个AC自动机 // 构建一个AC自动机
m := initAc() m := initAc()
...@@ -26,6 +29,9 @@ func SensitiveWordContains(text string) (bool, []string) { ...@@ -26,6 +29,9 @@ func SensitiveWordContains(text string) (bool, []string) {
// SensitiveWordReplace 敏感词替换,返回是否包含敏感词和替换后的文本 // SensitiveWordReplace 敏感词替换,返回是否包含敏感词和替换后的文本
func SensitiveWordReplace(text string, returnImmediately bool) (bool, []string, string) { func SensitiveWordReplace(text string, returnImmediately bool) (bool, []string, string) {
if len(constant.SensitiveWords) == 0 {
return false, nil, text
}
checkText := strings.ToLower(text) checkText := strings.ToLower(text)
m := initAc() m := initAc()
hits := m.MultiPatternSearch([]rune(checkText), returnImmediately) hits := m.MultiPatternSearch([]rune(checkText), returnImmediately)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment