Skip to content

Commit

Permalink
Merge pull request #22 from Luberry/master
Browse files Browse the repository at this point in the history
added the ability to disable space sanitization
  • Loading branch information
TwiN authored Sep 30, 2021
2 parents 39ccb87 + 1f7e2c3 commit 1389806
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
12 changes: 11 additions & 1 deletion goaway.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type ProfanityDetector struct {
sanitizeSpecialCharacters bool
sanitizeLeetSpeak bool
sanitizeAccents bool
sanitizeSpaces bool

profanities []string
falseNegatives []string
Expand All @@ -38,6 +39,7 @@ func NewProfanityDetector() *ProfanityDetector {
sanitizeSpecialCharacters: true,
sanitizeLeetSpeak: true,
sanitizeAccents: true,
sanitizeSpaces: true,
profanities: DefaultProfanities,
falsePositives: DefaultFalsePositives,
falseNegatives: DefaultFalseNegatives,
Expand Down Expand Up @@ -74,6 +76,12 @@ func (g *ProfanityDetector) WithCustomDictionary(profanities, falsePositives, fa
return g
}

// WithSanitizeSpaces allows configuring whether the sanitization process should also take into account spaces
func (g *ProfanityDetector) WithSanitizeSpaces(sanitize bool) *ProfanityDetector {
g.sanitizeSpaces = sanitize
return g
}

// IsProfane takes in a string (word or sentence) and look for profanities.
// Returns a boolean
func (g *ProfanityDetector) IsProfane(s string) bool {
Expand Down Expand Up @@ -130,7 +138,9 @@ func (g ProfanityDetector) sanitize(s string) string {
s = strings.Replace(s, "?", "", -1)
s = strings.Replace(s, "!", "", -1)
}
s = strings.Replace(s, space, "", -1)
if g.sanitizeSpaces {
s = strings.Replace(s, space, "", -1)
}
if g.sanitizeAccents {
s = removeAccents(s)
}
Expand Down
29 changes: 29 additions & 0 deletions goaway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,35 @@ func TestBadWords(t *testing.T) {
}
}

func TestBadWordsWithSpaces(t *testing.T) {
profanities := []string{"fuck", "ass", "poop", "penis", "bitch"}
words := []string{"fu ck", "as s", "po op", "pe ni s", "bit ch"}
tests := []struct {
name string
profanityDetector *ProfanityDetector
}{
{
name: "With Default Dictionary",
profanityDetector: NewProfanityDetector(),
},
{
name: "With Custom Dictionary",
profanityDetector: NewProfanityDetector().WithCustomDictionary(profanities, DefaultFalsePositives, DefaultFalseNegatives),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
for _, w := range words {
if !tt.profanityDetector.WithSanitizeSpaces(true).IsProfane(w) {
t.Error("Expected true because sanitizeSpaces is set to true, got false from word", w)
}
if tt.profanityDetector.WithSanitizeSpaces(false).IsProfane(w) {
t.Error("Expected false because sanitizeSpaces is set to false, got true from word", w)
}
}
})
}
}
func TestBadWordsWithAccentedLetters(t *testing.T) {
profanities := []string{"fuck", "ass", "poop", "penis", "bitch"}
words := []string{"fučk", "ÄšŚ", "pÓöp", "pÉnìŚ", "bitčh"}
Expand Down

0 comments on commit 1389806

Please sign in to comment.