-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
goaway.go
332 lines (304 loc) · 11.1 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
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
)
// ProfanityDetector contains the dictionaries as well as the configuration
// for determining how profanity detection is handled
type ProfanityDetector struct {
sanitizeSpecialCharacters bool // Whether to replace characters with the value ' ' in characterReplacements
sanitizeLeetSpeak bool // Whether to replace characters with a non-' ' value in characterReplacements
sanitizeAccents bool
sanitizeSpaces bool
profanities []string
falseNegatives []string
falsePositives []string
characterReplacements map[rune]rune
}
// NewProfanityDetector creates a new ProfanityDetector
func NewProfanityDetector() *ProfanityDetector {
return &ProfanityDetector{
sanitizeSpecialCharacters: true,
sanitizeLeetSpeak: true,
sanitizeAccents: true,
sanitizeSpaces: true,
profanities: DefaultProfanities,
falsePositives: DefaultFalsePositives,
falseNegatives: DefaultFalseNegatives,
characterReplacements: DefaultCharacterReplacements,
}
}
// WithSanitizeLeetSpeak allows configuring whether the sanitization process should also take into account leetspeak
//
// Leetspeak characters are characters to be replaced by non-' ' values in the characterReplacements map.
// For instance, '4' is replaced by 'a' and '3' is replaced by 'e', which means that "4sshol3" would be
// sanitized to "asshole", which would be detected as a profanity.
//
// By default, this is set to true.
func (g *ProfanityDetector) WithSanitizeLeetSpeak(sanitize bool) *ProfanityDetector {
g.sanitizeLeetSpeak = sanitize
return g.buildCharacterReplacements()
}
// WithSanitizeSpecialCharacters allows configuring whether the sanitization process should also take into account
// special characters.
//
// Special characters are characters that are part of the characterReplacements map (DefaultCharacterReplacements by
// default) and are to be removed during the sanitization step.
//
// For instance, "fu_ck" would be sanitized to "fuck", which would be detected as a profanity.
//
// By default, this is set to true.
func (g *ProfanityDetector) WithSanitizeSpecialCharacters(sanitize bool) *ProfanityDetector {
g.sanitizeSpecialCharacters = sanitize
return g.buildCharacterReplacements()
}
// 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
}
// 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
}
// WithCustomDictionary allows configuring whether the sanitization process should also take into account
// custom profanities, false positives and false negatives dictionaries.
// All dictionaries are expected to be lowercased.
func (g *ProfanityDetector) WithCustomDictionary(profanities, falsePositives, falseNegatives []string) *ProfanityDetector {
g.profanities = profanities
g.falsePositives = falsePositives
g.falseNegatives = falseNegatives
return g
}
// WithCustomCharacterReplacements allows configuring characters that to be replaced by other characters.
//
// Note that all entries that have the value ' ' are considered as special characters while all entries with a value
// that is not ' ' are considered as leet speak.
//
// Defaults to DefaultCharacterReplacements
func (g *ProfanityDetector) WithCustomCharacterReplacements(characterReplacements map[rune]rune) *ProfanityDetector {
g.characterReplacements = characterReplacements
return g
}
// IsProfane takes in a string (word or sentence) and look for profanities.
// Returns a boolean
func (g *ProfanityDetector) IsProfane(s string) bool {
return len(g.ExtractProfanity(s)) > 0
}
// ExtractProfanity takes in a string (word or sentence) and look for profanities.
// 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 {
return word
}
}
// Remove false positives
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
}
}
return ""
}
func (g *ProfanityDetector) indexToRune(s string, index int) int {
count := 0
for i := range s {
if i == index {
break
}
if i < index {
count++
}
}
return count
}
func (g *ProfanityDetector) Censor(s string) string {
censored := []rune(s)
var originalIndexes []int
s, originalIndexes = g.sanitize(s, true)
runeWordLength := 0
g.checkProfanity(&s, &originalIndexes, &censored, g.falseNegatives, &runeWordLength)
g.removeFalsePositives(&s, &originalIndexes, &runeWordLength)
g.checkProfanity(&s, &originalIndexes, &censored, g.profanities, &runeWordLength)
return string(censored)
}
func (g *ProfanityDetector) checkProfanity(s *string, originalIndexes *[]int, censored *[]rune, wordList []string, runeWordLength *int) {
for _, word := range wordList {
currentIndex := 0
*runeWordLength = len([]rune(word))
for currentIndex != -1 {
if foundIndex := strings.Index((*s)[currentIndex:], word); foundIndex != -1 {
for i := 0; i < *runeWordLength; i++ {
runeIndex := g.indexToRune(*s, currentIndex+foundIndex) + i
if runeIndex < len(*originalIndexes) {
(*censored)[(*originalIndexes)[runeIndex]] = '*'
}
}
currentIndex += foundIndex + len([]byte(word))
} else {
break
}
}
}
}
func (g *ProfanityDetector) removeFalsePositives(s *string, originalIndexes *[]int, runeWordLength *int) {
for _, word := range g.falsePositives {
currentIndex := 0
*runeWordLength = len([]rune(word))
for currentIndex != -1 {
if foundIndex := strings.Index((*s)[currentIndex:], word); foundIndex != -1 {
foundRuneIndex := g.indexToRune(*s, foundIndex)
*originalIndexes = append((*originalIndexes)[:foundRuneIndex], (*originalIndexes)[foundRuneIndex+*runeWordLength:]...)
currentIndex += foundIndex + len([]byte(word))
} else {
break
}
}
*s = strings.Replace(*s, word, "", -1)
}
}
func (g ProfanityDetector) sanitize(s string, rememberOriginalIndexes bool) (string, []int) {
s = strings.ToLower(s)
if g.sanitizeLeetSpeak && !rememberOriginalIndexes && g.sanitizeSpecialCharacters {
s = strings.ReplaceAll(s, "()", "o")
}
sb := strings.Builder{}
for _, char := range s {
if replacement, found := g.characterReplacements[char]; found {
if g.sanitizeSpecialCharacters && replacement == ' ' {
// If the replacement is a space, and we're sanitizing special characters speak, we replace.
sb.WriteRune(replacement)
continue
} else if g.sanitizeLeetSpeak && replacement != ' ' {
// If the replacement isn't a space, and we're sanitizing leet speak, we replace.
sb.WriteRune(replacement)
continue
}
}
sb.WriteRune(char)
}
s = sb.String()
if g.sanitizeAccents {
s = removeAccents(s)
}
var originalIndexes []int
if rememberOriginalIndexes {
for i, c := range []rune(s) {
// If spaces aren't being sanitized, appending to the original indices prevents off-by-one errors later on.
if c != ' ' || !g.sanitizeSpaces {
originalIndexes = append(originalIndexes, i)
}
}
}
if g.sanitizeSpaces {
s = strings.Replace(s, space, "", -1)
}
return s, originalIndexes
}
// removeAccents strips all accents from characters.
// Only called if ProfanityDetector.removeAccents is set to true
func removeAccents(s string) string {
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
}
// buildCharacterReplacements builds characterReplacements if WithSanitizeLeetSpeak or WithSanitizeSpecialCharacters is
// called.
//
// If this is not called, DefaultCharacterReplacements
func (g *ProfanityDetector) buildCharacterReplacements() *ProfanityDetector {
g.characterReplacements = make(map[rune]rune)
if g.sanitizeSpecialCharacters {
g.characterReplacements['-'] = ' '
g.characterReplacements['_'] = ' '
g.characterReplacements['|'] = ' '
g.characterReplacements['.'] = ' '
g.characterReplacements[','] = ' '
g.characterReplacements['('] = ' '
g.characterReplacements[')'] = ' '
g.characterReplacements['<'] = ' '
g.characterReplacements['>'] = ' '
g.characterReplacements['"'] = ' '
g.characterReplacements['`'] = ' '
g.characterReplacements['~'] = ' '
g.characterReplacements['*'] = ' '
g.characterReplacements['&'] = ' '
g.characterReplacements['%'] = ' '
g.characterReplacements['$'] = ' '
g.characterReplacements['#'] = ' '
g.characterReplacements['@'] = ' '
g.characterReplacements['!'] = ' '
g.characterReplacements['?'] = ' '
g.characterReplacements['+'] = ' '
}
if g.sanitizeLeetSpeak {
g.characterReplacements['4'] = 'a'
g.characterReplacements['$'] = 's'
g.characterReplacements['!'] = 'i'
g.characterReplacements['+'] = 't'
g.characterReplacements['#'] = 'h'
g.characterReplacements['@'] = 'a'
g.characterReplacements['0'] = 'o'
g.characterReplacements['1'] = 'i'
g.characterReplacements['7'] = 'l'
g.characterReplacements['3'] = 'e'
g.characterReplacements['5'] = 's'
g.characterReplacements['<'] = 'c'
}
return g
}
// 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)
}
// ExtractProfanity takes in a string (word or sentence) and look for profanities.
// Returns the first profanity found, or an empty string if none are found
//
// Uses the default ProfanityDetector
func ExtractProfanity(s string) string {
if defaultProfanityDetector == nil {
defaultProfanityDetector = NewProfanityDetector()
}
return defaultProfanityDetector.ExtractProfanity(s)
}
// Censor takes in a string (word or sentence) and tries to censor all profanities found.
//
// Uses the default ProfanityDetector
func Censor(s string) string {
if defaultProfanityDetector == nil {
defaultProfanityDetector = NewProfanityDetector()
}
return defaultProfanityDetector.Censor(s)
}