-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmodel_test.go
79 lines (62 loc) · 1.3 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
package randtxt
import (
"math/rand"
"reflect"
"testing"
"time"
)
func TestModel(t *testing.T) {
chain, close := testChain(t, "testfiles/ion/trigram.mkv")
defer close()
model, err := NewModel(chain, "")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
var last []TagProbability
for i := 0; i < 100; i++ {
next, err := model.NextTags()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
sum := 0.0
for _, tp := range next {
sum += tp.Probability
}
if int(sum+0.5) != 1 {
t.Errorf("got %f, want %f", sum, 1.0)
}
if reflect.DeepEqual(last, next) {
t.Errorf("got duplicate next tags")
}
last = next
model.Step()
}
}
func TestUnigramModel(t *testing.T) {
rand.Seed(time.Now().Unix())
chain, close := testChain(t, "testfiles/ion/unigram.mkv")
defer close()
model, err := NewModel(chain, "")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
var last []TagProbability
for i := 0; i < 10; i++ {
next, err := model.NextTags()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
sum := 0.0
for _, tp := range next {
sum += tp.Probability
}
if int(sum+0.5) != 1 {
t.Errorf("got %f, want %f", sum, 1.0)
}
if reflect.DeepEqual(last, next) {
t.Fatalf("got duplicate next tags")
}
last = next
model.Step()
}
}