From c7658b70d1202a10cb673a205a21a35287408e7d Mon Sep 17 00:00:00 2001 From: CaIon <1808837298@qq.com> Date: Wed, 20 Mar 2024 22:33:22 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=95=8F=E6=84=9F=E8=AF=8D=E5=BA=93?= =?UTF-8?q?=E4=B8=BA=E7=A9=BA=E6=97=B6=EF=BC=8C=E4=B8=8D=E6=A3=80=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- constant/sensitive.go | 8 +++++++- service/sensitive.go | 6 ++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/constant/sensitive.go b/constant/sensitive.go index fabca67..4a9a7e6 100644 --- a/constant/sensitive.go +++ b/constant/sensitive.go @@ -23,7 +23,13 @@ func SensitiveWordsToString() 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 { diff --git a/service/sensitive.go b/service/sensitive.go index b216376..dbb0887 100644 --- a/service/sensitive.go +++ b/service/sensitive.go @@ -10,6 +10,9 @@ import ( // SensitiveWordContains 是否包含敏感词,返回是否包含敏感词和敏感词列表 func SensitiveWordContains(text string) (bool, []string) { + if len(constant.SensitiveWords) == 0 { + return false, nil + } checkText := strings.ToLower(text) // 构建一个AC自动机 m := initAc() @@ -26,6 +29,9 @@ func SensitiveWordContains(text string) (bool, []string) { // SensitiveWordReplace 敏感词替换,返回是否包含敏感词和替换后的文本 func SensitiveWordReplace(text string, returnImmediately bool) (bool, []string, string) { + if len(constant.SensitiveWords) == 0 { + return false, nil, text + } checkText := strings.ToLower(text) m := initAc() hits := m.MultiPatternSearch([]rune(checkText), returnImmediately)