From b92d2c86a559f0b33d5ec6ac56907d8de369f560 Mon Sep 17 00:00:00 2001 From: Umputun Date: Thu, 26 Dec 2024 13:00:36 -0600 Subject: [PATCH] rename opts to make it easier to understand --- app/main.go | 12 ++++++------ lib/tgspam/detector.go | 6 +++--- lib/tgspam/detector_test.go | 6 +++--- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/app/main.go b/app/main.go index 40a6ba6..b4e20be 100644 --- a/app/main.go +++ b/app/main.go @@ -86,11 +86,11 @@ type options struct { RetryCount int `long:"retry-count" env:"RETRY_COUNT" default:"1" description:"openai retry count"` } `group:"openai" namespace:"openai" env-namespace:"OPENAI"` - AbnormalWords struct { + AbnormalSpacing struct { Enabled bool `long:"enabled" env:"ENABLED" description:"enable abnormal words check"` SpaceRatioThreshold float64 `long:"ratio" env:"RATIO" default:"0.3" description:"the ratio of spaces to all characters in the message"` - ShortWordThreshold int `long:"short-word" env:"SHORT_WORD" default:"3" description:"the length of the word to be considered short"` ShortWordRatioThreshold float64 `long:"short-ratio" env:"SHORT_RATIO" default:"0.7" description:"the ratio of short words to all words in the message"` + ShortWordLen int `long:"short-word" env:"SHORT_WORD" default:"3" description:"the length of the word to be considered short"` } `group:"space" namespace:"space" env-namespace:"SPACE"` Files struct { @@ -467,12 +467,12 @@ func makeDetector(opts options) *tgspam.Detector { detector.WithOpenAIChecker(openai.NewClientWithConfig(config), openAIConfig) } - if opts.AbnormalWords.Enabled { + if opts.AbnormalSpacing.Enabled { log.Printf("[INFO] words spacing check enabled") detector.AbnormalSpacing.Enabled = true - detector.AbnormalSpacing.ShortWordThreshold = opts.AbnormalWords.ShortWordThreshold - detector.AbnormalSpacing.ShortWordRatioThreshold = opts.AbnormalWords.ShortWordRatioThreshold - detector.AbnormalSpacing.SpaceRatioThreshold = opts.AbnormalWords.SpaceRatioThreshold + detector.AbnormalSpacing.ShortWordLen = opts.AbnormalSpacing.ShortWordLen + detector.AbnormalSpacing.ShortWordRatioThreshold = opts.AbnormalSpacing.ShortWordRatioThreshold + detector.AbnormalSpacing.SpaceRatioThreshold = opts.AbnormalSpacing.SpaceRatioThreshold } metaChecks := []tgspam.MetaCheck{} diff --git a/lib/tgspam/detector.go b/lib/tgspam/detector.go index b70e419..a2ad681 100644 --- a/lib/tgspam/detector.go +++ b/lib/tgspam/detector.go @@ -59,7 +59,7 @@ type Config struct { AbnormalSpacing struct { Enabled bool // if true, enable check for abnormal spacing - ShortWordThreshold int // the length of the word to be considered short (in rune characters) + ShortWordLen int // the length of the word to be considered short (in rune characters) ShortWordRatioThreshold float64 // the ratio of short words to all words in the message SpaceRatioThreshold float64 // the ratio of spaces to all characters in the message } @@ -692,10 +692,10 @@ func (d *Detector) isAbnormalSpacing(msg string) spamcheck.Response { // look for suspicious word lengths and spacing patterns shortWords := 0 - if d.AbnormalSpacing.ShortWordThreshold > 0 { // if ShortWordThreshold is 0, skip short word detection + if d.AbnormalSpacing.ShortWordLen > 0 { // if ShortWordLen is 0, skip short word detection for _, word := range words { wordRunes := []rune(word) - if len(wordRunes) <= d.AbnormalSpacing.ShortWordThreshold && len(wordRunes) > 0 { + if len(wordRunes) <= d.AbnormalSpacing.ShortWordLen && len(wordRunes) > 0 { shortWords++ } } diff --git a/lib/tgspam/detector_test.go b/lib/tgspam/detector_test.go index b7dfc2e..8fd2d52 100644 --- a/lib/tgspam/detector_test.go +++ b/lib/tgspam/detector_test.go @@ -695,7 +695,7 @@ func TestDetector_CheckMultiLang(t *testing.T) { func TestDetector_CheckWithAbnormalSpacing(t *testing.T) { d := NewDetector(Config{MaxAllowedEmoji: -1}) d.Config.AbnormalSpacing.Enabled = true - d.Config.AbnormalSpacing.ShortWordThreshold = 3 + d.Config.AbnormalSpacing.ShortWordLen = 3 d.Config.AbnormalSpacing.ShortWordRatioThreshold = 0.7 d.Config.AbnormalSpacing.SpaceRatioThreshold = 0.3 @@ -785,7 +785,7 @@ func TestDetector_CheckWithAbnormalSpacing(t *testing.T) { } t.Run("disabled short word threshold", func(t *testing.T) { - d.Config.AbnormalSpacing.ShortWordThreshold = 0 + d.Config.AbnormalSpacing.ShortWordLen = 0 spam, resp := d.Check(spamcheck.Request{Msg: "СРО ЧНО ЭТО КАС АЕТ СЯ КАЖ ДОГО В ЭТ ОЙ ГРУ ППЕ something else"}) t.Logf("Response: %+v", resp) assert.False(t, spam) @@ -793,7 +793,7 @@ func TestDetector_CheckWithAbnormalSpacing(t *testing.T) { }) t.Run("enabled short word threshold", func(t *testing.T) { - d.Config.AbnormalSpacing.ShortWordThreshold = 3 + d.Config.AbnormalSpacing.ShortWordLen = 3 spam, resp := d.Check(spamcheck.Request{Msg: "СРО ЧНО ЭТО КАС АЕТ СЯ КАЖ ДОГО В ЭТ ОЙ ГРУ ППЕ something else"}) t.Logf("Response: %+v", resp) assert.True(t, spam)