-
Notifications
You must be signed in to change notification settings - Fork 1
/
perceptron_test.go
154 lines (139 loc) · 3.35 KB
/
perceptron_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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"reflect"
"testing"
)
func TestEdgeFor(t *testing.T) {
words := make([]*Word, 0)
words = append(words,
makeWord("ms.", "NNP", 0, -1),
makeWord("hang", "NNP", 1, 0),
makeWord("plays", "VBZ", 2, 1),
)
s := NewState(words)
pair, err := EdgeFor(s, 0, 0)
if err != nil {
t.Error("error should be nil")
}
if !reflect.DeepEqual(pair, []int{0, 1}) {
t.Error("pair shoud be [0, 1] but: ", pair)
}
}
func TestIsValidFalse(t *testing.T) {
words := make([]*Word, 0)
words = append(words,
makeWord("ms.", "NNP", 0, -1),
makeWord("hang", "NNP", 1, 0),
makeWord("plays", "VBZ", 2, 1),
)
s := NewState(words)
goldArcs := make(map[int][]int)
goldArcs[-1] = []int{0}
goldArcs[0] = []int{1}
goldArcs[1] = []int{2}
if IsValid(s, 0, 0, goldArcs) != false {
t.Error("should return false")
}
}
func TestIsValidTrue(t *testing.T) {
words := make([]*Word, 0)
words = append(words,
makeWord("ms.", "NNP", 0, -1),
makeWord("hang", "NNP", 1, 0),
makeWord("plays", "VBZ", 2, 1),
)
s := NewState(words)
arcs := make(map[int]int)
arcs[2] = 1
s.arcs = arcs
goldArcs := make(map[int][]int)
goldArcs[-1] = []int{0}
goldArcs[0] = []int{1}
goldArcs[1] = []int{2}
if IsValid(s, 0, 0, goldArcs) != true {
t.Error("should return true")
}
}
func TestAllowedActions(t *testing.T) {
words := make([]*Word, 0)
words = append(words,
makeRootWord(),
makeWord("ms.", "NNP", 1, 2),
makeWord("hang", "NNP", 2, 3),
makeWord("plays", "VBZ", 3, 0),
makeWord("elianti", "NNP", 4, 3),
makeWord(".", ".", 5, 3),
)
s := NewState(words)
AttachRight(s, 3)
goldArcs := make(map[int][]int)
goldArcs[-1] = []int{0}
goldArcs[0] = []int{1}
goldArcs[1] = []int{2}
if 1 != len(AllowedActions(s, goldArcs)) {
t.Error("length of allowed actions must be 1")
}
}
func TestCandidateActions(t *testing.T) {
words := make([]*Word, 0)
words = append(words,
makeRootWord(),
makeWord("ms.", "NNP", 1, 2),
makeWord("hang", "NNP", 2, 3),
makeWord("plays", "VBZ", 3, 0),
makeWord("elianti", "NNP", 4, 3),
makeWord(".", ".", 5, 3),
)
s := NewState(words)
if 10 != len(CandidateActions(s)) {
t.Error("length of candidate actions must be 10")
}
}
func TestUpdateWeight(t *testing.T) {
model := NewModel()
gold := []int{1, 2, 3}
predict := []int{1, 3, 4}
model.updateWeight(&gold, &predict)
if w := model.weight[1]; w != 0 {
t.Error("weight of '1' must be 0")
}
if w := model.weight[2]; w != 1 {
t.Error("weight of '2' must be 1")
}
if w := model.weight[3]; w != 0 {
t.Error("weight of '3' must be 0")
}
if w := model.weight[4]; w != -1 {
t.Error("weight of '4' must be -1")
}
model.updateWeight(&gold, &predict)
if w := model.cumWeight[1]; w != 0 {
t.Error("cumWeight of '1' must be 0")
}
if w := model.cumWeight[2]; w != 3 {
t.Error("cumWeight of '2' must be 3")
}
if w := model.cumWeight[3]; w != 0 {
t.Error("cumWeight of '3' must be 0")
}
if w := model.cumWeight[4]; w != -3 {
t.Error("cumWeight of '4' must be -3")
}
}
func TestUpdate(t *testing.T) {
words := make([]*Word, 0)
words = append(words,
makeRootWord(),
makeWord("ms.", "NNP", 1, 2),
makeWord("hang", "NNP", 2, 3),
makeWord("plays", "VBZ", 3, 0),
makeWord("elianti", "NNP", 4, 3),
makeWord(".", ".", 5, 3),
)
sent := Sentence{words: words}
model := NewModel()
model.Update(&sent)
if model.count == 1 {
t.Error("count must be greater than 1")
}
}