-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathswearfilter.go
224 lines (189 loc) · 5.63 KB
/
swearfilter.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
package swearfilter
import (
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
"regexp"
"strings"
"sync"
"unicode"
)
var multiCharLeet = map[string]string{
"vv": "w",
"uu": "w",
`\/\/`: "w",
"><": "x",
"1<": "k",
"|<": "k",
"()": "o",
"[]": "o",
"ph": "f",
}
var leetChars = map[string]string{
"4": "a",
"@": "a",
"8": "b",
"(": "c",
"<": "c",
"[": "c",
"3": "e",
`€`: "e", // Unicode escape
"6": "g",
"9": "g",
"#": "h",
"j": "i",
"0": "o",
"5": "s",
"$": "s",
"7": "t",
"+": "t",
"v": "u",
"2": "z",
}
var ambiguousLeetMap = map[string][]string{
"!": {"i", "l"},
"|": {"i", "l"},
"1": {"i", "l"},
"]": {"i", "l"},
"}": {"i", "l"},
}
// SwearFilter contains settings for the swear filter
type SwearFilter struct {
//Options to tell the swear filter how to operate
DisableNormalize bool //Disables normalization of alphabetic characters if set to true (ex: à -> a)
DisableSpacedTab bool //Disables converting tabs to singular spaces (ex: [tab][tab] -> [space][space])
DisableMultiWhitespaceStripping bool //Disables stripping down multiple whitespaces (ex: hello[space][space]world -> hello[space]world)
DisableZeroWidthStripping bool //Disables stripping zero-width spaces
EnableSpacedBypass bool //Disables testing for spaced bypasses (if hell is in filter, look for occurrences of h and detect only alphabetic characters that follow; ex: h[space]e[space]l[space]l[space] -> hell)
DisableLeetSpeak bool
//A list of words to check against the filters
BadWords map[string]struct{}
mutex sync.RWMutex
}
// NewSwearFilter returns an initialized SwearFilter struct to check messages against
func NewSwearFilter(enableSpacedBypass bool, uhohwords ...string) (filter *SwearFilter) {
filter = &SwearFilter{
EnableSpacedBypass: enableSpacedBypass,
BadWords: make(map[string]struct{}),
}
for _, word := range uhohwords {
filter.BadWords[word] = struct{}{}
}
return
}
// Check will return any words that trip an enabled swear filter, an error if any, or nothing if you've removed all the words for some reason
func (filter *SwearFilter) Check(msg string) (trippedWords []string, err error) {
filter.mutex.RLock()
defer filter.mutex.RUnlock()
if filter.BadWords == nil || len(filter.BadWords) == 0 {
return nil, nil
}
message := strings.ToLower(msg)
if !filter.DisableLeetSpeak {
message = filter.normalizeLeetSpeak(message)
}
//Normalize the text
if !filter.DisableNormalize {
bytes := make([]byte, len(message))
normalize := transform.Chain(norm.NFD, transform.RemoveFunc(func(r rune) bool {
return unicode.Is(unicode.Mn, r)
}), norm.NFC)
_, _, err = normalize.Transform(bytes, []byte(message), true)
if err != nil {
return nil, err
}
message = string(bytes)
}
//Turn tabs into spaces
if !filter.DisableSpacedTab {
message = strings.Replace(message, "\t", " ", -1)
}
//Get rid of zero-width spaces
if !filter.DisableZeroWidthStripping {
message = strings.Replace(message, "\u200b", "", -1)
}
//Convert multiple re-occurring whitespaces into a single space
if !filter.DisableMultiWhitespaceStripping {
regexLeadCloseWhitepace := regexp.MustCompile(`^[\s\p{Zs}]+|[\s\p{Zs}]+$`)
message = regexLeadCloseWhitepace.ReplaceAllString(message, "")
regexInsideWhitespace := regexp.MustCompile(`[\s\p{Zs}]{2,}`)
message = regexInsideWhitespace.ReplaceAllString(message, "")
}
trippedWords = make([]string, 0)
checkSpace := false
for swear := range filter.BadWords {
if swear == " " {
checkSpace = true
continue
}
if strings.Contains(message, swear) {
trippedWords = append(trippedWords, swear)
continue
}
if filter.EnableSpacedBypass {
nospaceMessage := strings.Replace(message, " ", "", -1)
if strings.Contains(nospaceMessage, swear) {
trippedWords = append(trippedWords, swear)
}
}
}
if checkSpace && message == "" {
trippedWords = append(trippedWords, " ")
}
return
}
func (filter *SwearFilter) normalizeLeetSpeak(message string) string {
normalized := strings.ToLower(message)
// Handle multi-character replacements first
for leet, normal := range multiCharLeet {
normalized = strings.ReplaceAll(normalized, leet, normal)
}
// Handle single character replacements
for leet, normal := range leetChars {
normalized = strings.ReplaceAll(normalized, leet, normal)
}
var possibleStrings []string
for leet, possibilities := range ambiguousLeetMap {
if strings.Contains(normalized, leet) {
for _, replacement := range possibilities {
newStr := strings.ReplaceAll(normalized, leet, replacement)
possibleStrings = append(possibleStrings, newStr)
}
}
}
// Join all possible interpretations with spaces and check as one string
if len(possibleStrings) > 0 {
normalized = strings.Join(possibleStrings, " ")
}
return normalized
}
// Add appends the given word to the uhohwords list
func (filter *SwearFilter) Add(badWords ...string) {
filter.mutex.Lock()
defer filter.mutex.Unlock()
if filter.BadWords == nil {
filter.BadWords = make(map[string]struct{})
}
for _, word := range badWords {
filter.BadWords[word] = struct{}{}
}
}
// Delete deletes the given word from the uhohwords list
func (filter *SwearFilter) Delete(badWords ...string) {
filter.mutex.Lock()
defer filter.mutex.Unlock()
for _, word := range badWords {
delete(filter.BadWords, word)
}
}
// Words return the uhohwords list
func (filter *SwearFilter) Words() (activeWords []string) {
filter.mutex.RLock()
defer filter.mutex.RUnlock()
if filter.BadWords == nil {
return nil
}
for word := range filter.BadWords {
activeWords = append(activeWords, word)
}
return
}