-
Notifications
You must be signed in to change notification settings - Fork 0
/
fss.go
150 lines (127 loc) · 3.06 KB
/
fss.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
package fss
import (
"hash"
"io"
"strings"
"unicode/utf8"
"github.com/OneOfOne/xxhash/native"
)
type Fss struct {
count int
hf hash.Hash32
ht map[uint32][]int
d int
fi map[int]string
}
func NewFss(d int) *Fss {
f := &Fss{
hf: xxhash.New32(),
ht: make(map[uint32][]int),
d: d,
count: 0,
fi: make(map[int]string),
}
return f
}
func (f *Fss) Insert(w string) {
w = strings.ToLower(w)
f.count++
id := f.count
f.fi[id] = w
permutations := getpermutations(w, f.d)
for _, p := range permutations {
hash := f.gethash(p)
f.ht[hash] = append(f.ht[hash], id)
}
}
type QueryResult struct {
S string
Rank float32
}
func (f *Fss) Search(q string) []QueryResult {
q = strings.ToLower(q)
results := make([]QueryResult, 0)
permutations := getpermutations(q, f.d)
for _, p := range permutations {
hash := f.gethash(p)
vals, ok := f.ht[hash]
if ok {
for _, val := range vals {
str, _ := f.fi[val]
qr := QueryResult{
S: str,
}
results = append(results, qr)
}
}
}
return results
}
func (f *Fss) gethash(w string) uint32 {
f.hf.Reset()
r := strings.NewReader(w)
io.Copy(f.hf, r)
hash := f.hf.Sum32()
return hash
}
type loc struct {
index int
width int
}
//getpermutations takes a word and an edit distance and returns all strings in the deletion neighborhood
//specified by the edit distance
func getpermutations(word string, del int) []string {
locs := make([]loc, 0)
for i, w := 0, 0; i < len(word); i += w {
_, width := utf8.DecodeRuneInString(word[i:])
locs = append(locs, loc{
index: i,
width: width,
})
w = width
}
perms := permutations(locs, del)
results := stringsFromPermutations(word, locs, perms)
return results
}
func permutations(locs []loc, del int) [][]loc {
results := make([][]loc, 0)
for d := 1; d <= del; d++ {
results = append(results, permutationsR(locs, len(locs)-d)...)
}
return results
}
//permutationsR takes in a length and a deletion distance. It then recursively
//calls itself, assembling every combination of len(loc) choose length
func permutationsR(locs []loc, length int) [][]loc {
results := make([][]loc, 0)
for i, cur := range locs {
if length == 1 {
results = append(results, []loc{cur})
} else {
items := permutationsR(locs[i+1:], length-1)
for _, next := range items {
tmp := make([]loc, 0)
tmp = append(tmp, cur)
tmp = append(tmp, next...)
results = append(results, tmp)
}
}
}
return results
}
//stringsFromPermutations takes an original string, a list of its rune start indices and widths, and
//an array of inclusions (perm). It goes through each deletion item and builds a string from the original.
//The return value is an array of strings that make up each version of the original string with associated deletions.
func stringsFromPermutations(w string, locs []loc, perms [][]loc) []string {
results := make([]string, 0)
results = append(results, w)
for _, v := range perms {
s := ""
for _, l := range v {
s += w[l.index : l.index+l.width]
}
results = append(results, s)
}
return results
}