forked from TwiN/go-away
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoaway.go
141 lines (128 loc) · 4.13 KB
/
goaway.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package goaway
import (
"strings"
"unicode"
"golang.org/x/text/runes"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
)
const (
space = " "
firstRuneSupported = ' '
lastRuneSupported = '~'
)
var (
defaultProfanityDetector *ProfanityDetector
removeAccentsTransformer transform.Transformer
)
// ProfanityDetector
type ProfanityDetector struct {
sanitizeSpecialCharacters bool
sanitizeLeetSpeak bool
sanitizeAccents bool
}
// NewProfanityDetector creates a new ProfanityDetector
func NewProfanityDetector() *ProfanityDetector {
return &ProfanityDetector{
sanitizeSpecialCharacters: true,
sanitizeLeetSpeak: true,
sanitizeAccents: true,
}
}
// WithSanitizeLeetSpeak allows configuring whether the sanitization process should also take into account
// leetspeak
func (g *ProfanityDetector) WithSanitizeLeetSpeak(sanitize bool) *ProfanityDetector {
g.sanitizeLeetSpeak = sanitize
return g
}
// WithSanitizeSpecialCharacters allows configuring whether the sanitization process should also take into account
// special characters
func (g *ProfanityDetector) WithSanitizeSpecialCharacters(sanitize bool) *ProfanityDetector {
g.sanitizeSpecialCharacters = sanitize
return g
}
// WithSanitizeAccents allows configuring of whether the sanitization process should also take into account accents.
// By default, this is set to true, but since this adds a bit of overhead, you may disable it if your use case
// is time-sensitive or if the input doesn't involve accents (i.e. if the input can never contain special characters)
func (g *ProfanityDetector) WithSanitizeAccents(sanitize bool) *ProfanityDetector {
g.sanitizeAccents = 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 {
s = g.sanitize(s)
// Check for false false positives
for _, word := range falseNegatives {
if match := strings.Contains(s, word); match {
return true
}
}
// Remove false positives
for _, word := range falsePositives {
s = strings.Replace(s, word, "", -1)
}
// Check for profanities
for _, word := range profanities {
if match := strings.Contains(s, word); match {
return true
}
}
return false
}
func (g ProfanityDetector) sanitize(s string) string {
s = strings.ToLower(s)
if g.sanitizeLeetSpeak {
s = strings.Replace(s, "0", "o", -1)
s = strings.Replace(s, "1", "i", -1)
s = strings.Replace(s, "3", "e", -1)
s = strings.Replace(s, "4", "a", -1)
s = strings.Replace(s, "5", "s", -1)
s = strings.Replace(s, "6", "b", -1)
s = strings.Replace(s, "7", "l", -1)
s = strings.Replace(s, "8", "b", -1)
}
if g.sanitizeSpecialCharacters {
if g.sanitizeLeetSpeak {
s = strings.Replace(s, "@", "a", -1)
s = strings.Replace(s, "+", "t", -1)
s = strings.Replace(s, "$", "s", -1)
s = strings.Replace(s, "#", "h", -1)
s = strings.Replace(s, "()", "o", -1)
}
s = strings.Replace(s, "_", "", -1)
s = strings.Replace(s, "-", "", -1)
s = strings.Replace(s, "*", "", -1)
s = strings.Replace(s, "'", "", -1)
s = strings.Replace(s, "?", "", -1)
s = strings.Replace(s, "!", "", -1)
}
s = strings.Replace(s, space, "", -1)
if g.sanitizeAccents {
s = removeAccents(s)
}
return s
}
// removeAccents strips all accents from characters.
// Only called if ProfanityDetector.removeAccents is set to true
func removeAccents(s string) string {
if removeAccentsTransformer == nil {
removeAccentsTransformer = transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
}
for _, character := range s {
// If there's a character outside the range of supported runes, there might be some accented words
if character < firstRuneSupported || character > lastRuneSupported {
s, _, _ = transform.String(removeAccentsTransformer, s)
break
}
}
return s
}
// IsProfane checks whether there are any profanities in a given string (word or sentence).
// Uses the default ProfanityDetector
func IsProfane(s string) bool {
if defaultProfanityDetector == nil {
defaultProfanityDetector = NewProfanityDetector()
}
return defaultProfanityDetector.IsProfane(s)
}