refactor: Optimize sensitive word detection and text processing
This commit is contained in:
@@ -9,15 +9,23 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func CheckSensitiveMessages(messages []dto.Message) ([]string, error) {
|
func CheckSensitiveMessages(messages []dto.Message) ([]string, error) {
|
||||||
|
if len(messages) == 0 {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
for _, message := range messages {
|
for _, message := range messages {
|
||||||
arrayContent := message.ParseContent()
|
arrayContent := message.ParseContent()
|
||||||
for _, m := range arrayContent {
|
for _, m := range arrayContent {
|
||||||
if m.Type == "image_url" {
|
if m.Type == "image_url" {
|
||||||
// TODO: check image url
|
// TODO: check image url
|
||||||
} else {
|
continue
|
||||||
if ok, words := SensitiveWordContains(m.Text); ok {
|
}
|
||||||
return words, errors.New("sensitive words detected")
|
// 检查 text 是否为空
|
||||||
}
|
if m.Text == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if ok, words := SensitiveWordContains(m.Text); ok {
|
||||||
|
return words, errors.New("sensitive words detected")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -36,11 +44,11 @@ func CheckSensitiveInput(input any) ([]string, error) {
|
|||||||
case string:
|
case string:
|
||||||
return CheckSensitiveText(v)
|
return CheckSensitiveText(v)
|
||||||
case []string:
|
case []string:
|
||||||
text := ""
|
var builder strings.Builder
|
||||||
for _, s := range v {
|
for _, s := range v {
|
||||||
text += s
|
builder.WriteString(s)
|
||||||
}
|
}
|
||||||
return CheckSensitiveText(text)
|
return CheckSensitiveText(builder.String())
|
||||||
}
|
}
|
||||||
return CheckSensitiveText(fmt.Sprintf("%v", input))
|
return CheckSensitiveText(fmt.Sprintf("%v", input))
|
||||||
}
|
}
|
||||||
@@ -50,6 +58,9 @@ func SensitiveWordContains(text string) (bool, []string) {
|
|||||||
if len(setting.SensitiveWords) == 0 {
|
if len(setting.SensitiveWords) == 0 {
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
if len(text) == 0 {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
checkText := strings.ToLower(text)
|
checkText := strings.ToLower(text)
|
||||||
return AcSearch(checkText, setting.SensitiveWords, true)
|
return AcSearch(checkText, setting.SensitiveWords, true)
|
||||||
}
|
}
|
||||||
@@ -63,14 +74,21 @@ func SensitiveWordReplace(text string, returnImmediately bool) (bool, []string,
|
|||||||
m := InitAc(setting.SensitiveWords)
|
m := InitAc(setting.SensitiveWords)
|
||||||
hits := m.MultiPatternSearch([]rune(checkText), returnImmediately)
|
hits := m.MultiPatternSearch([]rune(checkText), returnImmediately)
|
||||||
if len(hits) > 0 {
|
if len(hits) > 0 {
|
||||||
words := make([]string, 0)
|
words := make([]string, 0, len(hits))
|
||||||
|
var builder strings.Builder
|
||||||
|
builder.Grow(len(text))
|
||||||
|
lastPos := 0
|
||||||
|
|
||||||
for _, hit := range hits {
|
for _, hit := range hits {
|
||||||
pos := hit.Pos
|
pos := hit.Pos
|
||||||
word := string(hit.Word)
|
word := string(hit.Word)
|
||||||
text = text[:pos] + "**###**" + text[pos+len(word):]
|
builder.WriteString(text[lastPos:pos])
|
||||||
|
builder.WriteString("**###**")
|
||||||
|
lastPos = pos + len(word)
|
||||||
words = append(words, word)
|
words = append(words, word)
|
||||||
}
|
}
|
||||||
return true, words, text
|
builder.WriteString(text[lastPos:])
|
||||||
|
return true, words, builder.String()
|
||||||
}
|
}
|
||||||
return false, nil, text
|
return false, nil, text
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user