MCPcopy Create free account
hub / github.com/aiprodcoder/MIXAPI / SensitiveWordReplace

Function SensitiveWordReplace

service/sensitive.go:69–94  ·  view source on GitHub ↗

SensitiveWordReplace 敏感词替换,返回是否包含敏感词和替换后的文本

(text string, returnImmediately bool)

Source from the content-addressed store, hash-verified

67
68// SensitiveWordReplace 敏感词替换,返回是否包含敏感词和替换后的文本
69func SensitiveWordReplace(text string, returnImmediately bool) (bool, []string, string) {
70 if len(setting.SensitiveWords) == 0 {
71 return false, nil, text
72 }
73 checkText := strings.ToLower(text)
74 m := InitAc(setting.SensitiveWords)
75 hits := m.MultiPatternSearch([]rune(checkText), returnImmediately)
76 if len(hits) > 0 {
77 words := make([]string, 0, len(hits))
78 var builder strings.Builder
79 builder.Grow(len(text))
80 lastPos := 0
81
82 for _, hit := range hits {
83 pos := hit.Pos
84 word := string(hit.Word)
85 builder.WriteString(text[lastPos:pos])
86 builder.WriteString("**###**")
87 lastPos = pos + len(word)
88 words = append(words, word)
89 }
90 builder.WriteString(text[lastPos:])
91 return true, words, builder.String()
92 }
93 return false, nil, text
94}

Callers

nothing calls this directly

Calls 1

InitAcFunction · 0.85

Tested by

no test coverage detected