-
Notifications
You must be signed in to change notification settings - Fork 50
/
closestmatch_test.go
executable file
·199 lines (177 loc) · 5.69 KB
/
closestmatch_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
package closestmatch
import (
"fmt"
"io/ioutil"
"strings"
"testing"
"github.com/schollz/closestmatch/test"
)
func BenchmarkNew(b *testing.B) {
for i := 0; i < b.N; i++ {
New(test.WordsToTest, []int{3})
}
}
func BenchmarkSplitOne(b *testing.B) {
cm := New(test.WordsToTest, []int{3})
searchWord := test.SearchWords[0]
b.ResetTimer()
for i := 0; i < b.N; i++ {
cm.splitWord(searchWord)
}
}
func BenchmarkClosestOne(b *testing.B) {
bText, _ := ioutil.ReadFile("test/books.list")
wordsToTest := strings.Split(strings.ToLower(string(bText)), "\n")
cm := New(wordsToTest, []int{3})
searchWord := test.SearchWords[0]
b.ResetTimer()
for i := 0; i < b.N; i++ {
cm.Closest(searchWord)
}
}
func BenchmarkClosest3(b *testing.B) {
bText, _ := ioutil.ReadFile("test/books.list")
wordsToTest := strings.Split(strings.ToLower(string(bText)), "\n")
cm := New(wordsToTest, []int{3})
searchWord := test.SearchWords[0]
b.ResetTimer()
for i := 0; i < b.N; i++ {
cm.ClosestN(searchWord, 3)
}
}
func BenchmarkClosest30(b *testing.B) {
bText, _ := ioutil.ReadFile("test/books.list")
wordsToTest := strings.Split(strings.ToLower(string(bText)), "\n")
cm := New(wordsToTest, []int{3})
searchWord := test.SearchWords[0]
b.ResetTimer()
for i := 0; i < b.N; i++ {
cm.ClosestN(searchWord, 30)
}
}
func BenchmarkFileLoad(b *testing.B) {
bText, _ := ioutil.ReadFile("test/books.list")
wordsToTest := strings.Split(strings.ToLower(string(bText)), "\n")
cm := New(wordsToTest, []int{3, 4})
cm.Save("test/books.list.cm.gz")
b.ResetTimer()
for i := 0; i < b.N; i++ {
Load("test/books.list.cm.gz")
}
}
func BenchmarkFileSave(b *testing.B) {
bText, _ := ioutil.ReadFile("test/books.list")
wordsToTest := strings.Split(strings.ToLower(string(bText)), "\n")
cm := New(wordsToTest, []int{3, 4})
b.ResetTimer()
for i := 0; i < b.N; i++ {
cm.Save("test/books.list.cm.gz")
}
}
func ExampleMatchingSmall() {
cm := New([]string{"love", "loving", "cat", "kit", "cats"}, []int{4})
fmt.Println(cm.splitWord("love"))
fmt.Println(cm.splitWord("kit"))
fmt.Println(cm.Closest("kit"))
// Output:
// map[love:{}]
// map[kit:{}]
// kit
}
func ExampleMatchingSimple() {
cm := New(test.WordsToTest, []int{3})
for _, searchWord := range test.SearchWords {
fmt.Printf("'%s' matched '%s'\n", searchWord, cm.Closest(searchWord))
}
// Output:
// 'cervantes don quixote' matched 'don quixote by miguel de cervantes saavedra'
// 'mysterious afur at styles by christie' matched 'the mysterious affair at styles by agatha christie'
// 'hard times by charles dickens' matched 'hard times by charles dickens'
// 'complete william shakespeare' matched 'the complete works of william shakespeare by william shakespeare'
// 'war by hg wells' matched 'the war of the worlds by h. g. wells'
}
func ExampleMatchingN() {
cm := New(test.WordsToTest, []int{4})
fmt.Println(cm.ClosestN("war h.g. wells", 3))
// Output:
// [the war of the worlds by h. g. wells the time machine by h. g. wells war and peace by graf leo tolstoy]
}
func ExampleMatchingBigList() {
bText, _ := ioutil.ReadFile("test/books.list")
wordsToTest := strings.Split(strings.ToLower(string(bText)), "\n")
cm := New(wordsToTest, []int{3})
searchWord := "island of a thod mirrors"
fmt.Println(cm.Closest(searchWord))
// Output:
// island of a thousand mirrors by nayomi munaweera
}
func ExampleMatchingCatcher() {
bText, _ := ioutil.ReadFile("test/catcher.txt")
wordsToTest := strings.Split(strings.ToLower(string(bText)), "\n")
cm := New(wordsToTest, []int{5})
searchWord := "catcher in the rye by jd salinger"
for i, match := range cm.ClosestN(searchWord, 3) {
if i == 2 {
fmt.Println(match)
}
}
// Output:
// the catcher in the rye by j.d. salinger
}
func ExampleMatchingPotter() {
bText, _ := ioutil.ReadFile("test/potter.txt")
wordsToTest := strings.Split(strings.ToLower(string(bText)), "\n")
cm := New(wordsToTest, []int{5})
searchWord := "harry potter and the half blood prince by j.k. rowling"
for i, match := range cm.ClosestN(searchWord, 3) {
if i == 1 {
fmt.Println(match)
}
}
// Output:
// harry potter and the order of the phoenix (harry potter, #5, part 1) by j.k. rowling
}
func TestAccuracyBookWords(t *testing.T) {
bText, _ := ioutil.ReadFile("test/books.list")
wordsToTest := strings.Split(strings.ToLower(string(bText)), "\n")
cm := New(wordsToTest, []int{4, 5})
accuracy := cm.AccuracyMutatingWords()
fmt.Printf("Accuracy with mutating words in book list:\t%2.1f%%\n", accuracy)
}
func TestAccuracyBookLetters(t *testing.T) {
bText, _ := ioutil.ReadFile("test/books.list")
wordsToTest := strings.Split(strings.ToLower(string(bText)), "\n")
cm := New(wordsToTest, []int{5})
accuracy := cm.AccuracyMutatingLetters()
fmt.Printf("Accuracy with mutating letters in book list:\t%2.1f%%\n", accuracy)
}
func TestAccuracyDictionaryLetters(t *testing.T) {
bText, _ := ioutil.ReadFile("test/popular.txt")
wordsToTest := strings.Split(strings.ToLower(string(bText)), "\n")
cm := New(wordsToTest, []int{2, 3, 4})
accuracy := cm.AccuracyMutatingWords()
fmt.Printf("Accuracy with mutating letters in dictionary:\t%2.1f%%\n", accuracy)
}
func TestSaveLoad(t *testing.T) {
bText, _ := ioutil.ReadFile("test/books.list")
wordsToTest := strings.Split(strings.ToLower(string(bText)), "\n")
type TestStruct struct {
cm *ClosestMatch
}
tst := new(TestStruct)
tst.cm = New(wordsToTest, []int{5})
err := tst.cm.Save("test.gob")
if err != nil {
t.Error(err)
}
tst2 := new(TestStruct)
tst2.cm, err = Load("test.gob")
if err != nil {
t.Error(err)
}
answer2 := tst2.cm.Closest("war of the worlds by hg wells")
answer1 := tst.cm.Closest("war of the worlds by hg wells")
if answer1 != answer2 {
t.Errorf("Differing answers: '%s' '%s'", answer1, answer2)
}
}