-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.go
56 lines (48 loc) · 1.16 KB
/
filter.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
package main
import (
"strings"
"unicode/utf8"
jsoniter "github.com/json-iterator/go"
)
// filter provides rule sets for the input string to skip specified values.
func (c *config) filter(s string) bool {
// Skip empty strings.
if s == "" {
return false
}
// Skip strings less than min word length.
if utf8.RuneCountInString(s) < c.minWordLen {
return false
}
// Create a new map for JSON.
skipWords := make(map[string][]string, 0)
// Unmarshal JSON data to the result slice.
err := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(c.filterFile, &skipWords)
if err != nil {
return false
}
// Filter the given words.
for key, words := range skipWords {
switch key {
case "skip_prefixes":
for _, word := range words {
if strings.HasPrefix(strings.ToLower(s), strings.ToLower(word)) {
return false
}
}
case "skip_suffixes":
for _, word := range words {
if strings.HasSuffix(strings.ToLower(s), strings.ToLower(word)) {
return false
}
}
case "skip_words":
for _, word := range words {
if strings.Contains(strings.ToLower(s), strings.ToLower(word)) {
return false
}
}
}
}
return true
}