-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_test.go
234 lines (206 loc) · 7.08 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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package gopostagger
import (
"fmt"
"io"
"net/http"
"os"
"testing"
)
const exampleCorpus string = "./example_corpus"
const exampleModel string = "./example_model"
func downloadCorpus(t *testing.T) bool {
if fd, e := os.Create(exampleCorpus); e != nil {
t.Errorf("Expected nil, got Error:'%s'", e)
} else {
defer fd.Close()
if r, e := http.Get("https://raw.githubusercontent.com/lucasmenendez/gopostagger/master/es"); e != nil {
t.Errorf("Expected nil, got Error:'%s'", e)
} else {
defer r.Body.Close()
if _, e := io.Copy(fd, r.Body); e != nil {
t.Errorf("Expected nil, got Error:'%s'", e)
} else {
return true
}
}
}
return false
}
func deleteCorpus(t *testing.T) {
if e := os.Remove(exampleCorpus); e != nil {
t.Errorf("Expected nil, got Error:'%s'", e)
} else if e = os.RemoveAll(exampleModel); e != nil {
t.Errorf("Expected nil, got Error:'%s'", e)
}
}
func TestGetLink(t *testing.T) {
var ls links = []*link{
{"AA", "BB", 0, 0},
}
if _, exists := ls.getLink("AA", "CC"); exists {
t.Error("Expected false, got true")
return
}
if r, exists := ls.getLink("AA", "BB"); !exists {
t.Error("Expected true, got false")
return
} else if e := ls[0]; r.current != e.current || r.previous != e.previous {
t.Error("Expected true, got false")
return
}
}
func TestTrainAndStore(t *testing.T) {
if _, e := Train("fail"); e == nil {
t.Error("Expected error, got nil")
return
}
if ok := downloadCorpus(t); !ok {
t.Error("Expected true, got false")
} else if m, e := Train(exampleCorpus); e != nil {
t.Fatalf("Expected nil, got %s", e.Error())
} else if e := m.Store(exampleModel); e != nil {
t.Errorf("Expected nil, got %s", e.Error())
}
}
func TestLoadTransitions(t *testing.T) {
var (
tp string = fmt.Sprintf("%s/transitions", exampleModel)
m *Model = &Model{}
)
if err := m.loadTransitions("fail"); err == nil {
t.Error("Expected error, got nil")
return
} else if err = m.loadTransitions(tp); err != nil {
t.Errorf("Expected nil, got '%s'", err.Error())
return
} else {
var expected links = links{
{previous: "<s>", current: "DET", weight: 0.3690260133091349},
{previous: "DET", current: "NOUN", weight: 0.7237326536391957},
{previous: "NOUN", current: "PUNCT", weight: 0.21732791835884618},
{previous: "PUNCT", current: "ADP", weight: 0.11075697211155379},
{previous: "ADP", current: "ADJ", weight: 0.020774647887323944},
{previous: "ADJ", current: "NOUN", weight: 0.215375918598078},
{previous: "NOUN", current: "ADP", weight: 0.3826929084661043},
{previous: "ADP", current: "SCONJ", weight: 0.01784037558685446},
{previous: "SCONJ", current: "VERB", weight: 0.15371024734982333},
{previous: "VERB", current: "AUX", weight: 0.008492569002123142},
}
for _, tm := range m.transitions[:10] {
var in bool = false
for _, te := range expected {
in = in || (tm.previous == te.previous && tm.current == te.current && tm.weight == te.weight)
}
if !in {
t.Error("Expected true, got false")
return
}
}
}
}
func TestLoadEmissions(t *testing.T) {
var (
ep string = fmt.Sprintf("%s/emissions", exampleModel)
m *Model = &Model{}
)
if err := m.loadEmissions("fail"); err == nil {
t.Error("Expected error, got nil")
return
} else if err = m.loadEmissions(ep); err != nil {
t.Errorf("Expected nil, got '%s'", err.Error())
return
} else {
var expected links = links{
{previous: "DET", current: "El", weight: 0.04290569243840272},
{previous: "NOUN", current: "gobernante", weight: 0.0002082682495053629},
{previous: "PUNCT", current: ",", weight: 0.45322709163346614},
{previous: "ADP", current: "con", weight: 0.05},
{previous: "ADJ", current: "ganada", weight: 0.0002826455624646693},
{previous: "NOUN", current: "fama", weight: 0.00010413412475268145},
{previous: "ADP", current: "desde", weight: 0.007863849765258215},
{previous: "SCONJ", current: "que", weight: 0.6351590106007067},
{previous: "VERB", current: "llegó", weight: 0.001887237556027365},
{previous: "AUX", current: "hace", weight: 0.013861386138613862},
}
for _, em := range m.emissions[:10] {
var in bool = false
for _, ee := range expected {
in = in || (em.previous == ee.previous && em.current == ee.current && em.weight == ee.weight)
}
if !in {
t.Error("Expected true, got false")
return
}
}
}
}
func TestLoadModel(t *testing.T) {
if _, err := LoadModel("dslknfdslkngsd"); err == nil {
t.Error("Expected error, got nil")
} else if m, err := LoadModel(exampleModel); err != nil {
t.Errorf("Expected nil, got %s", err.Error())
} else {
var expt links = links{
{previous: "<s>", current: "DET", weight: 0.3690260133091349},
{previous: "DET", current: "NOUN", weight: 0.7237326536391957},
{previous: "NOUN", current: "PUNCT", weight: 0.21732791835884618},
{previous: "PUNCT", current: "ADP", weight: 0.11075697211155379},
{previous: "ADP", current: "ADJ", weight: 0.020774647887323944},
{previous: "ADJ", current: "NOUN", weight: 0.215375918598078},
{previous: "NOUN", current: "ADP", weight: 0.3826929084661043},
{previous: "ADP", current: "SCONJ", weight: 0.01784037558685446},
{previous: "SCONJ", current: "VERB", weight: 0.15371024734982333},
{previous: "VERB", current: "AUX", weight: 0.008492569002123142},
}
for _, tm := range m.transitions[:10] {
var in bool = false
for _, te := range expt {
in = in || (tm.previous == te.previous && tm.current == te.current && tm.weight == te.weight)
}
if !in {
t.Error("Expected true, got false")
return
}
}
var expe links = links{
{previous: "DET", current: "El", weight: 0.04290569243840272},
{previous: "NOUN", current: "gobernante", weight: 0.0002082682495053629},
{previous: "PUNCT", current: ",", weight: 0.45322709163346614},
{previous: "ADP", current: "con", weight: 0.05},
{previous: "ADJ", current: "ganada", weight: 0.0002826455624646693},
{previous: "NOUN", current: "fama", weight: 0.00010413412475268145},
{previous: "ADP", current: "desde", weight: 0.007863849765258215},
{previous: "SCONJ", current: "que", weight: 0.6351590106007067},
{previous: "VERB", current: "llegó", weight: 0.001887237556027365},
{previous: "AUX", current: "hace", weight: 0.013861386138613862},
}
for _, em := range m.emissions[:10] {
var in bool = false
for _, ee := range expe {
in = in || (em.previous == ee.previous && em.current == ee.current && em.weight == ee.weight)
}
if !in {
t.Error("Expected true, got false")
return
}
}
}
}
func TestProbs(t *testing.T) {
defer deleteCorpus(t)
if m, err := LoadModel(exampleModel); err != nil {
t.Errorf("Expected nil, got %s", err.Error())
} else {
if ps, sg := m.probs("fdsfsf", ""); len(ps) > 0 {
t.Errorf("Expecetd 0, got %d", len(ps))
} else if sg != "<s>?" {
t.Errorf("Expected \"<s>?\", got \"%s\"", sg)
} else if ps, sg := m.probs("El", "<s>"); len(ps) == 0 {
t.Error("Expecetd >0, got 0")
} else if ps["DET"] != 0.04290569243840272 {
t.Errorf("Expected \"DET\":0.04290569243840272, got \"%+v\"", ps)
} else if sg != "" {
t.Errorf("Expected \"\", got \"%s\"", sg)
}
}
}