-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathutils.go
More file actions
74 lines (63 loc) · 1.39 KB
/
utils.go
File metadata and controls
74 lines (63 loc) · 1.39 KB
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
package main
import (
"regexp"
"strings"
)
type Empty struct{}
type Set struct {
Store map[string]Empty
}
func NewSet() *Set {
return &Set{Store: make(map[string]Empty)}
}
func (s Set) List() []string {
var result []string
for k := range s.Store {
result = append(result, k)
}
return result
}
func (s Set) Set(key string) {
s.Store[key] = Empty{}
}
func (s Set) Del(key string) {
if s.Exist(key) {
delete(s.Store, key)
}
}
func (s Set) Exist(key string) bool {
_, exist := s.Store[key]
return exist
}
func ToLower(text string) string {
match, _ := regexp.MatchString(`^[A-Za-z]+$`, text)
if match {
return strings.ToLower(text)
}
return text
}
func TrimHtml(src string) string {
re, _ := regexp.Compile("\\<[\\S\\s]+?\\>")
src = re.ReplaceAllStringFunc(src, strings.ToLower)
re, _ = regexp.Compile("\\<style[\\S\\s]+?\\</style\\>")
src = re.ReplaceAllString(src, "")
re, _ = regexp.Compile("\\<script[\\S\\s]+?\\</script\\>")
src = re.ReplaceAllString(src, "")
re, _ = regexp.Compile("\\<[\\S\\s]+?\\>")
src = re.ReplaceAllString(src, "\n")
re, _ = regexp.Compile("\\s{2,}")
src = re.ReplaceAllString(src, "\n")
return strings.TrimSpace(src)
}
func Search(text string) []string {
var result []string
for _, v := range Seg.Cut(text, true) {
var key = ToLower(v)
doc, exist := Store[key]
if !exist {
continue
}
result = append(result, doc.List()...)
}
return result
}