Skip to content

Commit

Permalink
rename opts to make it easier to understand
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed Dec 26, 2024
1 parent 46cb3a2 commit b92d2c8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
12 changes: 6 additions & 6 deletions app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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{}
Expand Down
6 changes: 3 additions & 3 deletions lib/tgspam/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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++
}
}
Expand Down
6 changes: 3 additions & 3 deletions lib/tgspam/detector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -785,15 +785,15 @@ 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)
assert.Equal(t, "normal spacing (ratio: 0.29, short words: 0%)", resp[0].Details)
})

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)
Expand Down

0 comments on commit b92d2c8

Please sign in to comment.