forked from schollz/closestmatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
closestmatch_test.go
executable file
·116 lines (103 loc) · 2.9 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
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) {
cm := New(test.WordsToTest, []int{3})
searchWord := test.SearchWords[0]
b.ResetTimer()
for i := 0; i < b.N; i++ {
cm.Closest(searchWord)
}
}
func BenchmarkClosest3(b *testing.B) {
cm := New(test.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) {
cm := New(test.WordsToTest, []int{3})
searchWord := test.SearchWords[0]
b.ResetTimer()
for i := 0; i < b.N; i++ {
cm.ClosestN(searchWord, 30)
}
}
func BenchmarkLargeFile(b *testing.B) {
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"
b.ResetTimer()
for i := 0; i < b.N; i++ {
cm.Closest(searchWord)
}
}
func ExampleMatching() {
cm := New(test.WordsToTest, []int{1, 2, 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'
// 'charles dickens' matched 'hard times by charles dickens'
// 'william shakespeare' matched 'the tragedy of romeo and juliet by william shakespeare'
// 'war by hg wells' matched 'the war of the worlds by h. g. wells'
}
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 ExampleMatchingN() {
cm := New(test.WordsToTest, []int{1, 2, 3})
fmt.Println(cm.ClosestN("war by hg wells", 3))
// Output:
// [the war of the worlds by h. g. wells the time machine by h. g. wells the iliad by homer]
}
func TestAccuray(t *testing.T) {
cm := New(test.WordsToTest, []int{3})
accuracy := cm.Accuracy()
if accuracy < 98 {
t.Errorf("Accuracy should be higher than %2.1f", accuracy)
}
}
func TestSaveLoad(t *testing.T) {
cm := New(test.WordsToTest, []int{1, 2, 3})
err := cm.Save("test.txt")
if err != nil {
t.Error(err)
}
cm2, err := Load("test.txt")
if err != nil {
t.Error(err)
}
if cm2.Closest("war by hg wells") != cm.Closest("war by hg wells") {
t.Errorf("Differing answers")
}
}