-
Notifications
You must be signed in to change notification settings - Fork 1
/
state_test.go
112 lines (97 loc) · 2.66 KB
/
state_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
package fgsdmm
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestCluster(t *testing.T) {
Convey("Given a cluster and a document", t, func() {
c := &Cluster{
TknCts: make(map[int]int),
NDoc: 0,
NTkn: 0,
}
d1 := &Document{
TknIDs: []int{0, 1, 2},
TknCts: []int{1, 2, 1},
NTkn: 4,
}
d2 := &Document{
TknIDs: []int{1, 2, 3},
TknCts: []int{3, 2, 1},
NTkn: 6,
}
s := &State{
Clusters: []*Cluster{c},
Labels: []int{0, 0},
}
Convey("When the document is added to the cluster", func() {
s.clusterAdd(0, 0, d1)
Convey("The number of documents in the cluster should be 1.", func() {
So(c.NDoc, ShouldEqual, 1)
})
Convey("The cluster's number of tokens should equal the document's.", func() {
So(c.NTkn, ShouldEqual, d1.NTkn)
})
Convey("The cluster token counts should equal the document's.", func() {
for i, tknID := range d1.TknIDs {
So(c.TknCts[tknID], ShouldEqual, d1.TknCts[i])
}
})
})
Convey("When two documents are added to the cluster", func() {
s.clusterAdd(0, 0, d1)
s.clusterAdd(0, 1, d2)
Convey("The number of documents in the cluster should be two.", func() {
So(c.NDoc, ShouldEqual, 2)
})
Convey("The number of tokens in the cluster should equal the document sum", func() {
So(c.NTkn, ShouldEqual, d1.NTkn+d2.NTkn)
})
Convey("and when the second document is removed", func() {
clusterRemove(c, d2)
Convey("The number of documents in the cluster should be 1.", func() {
So(c.NDoc, ShouldEqual, 1)
})
Convey("The cluster's number of tokens should equal the first document's.", func() {
So(c.NTkn, ShouldEqual, d1.NTkn)
})
Convey("The cluster token counts should equal the first document's.", func() {
for i, tknID := range d1.TknIDs {
So(c.TknCts[tknID], ShouldEqual, d1.TknCts[i])
}
})
})
})
})
}
func TestScoreNonEmpty(t *testing.T) {
Convey("Given a model, cluster and a document", t, func() {
model := NewFGSDMM(&HyperParams{
KMax: 5,
Alpha: 0.1,
Beta: 0.1,
})
model.KNon = 2
model.Corpus = Corpus{
V: 5,
}
c := &Cluster{
TknCts: map[int]int{0: 2, 1: 2, 2: 1},
NDoc: 2,
NTkn: 5,
}
d := &Document{
TknIDs: []int{0, 1, 2},
TknCts: []int{1, 1, 1},
NTkn: 3,
}
Convey("The score for the document in the cluster should be correct.", func() {
score := model.scoreNonEmpty(c, d)
So(score, ShouldAlmostEqual, 0.03799384615384616, 0.0001)
})
Convey("The score for the document in an empty cluster should be correct", func() {
score := model.scoreEmpty(d)
So(score, ShouldAlmostEqual, 0.00016000000000000007)
})
})
}