-
Notifications
You must be signed in to change notification settings - Fork 0
/
blacklist.go
115 lines (95 loc) · 1.98 KB
/
blacklist.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
package estruturadados
import (
"sort"
"strings"
)
// Node .
type Node struct {
Value string
Children map[string]*Node
}
func (n *Node) isBlacklisted(termParts map[string]bool) bool {
if n == nil {
panic("como isso foi acontecer, o que fiz de errado?")
}
if len(n.Children) == 0 {
return true
}
if _, ok := termParts[n.Value]; !ok {
return false
}
for _, v := range n.Children {
if v.isBlacklisted(termParts) {
return true
}
}
return false
}
func (n *Node) addWords(termParts []string) {
if len(termParts) == 0 {
return
}
value := termParts[0]
if _, ok := n.Children[value]; !ok {
n.Children[value] = &Node{
Value: value,
Children: make(map[string]*Node),
}
}
n.Children[value].addWords(termParts[1:])
}
// NewBlacklist returns a initialized blacklist.
func NewBlacklist() *Blacklist {
return &Blacklist{
tree: make(map[string]*Node),
}
}
// Blacklist .
type Blacklist struct {
tree map[string]*Node
}
func (b *Blacklist) addWords(termParts []string) {
value := termParts[0]
if _, ok := b.tree[value]; !ok {
b.tree[value] = &Node{
Value: value,
Children: make(map[string]*Node),
}
}
b.tree[value].addWords(termParts[1:])
}
// AddTerm .
func (b *Blacklist) AddTerm(v string) {
if b.IsBlacklisted(v) {
return
}
term := Slugfy(v)
termParts := strings.Split(term, "-")
sort.Strings(termParts)
b.addWords(termParts)
// strings.Split()
// add novo termo na blacklist
}
// IsBlacklisted .
func (b *Blacklist) IsBlacklisted(v string) bool {
term := Slugfy(v)
termParts := strings.Split(term, "-")
m := make(map[string]bool)
for _, v := range termParts {
m[v] = true
}
for _, v := range termParts {
if n, ok := b.tree[v]; ok {
if n.isBlacklisted(m) {
return true
}
}
}
return false
}
// ContentOnBlackList returns whether a term is blacklisted or not.
func ContentOnBlackList(searchBlacklist []string, content string) bool {
contentWords := strings.Split(Slugfy(content), "-")
_ = contentWords
return false
}