File: closestmatch_test.go

package info (click to toggle)
golang-github-schollz-closestmatch 2.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,316 kB
  • sloc: makefile: 12
file content (149 lines) | stat: -rwxr-xr-x 4,404 bytes parent folder | download | duplicates (2)
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
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 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 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) {
	cm := New(test.WordsToTest, []int{2, 3, 4})
	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")
	}
}