-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
320 lines (277 loc) · 6.42 KB
/
main_test.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package spell
import (
"bytes"
"fmt"
"io/ioutil"
"testing"
txt "github.com/hvlck/txt"
)
type LdResult struct {
one, two string
dist uint8
}
func Testld(t *testing.T) {
results := []LdResult{
{
one: "burn",
two: "bayou",
dist: 4,
},
{
one: "avid",
two: "antidisestablishmentarianism",
dist: 25,
},
}
for _, v := range results {
l := ld(v.one, v.two)
if l != v.dist {
t.Fatalf(v.one, v.two, v.dist)
}
}
}
// rt and ro() https://rosettacode.org/wiki/Levenshtein_distance#Go
func rt(s, t string) int {
d := make([][]int, len(s)+1)
for i := range d {
d[i] = make([]int, len(t)+1)
}
for i := range d {
d[i][0] = i
}
for j := range d[0] {
d[0][j] = j
}
for j := 1; j <= len(t); j++ {
for i := 1; i <= len(s); i++ {
if s[i-1] == t[j-1] {
d[i][j] = d[i-1][j-1]
} else {
min := d[i-1][j]
if d[i][j-1] < min {
min = d[i][j-1]
}
if d[i-1][j-1] < min {
min = d[i-1][j-1]
}
d[i][j] = min + 1
}
}
}
return d[len(s)][len(t)]
}
func ro(s, t string) int {
if s == "" {
return len(t)
}
if t == "" {
return len(s)
}
if s[0] == t[0] {
return ro(s[1:], t[1:])
}
a := ro(s[1:], t[1:])
b := ro(s, t[1:])
c := ro(s[1:], t)
if a > b {
a = b
}
if a > c {
a = c
}
return a + 1
}
const one = "avid"
const two = "antidisestablishmentarianism"
// system specs:
// M1 8-core Macbook Air, 16GB RAM
// benchmark ran on July 9 2022
// latest commit was https://github.com/hvlck/txt/commit/66a24f9329b2d6bd8c9ff496314cc2bcca9aed49
// go test -bench=. -benchmem -benchtime=1x
// BenchmarkLd/rosetta_code_loop-8 1 3333 ns/op 1328 B/op 6 allocs/op
// BenchmarkLd/rosetta_code_slice-8 1 31292 ns/op 0 B/op 0 allocs/op
// BenchmarkLd/txt-8 1 1000 ns/op 16 B/op 1 allocs/op
// BenchmarkSpellcheck-8 1 10183583 ns/op 339536 B/op 84157 allocs/op
// go test -bench=. -benchmem -benchtime=100x
// BenchmarkLd/rosetta_code_loop-8 100 89.16 ns/op 13 B/op 0 allocs/op
// BenchmarkLd/rosetta_code_slice-8 100 317.9 ns/op 0 B/op 0 allocs/op
// BenchmarkLd/txt-8 100 8.330 ns/op 0 B/op 0 allocs/op
// BenchmarkSpellcheck-8 100 100943 ns/op 3393 B/op 841 allocs/op
// go test -bench=. -benchmem -benchtime=1000000x
// BenchmarkLd/rosetta_code_loop-8 1000000 0.008833 ns/op 0 B/op 0 allocs/op
// BenchmarkLd/rosetta_code_slice-8 1000000 0.03225 ns/op 0 B/op 0 allocs/op
// BenchmarkLd/txt-8 1000000 0.0007500 ns/op 0 B/op 0 allocs/op
// BenchmarkSpellcheck-8 1000000 10.12 ns/op 0 B/op 0 allocs/op
func Benchmarkld(b *testing.B) {
b.SetParallelism(1)
b.Run("rosetta code loop", func(b *testing.B) {
rt(one, two)
b.StopTimer()
})
b.Run("rosetta code slice", func(b *testing.B) {
ro(one, two)
b.StopTimer()
})
b.Run("txt", func(b *testing.B) {
ld(one, two)
b.StopTimer()
})
}
func TestWeigh(t *testing.T) {
c := Correction{
Word: "typo",
ld: ld("typo", "testing"),
}
c.weigh("testing")
}
func TestPrefixLength(t *testing.T) {
vals := []uint8{
PrefixLength("tree", "trees"),
PrefixLength("grant", "grace"),
PrefixLength("hammer", "hankering"),
}
answers := []uint8{4, 3, 2}
for i, v := range vals {
if v != answers[i] {
t.Fatal(v, answers[i], i)
}
}
}
func TestMax(t *testing.T) {
}
func TestAbs(t *testing.T) {
nth := abs(10 - 23)
th := abs(10 + 3)
if nth != 13 || th != 13 {
t.Fail()
}
}
func TestKeyProximity(t *testing.T) {
vals := []uint8{
KeyProximity('r', 't'),
KeyProximity('s', 'w'),
KeyProximity('a', 'w'),
KeyProximity('l', 'p'),
KeyProximity('v', 'p'),
KeyProximity('1', '.'),
KeyProximity('b', 'w'),
}
answers := []uint8{1, 1, 1, 1, 6, 7, 5}
for i, v := range vals {
if v != answers[i] {
t.Fatalf("expected %v, got %v", answers[i], v)
}
}
}
func BenchmarkKeyProximity(b *testing.B) {
vals := []uint8{
KeyProximity('r', 't'),
KeyProximity('s', 'w'),
KeyProximity('a', 'w'),
KeyProximity('l', 'p'),
KeyProximity('v', 'p'),
KeyProximity('1', '.'),
}
answers := []uint8{1, 1, 1, 1, 6, 7}
for i, v := range vals {
if v != answers[i] {
b.Fatalf("expected %v, got %v", answers[i], v)
}
}
}
func TestSearch_Lev(t *testing.T) {
}
func TestPartialMatch(t *testing.T) {
if dErr != nil {
t.Fatal(dErr)
}
matches := PartialMatch(d.trie, "tesk", 3, 15)
if len(matches) != 15 {
t.Fail()
}
// adopted from Peter Norvig's spelling correcter test suite
// http://norvig.com/spell-correct.html
results := map[string]string{
"speling": "spelling",
"korrectud": "corrected",
"bycycle": "bicycle",
"inconvient": "inconvenient",
"arrainged": "arranged",
"peotry": "poetry",
"peotryy": "poetry",
"word": "word",
}
for i, v := range results {
r := PartialMatch(d.trie, i, 2, 10)
if r != nil && len(r) > 0 {
if r[len(r)-1].Word != v {
t.Fatalf("expected %v, got %v (ld: %v)", v, r[len(r)-1], ld(v, i))
for _, tt := range r {
if tt.Word == v {
fmt.Println(tt)
}
}
}
}
}
}
func BenchmarkPartialMatch(b *testing.B) {
b.SetParallelism(1)
b.StopTimer()
if dErr != nil {
b.Fatal(dErr)
}
b.StartTimer()
matches := PartialMatch(d.trie, "tesk", 3, 15)
if len(matches) != 15 {
b.Fail()
}
}
func TestSpellcheck(t *testing.T) {
results, err := Correct("wat", 3)
fmt.Println(results)
if err != nil {
t.Fail()
}
}
var d, dErr = loadTrie()
type Dictionary struct {
trie *txt.Node
}
func loadTrie() (Dictionary, error) {
b, err := ioutil.ReadFile("./data/final.txt")
if err != nil {
return Dictionary{}, err
}
t := txt.NewTrie()
lines := bytes.Split(b, []byte("\n"))
for _, v := range lines {
if len(v) > 0 {
r := bytes.Split(v, []byte(","))
w := string(r[0])
t.Insert(w, r[1])
if err != nil {
panic(err)
}
}
}
return Dictionary{trie: t}, nil
}
func BenchmarkTrieSpellcheck(b *testing.B) {
b.SetParallelism(1)
if dErr != nil {
b.Fatal(dErr)
}
f := PartialMatch(d.trie, "wat", 5, 15)
if len(f) != 15 {
b.Fail()
}
}
func BenchmarkSpellcheck(b *testing.B) {
b.SetParallelism(1)
_, err := Correct("wat", 3)
b.StopTimer()
if err != nil {
b.Fail()
}
}