-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel_test.go
123 lines (92 loc) · 2.51 KB
/
model_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
package fate
import (
"bufio"
"os"
"strings"
"testing"
)
func TestReply(t *testing.T) {
model := NewModel(Config{})
text := "this is a test"
model.Learn(text)
reply := model.Reply(text)
if reply != text {
t.Errorf("Reply(this is a test) => %s, want %s", reply, text)
}
}
// TestConflate ensures an unlearned token isn't in conflate()
// results, a learned one is regardless of how it's stemmed.
func TestConflate(t *testing.T) {
model := NewModel(Config{})
toks := model.conflate(strings.Fields("_"))
if len(toks) != 0 {
t.Errorf("conflate(_) => [%d]token, want [0]token", len(toks))
}
model.Learn("foo bar _ baz")
toks = model.conflate(strings.Fields("_"))
if len(toks) != 1 {
t.Errorf("conflate(_) => [%d]token, want [1]token", len(toks))
}
}
func TestBabble(t *testing.T) {
model := NewModel(Config{})
text := "this is a test"
model.Learn(text)
for i := 0; i < 1000; i++ {
reply := model.Reply("unknown")
if reply != text {
t.Fatalf("Reply(this is a test) => %s, want %s", reply, text)
}
if _, ok := model.tokens.CheckID("unknown"); ok {
t.Fatalf("Reply(\"unknown\") registered token")
}
}
}
func TestDuel(t *testing.T) {
model := NewModel(Config{})
model.Learn("this is a test")
model.Learn("this is another test")
for i := 0; i < 1000; i++ {
reply := model.Reply("this")
if reply != "this is a test" && reply != "this is another test" {
t.Errorf("Reply(this is a test) => %s, want %s", reply, "this is (a|another) test")
}
}
}
func TestEmpty(t *testing.T) {
// Make sure Model doesn't panic when empty.
model := NewModel(Config{})
reply := model.Reply("")
if reply != "" {
t.Errorf("Reply() => %s, want empty string", reply)
}
model.Learn("")
reply = model.Reply("")
if reply != "" {
t.Errorf("Reply() => %s, want empty string", reply)
}
}
func learnFile(m *Model, filename string) error {
file, err := os.Open(filename)
if err != nil {
return err
}
defer file.Close()
s := bufio.NewScanner(file)
for s.Scan() {
m.Learn(s.Text())
}
return s.Err()
}
var quote = "On two occasions I have been asked, 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."
// BenchmarkOverhead checks the constant overhead of learning
// already-learned trigrams.
func BenchmarkOverhead(b *testing.B) {
m := NewModel(Config{})
m.Learn(quote)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.Learn(quote)
}
}