From 7089bb53b273633f46c82181cdea30f6569e6752 Mon Sep 17 00:00:00 2001 From: Ke Chen Date: Sat, 11 Nov 2023 00:22:58 +0800 Subject: [PATCH] feat: add weak word filter --- data/data.go | 19 ++++++++++++++++++- models/floor.go | 3 ++- utils/sensitive.go | 16 ++++++++++++---- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/data/data.go b/data/data.go index dc9e1d1..fde798c 100644 --- a/data/data.go +++ b/data/data.go @@ -19,6 +19,8 @@ var NamesMapping map[string]string var SensitiveWordFilter *sensitive.Filter +var WeakSensitiveWordFilter *sensitive.Filter + func init() { err := initNamesMapping() if err != nil { @@ -29,6 +31,11 @@ func init() { if err != nil { log.Err(err).Msg("could not init sensitive words") } + + err = initWeakSensitiveWords() + if err != nil { + log.Err(err).Msg("could not init weak sensitive words") + } } func initNamesMapping() error { @@ -42,7 +49,17 @@ func initNamesMapping() error { func initSensitiveWords() error { SensitiveWordFilter = sensitive.New() - err := SensitiveWordFilter.LoadWordDict("data/sensitive_words.txt") + err := WeakSensitiveWordFilter.LoadWordDict("data/sensitive_words.txt") + if err != nil { + WeakSensitiveWordFilter = nil + return err + } + return nil +} + +func initWeakSensitiveWords() error { + WeakSensitiveWordFilter = sensitive.New() + err := SensitiveWordFilter.LoadWordDict("data/weak_sensitive_words.txt") if err != nil { SensitiveWordFilter = nil return err diff --git a/models/floor.go b/models/floor.go index ee474de..d9f7c54 100644 --- a/models/floor.go +++ b/models/floor.go @@ -345,7 +345,8 @@ func (floor *Floor) SensitiveCheck(tx *gorm.DB, hole *Hole) (err error) { hasZZMGTag = true } } - if hasZZMGTag && utils.IsSensitive(floor.Content) { + + if utils.IsSensitive(floor.Content, !hasZZMGTag) { floor.IsSensitive = true floor.IsActualSensitive = nil } diff --git a/utils/sensitive.go b/utils/sensitive.go index c8f9ae0..85f4403 100644 --- a/utils/sensitive.go +++ b/utils/sensitive.go @@ -5,14 +5,22 @@ import ( "treehole_next/data" ) -func IsSensitive(content string) bool { +func IsSensitive(content string, weak bool) bool { if !config.Config.OpenSensitiveCheck { return false } - if data.SensitiveWordFilter != nil { - in, _ := data.SensitiveWordFilter.FindIn(content) - return in + if weak { + if data.WeakSensitiveWordFilter != nil { + in, _ := data.WeakSensitiveWordFilter.FindIn(content) + return in + } + } else { + if data.SensitiveWordFilter != nil { + in, _ := data.SensitiveWordFilter.FindIn(content) + return in + } } + return true }