Files
new-api/service/sensitive.go

72 lines
1.6 KiB
Go
Raw Normal View History

2024-03-20 17:07:42 +08:00
package service
import (
"bytes"
"fmt"
"github.com/anknown/ahocorasick"
"one-api/constant"
"strings"
)
// SensitiveWordContains 是否包含敏感词,返回是否包含敏感词和敏感词列表
func SensitiveWordContains(text string) (bool, []string) {
2024-03-20 22:33:22 +08:00
if len(constant.SensitiveWords) == 0 {
return false, nil
}
checkText := strings.ToLower(text)
2024-03-20 17:07:42 +08:00
// 构建一个AC自动机
m := initAc()
hits := m.MultiPatternSearch([]rune(checkText), false)
2024-03-20 17:07:42 +08:00
if len(hits) > 0 {
words := make([]string, 0)
for _, hit := range hits {
words = append(words, string(hit.Word))
}
return true, words
}
return false, nil
}
// SensitiveWordReplace 敏感词替换,返回是否包含敏感词和替换后的文本
2024-03-20 19:00:51 +08:00
func SensitiveWordReplace(text string, returnImmediately bool) (bool, []string, string) {
2024-03-20 22:33:22 +08:00
if len(constant.SensitiveWords) == 0 {
return false, nil, text
}
2024-03-20 20:26:34 +08:00
checkText := strings.ToLower(text)
2024-03-20 17:07:42 +08:00
m := initAc()
2024-03-20 20:26:34 +08:00
hits := m.MultiPatternSearch([]rune(checkText), returnImmediately)
2024-03-20 17:07:42 +08:00
if len(hits) > 0 {
2024-03-20 19:00:51 +08:00
words := make([]string, 0)
2024-03-20 17:07:42 +08:00
for _, hit := range hits {
pos := hit.Pos
word := string(hit.Word)
2024-03-21 14:29:56 +08:00
text = text[:pos] + "**###**" + text[pos+len(word):]
2024-03-20 19:00:51 +08:00
words = append(words, word)
2024-03-20 17:07:42 +08:00
}
2024-03-20 19:00:51 +08:00
return true, words, text
2024-03-20 17:07:42 +08:00
}
2024-03-20 19:00:51 +08:00
return false, nil, text
2024-03-20 17:07:42 +08:00
}
func initAc() *goahocorasick.Machine {
m := new(goahocorasick.Machine)
dict := readRunes()
if err := m.Build(dict); err != nil {
fmt.Println(err)
return nil
}
return m
}
func readRunes() [][]rune {
var dict [][]rune
for _, word := range constant.SensitiveWords {
2024-03-20 19:00:51 +08:00
word = strings.ToLower(word)
2024-03-20 17:07:42 +08:00
l := bytes.TrimSpace([]byte(word))
dict = append(dict, bytes.Runes(l))
}
return dict
}