-
Notifications
You must be signed in to change notification settings - Fork 15
/
search.go
181 lines (161 loc) · 4.26 KB
/
search.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// SPDX-License-Identifier: MIT OR Unlicense
package main
import (
str "github.com/boyter/go-string"
"strings"
)
const (
Default = iota
Quoted
Regex
Negated
Fuzzy1
Fuzzy2
)
type searchParams struct {
Term string
Type int64
}
// ParseQuery is a cheap and nasty parser. Needs to be reworked
// to provide real boolean logic with AND OR NOT
// but does enough for now
func ParseQuery(args []string) []searchParams {
cleanArgs := []string{}
// Clean the arguments to avoid redundant spaces and the like
for _, arg := range args {
cleanArgs = append(cleanArgs, strings.TrimSpace(arg))
}
params := []searchParams{}
startIndex := 0
mode := Default
// With the arguments cleaned up parse out what we need
// note that this is very ugly
for ind, arg := range cleanArgs {
if strings.HasPrefix(arg, `"`) {
if len(arg) != 1 {
if strings.HasSuffix(arg, `"`) {
params = append(params, searchParams{
Term: arg[1 : len(arg)-1],
Type: Quoted,
})
} else {
mode = Quoted
startIndex = ind
}
}
} else if mode == Quoted && strings.HasSuffix(arg, `"`) {
t := strings.Join(cleanArgs[startIndex:ind+1], " ")
params = append(params, searchParams{
Term: t[1 : len(t)-1],
Type: Quoted,
})
mode = Default
} else if strings.HasPrefix(arg, `/`) {
if len(arg) != 1 {
// If we end with / not prefixed with a \ we are done
if strings.HasSuffix(arg, `/`) {
// If the term is // don't treat it as a regex treat it as a search for //
if arg == "//" {
params = append(params, searchParams{
Term: "//",
Type: Default,
})
} else {
params = append(params, searchParams{
Term: arg[1 : len(arg)-1],
Type: Regex,
})
}
} else {
mode = Regex
startIndex = ind
}
}
} else if mode == Regex && strings.HasSuffix(arg, `/`) {
t := strings.Join(cleanArgs[startIndex:ind+1], " ")
params = append(params, searchParams{
Term: t[1 : len(t)-1],
Type: Regex,
})
mode = Default
} else if arg == "NOT" {
// If we start with NOT we cannot negate so ignore
if ind != 0 {
params = append(params, searchParams{
Term: arg,
Type: Negated,
})
}
} else if strings.HasSuffix(arg, "~1") {
params = append(params, searchParams{
Term: strings.TrimRight(arg, "~1"),
Type: Fuzzy1,
})
} else if strings.HasSuffix(arg, "~2") {
params = append(params, searchParams{
Term: strings.TrimRight(arg, "~2"),
Type: Fuzzy2,
})
} else {
params = append(params, searchParams{
Term: arg,
Type: Default,
})
}
}
// If the user didn't end properly that's ok lets do it for them
if mode == Regex {
t := strings.Join(cleanArgs[startIndex:], " ")
params = append(params, searchParams{
Term: t[1:],
Type: Regex,
})
}
if mode == Quoted {
t := strings.Join(cleanArgs[startIndex:], " ")
params = append(params, searchParams{
Term: t[1:],
Type: Quoted,
})
}
return params
}
const letterDigitFuzzyBytes = `abcdefghijklmnopqrstuvwxyz1234567890`
// Takes in a term and returns a slice of them which contains all the
// fuzzy versions of that str with things such as mis-spellings
// somewhat based on https://norvig.com/spell-correct.html
func makeFuzzyDistanceOne(term string) []string {
vals := []string{term}
if len(term) <= 2 {
return vals
}
// Delete letters so turn "test" into "est" "tst" "tet"
for i := 0; i < len(term); i++ {
vals = append(vals, term[:i]+term[i+1:])
}
// Replace a letter or digit which effectively does transpose for us
for i := 0; i < len(term); i++ {
for _, b := range letterDigitFuzzyBytes {
vals = append(vals, term[:i]+string(b)+term[i+1:])
}
}
// Insert a letter or digit
for i := 0; i < len(term); i++ {
for _, b := range letterDigitFuzzyBytes {
vals = append(vals, term[:i]+string(b)+term[i:])
}
}
return str.RemoveStringDuplicates(vals)
}
// Similar to fuzzy 1 but in this case we add letters
// to make the distance larger
func makeFuzzyDistanceTwo(term string) []string {
vals := makeFuzzyDistanceOne(term)
// Maybe they forgot to type a letter? Try adding one
for i := 0; i < len(term)+1; i++ {
for _, b := range letterDigitFuzzyBytes {
vals = append(vals, term[:i]+string(b)+term[i:])
}
}
return str.RemoveStringDuplicates(vals)
}