-
Notifications
You must be signed in to change notification settings - Fork 9
/
swearfilter.go
146 lines (121 loc) · 4.08 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
package swearfilter
import (
"regexp"
"strings"
"sync"
"unicode"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
)
//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)
//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)
//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
}
//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
}