Sfoglia il codice sorgente

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

CaIon 1 anno fa
parent
commit
c7658b70d1
2 ha cambiato i file con 13 aggiunte e 1 eliminazioni
  1. 7 1
      constant/sensitive.go
  2. 6 0
      service/sensitive.go

+ 7 - 1
constant/sensitive.go

@@ -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 {

+ 6 - 0
service/sensitive.go

@@ -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) {
 
 
 // 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)