forked from FabulousFabs/GoodReadsCrawler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeywordHandler.go
executable file
·89 lines (71 loc) · 2.07 KB
/
KeywordHandler.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
package main
import (
"regexp"
"strings"
)
type KeywordHandler struct {
keywords []string
}
func (keywordhandler *KeywordHandler) IncludeSlice(keywords []string) {
for _, keyword := range keywords {
keywordhandler.Include(keyword)
}
}
func (keywordhandler *KeywordHandler) Slice(keyword string) []string {
re := regexp.MustCompile(`([^\-:]+)[\-:]+(.+)`)
matches := re.FindAllSubmatch([]byte(keyword), -1)
var temp []string
if len(matches) > 0 {
for index, bytes := range matches[0] {
if index == 0 {
continue
}
temp = append(temp, string(bytes[:len(bytes)]))
}
}
return temp
}
func (keywordhandler *KeywordHandler) Sanitize(keyword string) string {
// sanitize for AMS
re := regexp.MustCompile(`[^a-zA-Z0-9\' ]+`)
keyword = re.ReplaceAllString(keyword, "")
re = regexp.MustCompile(`[ ]+`)
keyword = re.ReplaceAllString(keyword, " ")
keyword = strings.TrimSpace(keyword)
// shorten for AMS
if len(keyword) > 60 {
keyword = keyword[:60]
}
return keyword
}
func (keywordhandler *KeywordHandler) Add(keyword string) {
keyword = keywordhandler.Sanitize(keyword)
keywordhandler.keywords = append(keywordhandler.keywords, keyword)
}
func (keywordhandler *KeywordHandler) Include(keyword string) {
kws := keywordhandler.Slice(keyword)
keywordhandler.Add(keyword)
if len(kws) >= 1 {
keywordhandler.IncludeSlice(kws)
}
}
func (keywordhandler *KeywordHandler) Includes(keyword string) bool {
for _, kw := range keywordhandler.keywords {
if kw == keyword {
return true
}
}
return false
}
func (keywordhandler *KeywordHandler) Collapse() []string {
enc := map[string]bool{}
kws := []string{}
for _, keyword := range keywordhandler.keywords {
if enc[keyword] == false {
enc[keyword] = true
kws = append(kws, keyword)
}
}
keywordhandler.keywords = kws
return kws
}