Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement WithExactWord to allow stricter matches #107

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 36 additions & 4 deletions goaway.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type ProfanityDetector struct {
sanitizeLeetSpeak bool // Whether to replace characters with a non-' ' value in characterReplacements
sanitizeAccents bool
sanitizeSpaces bool
exactWord bool

profanities []string
falseNegatives []string
Expand All @@ -41,6 +42,7 @@ func NewProfanityDetector() *ProfanityDetector {
sanitizeLeetSpeak: true,
sanitizeAccents: true,
sanitizeSpaces: true,
exactWord: false,
profanities: DefaultProfanities,
falsePositives: DefaultFalsePositives,
falseNegatives: DefaultFalseNegatives,
Expand Down Expand Up @@ -109,6 +111,16 @@ func (g *ProfanityDetector) WithCustomCharacterReplacements(characterReplacement
return g
}

// WithExactWord allows configuring whether the profanity check process should require exact matches or not.
// Using this reduces false positives and winds up more permissive.
//
// Note: this entails also setting WithSanitizeSpaces(false), since without spaces present exact word matching
// does not make sense.
func (g *ProfanityDetector) WithExactWord(exactWord bool) *ProfanityDetector {
g.exactWord = exactWord
return g.WithSanitizeSpaces(false)
}

// IsProfane takes in a string (word or sentence) and look for profanities.
// Returns a boolean
func (g *ProfanityDetector) IsProfane(s string) bool {
Expand All @@ -119,6 +131,7 @@ func (g *ProfanityDetector) IsProfane(s string) bool {
// Returns the first profanity found, or an empty string if none are found
func (g *ProfanityDetector) ExtractProfanity(s string) string {
s, _ = g.sanitize(s, false)

// Check for false negatives
for _, word := range g.falseNegatives {
if match := strings.Contains(s, word); match {
Expand All @@ -129,15 +142,34 @@ func (g *ProfanityDetector) ExtractProfanity(s string) string {
for _, word := range g.falsePositives {
s = strings.Replace(s, word, "", -1)
}
// Check for profanities
for _, word := range g.profanities {
if match := strings.Contains(s, word); match {
return word

if !g.exactWord {
// Check for profanities
for _, word := range g.profanities {
if match := strings.Contains(s, word); match {
return word
}
}
} else {
tokens := strings.Split(s, space)
for _, token := range tokens {
if sliceContains(g.profanities, token) {
return token
}
}
}
return ""
}

func sliceContains(words []string, s string) bool {
for _, word := range words {
if strings.EqualFold(s, word) {
return true
}
}
return false
}

func (g *ProfanityDetector) indexToRune(s string, index int) int {
count := 0
for i := range s {
Expand Down
34 changes: 34 additions & 0 deletions goaway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,36 @@ func TestFalsePositives(t *testing.T) {
}
}

func TestExactWord(t *testing.T) {
accept_sentences := []string{
"I'm an analyst",
}
reject_sentences := []string{"Go away, ass."}
tests := []struct {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
accept_sentences := []string{
"I'm an analyst",
}
reject_sentences := []string{"Go away, ass."}
tests := []struct {
accept_sentences := []string{
"I'm an analyst",
}
reject_sentences := []string{"Go away, ass."}
tests := []struct {

Go uses camelCase instead of snake_case - could you make the changes to the other parts of your code?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in bf6efc8, thank you! the perils of switching between languages and getting my styles mixed up 😅

name string
profanityDetector *ProfanityDetector
}{
{
name: "With Empty FalsePositives",
profanityDetector: NewProfanityDetector().WithExactWord(true).WithSanitizeSpecialCharacters(true).WithCustomDictionary(DefaultProfanities, nil, nil),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
for _, s := range accept_sentences {
if tt.profanityDetector.IsProfane(s) {
t.Error("Expected false, got true from:", s)
}
}
for _, s := range reject_sentences {
if !tt.profanityDetector.IsProfane(s) {
t.Error("Expected true, got false from:", s)
}
}
})
}
}

func TestFalseNegatives(t *testing.T) {
sentences := []string{
"dumb ass", // ass -> bASS (FP) -> dumBASS (FFP)
Expand All @@ -564,6 +594,10 @@ func TestFalseNegatives(t *testing.T) {
name: "With Custom Dictionary",
profanityDetector: NewProfanityDetector().WithCustomDictionary(DefaultProfanities, DefaultFalsePositives, DefaultFalseNegatives),
},
{
name: "With Custom Dictionary",
profanityDetector: NewProfanityDetector().WithCustomDictionary(DefaultProfanities, DefaultFalsePositives, DefaultFalseNegatives),
},
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{
name: "With Custom Dictionary",
profanityDetector: NewProfanityDetector().WithCustomDictionary(DefaultProfanities, DefaultFalsePositives, DefaultFalseNegatives),
},
{
name: "With Custom Dictionary",
profanityDetector: NewProfanityDetector().WithCustomDictionary(DefaultProfanities, DefaultFalsePositives, DefaultFalseNegatives),
},

Was this a typo? It's a duplicate of the scenario above

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I meant to have it include coverage of the new code by setting it to use exact words for one of them. pushed that fix in bf6efc8

}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down