forked from eaigner/shield
-
Notifications
You must be signed in to change notification settings - Fork 1
/
shield_test.go
140 lines (126 loc) · 2.6 KB
/
shield_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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package shield
import (
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"testing"
)
func readDataSet(dataFile, labelFile string, t *testing.T) []string {
d, err := ioutil.ReadFile("testdata/" + dataFile)
if err != nil {
t.Fatal(err)
}
l, err := ioutil.ReadFile("testdata/" + labelFile)
if err != nil {
t.Fatal(err)
}
dl := strings.Split(string(d), "\n")
ll := strings.Split(string(l), "\n")
x, y := len(dl), len(ll)
if x != y {
t.Fatal(x, y)
}
var a []string
for i, v := range ll {
k := strings.TrimSpace(v)
if k != "" {
a = append(a, fmt.Sprintf("%s %s", k, strings.TrimSpace(dl[i])))
}
}
return a
}
func newShield() Shield {
logger := log.New(os.Stderr, "", log.LstdFlags)
store := NewRedisStore("127.0.0.1:6379", "", logger, "redis")
tokenizer := NewEnglishTokenizer()
sh := New(tokenizer, store)
err := sh.Reset()
if err != nil {
panic(err)
}
return sh
}
func TestLearn(t *testing.T) {
sh := newShield()
testData := readDataSet("testdata.txt", "testlabels.txt", t)
trainData := readDataSet("traindata.txt", "trainlabels.txt", t)
// Run on test sets
sets := []Set{}
for _, v := range trainData {
c := strings.SplitN(v, " ", 2)
sets = append(sets, Set{
Class: c[0],
Text: c[1],
})
}
sh.BulkLearn(sets)
var hit, miss int
for _, v2 := range testData {
c := strings.SplitN(v2, " ", 2)
k, v := c[0], c[1]
clz, err := sh.Classify(v)
if err != nil {
t.Fatal(err, "key:", k, "value:", v)
}
if clz != k {
miss++
} else {
hit++
}
}
// Test hit/miss ratio
// TODO: Tweak this, where possible
minHitRatio := 0.73
hitRatio := (float64(hit) / float64(hit+miss))
if hitRatio < minHitRatio {
t.Fatalf("%d hits, %d misses (expected ratio %.2f, is %.2f)", hit, miss, minHitRatio, hitRatio)
}
}
func TestDecrement(t *testing.T) {
sh := newShield()
sh.Learn("a", "hello")
sh.Learn("a", "sunshine")
sh.Learn("a", "tree")
sh.Learn("a", "water")
sh.Learn("b", "iamb!")
sh.Forget("a", "hello tree")
sh.Forget("a", "hello")
s := sh.(*shield)
m, err := s.store.ClassWordCounts("a", []string{
"hello",
"sunshine",
"tree",
"water",
})
if err != nil {
t.Fatal(err)
}
if r := fmt.Sprintf("%v", m); r != "map[hello:0 sunshine:1 tree:0 water:1]" {
t.Fatal(r)
}
m2, err := s.store.ClassWordCounts("b", []string{
"hello",
"iamb!",
})
if err != nil {
t.Fatal(err)
}
if r := fmt.Sprintf("%v", m2); r != "map[hello:0 iamb!:0]" {
t.Fatal(r)
}
wc, err := s.store.TotalClassWordCounts()
if err != nil {
t.Fatal(err)
}
if x := len(wc); x != 2 {
t.Fatal(x)
}
if x := wc["a"]; x != 2 {
t.Fatal(x)
}
if x := wc["b"]; x != 1 {
t.Fatal(x)
}
}