-
-
Notifications
You must be signed in to change notification settings - Fork 53
/
lattice_test.go
370 lines (336 loc) · 9.66 KB
/
lattice_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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package lattice
import (
"bytes"
"testing"
"unicode/utf8"
"github.com/ikawaha/kagome-dict/dict"
"github.com/ikawaha/kagome-dict/ipa"
)
func Test_LatticeBuildEmptyInput(t *testing.T) {
la := New(ipa.Dict(), nil)
if la == nil {
t.Fatal("lattice new failed")
}
defer la.Free()
inp := ""
la.Build(inp)
if la.Input != inp {
t.Errorf("got %v, expected %v", la.Input, inp)
}
boseos := node{ID: -1}
if len(la.list) != 2 {
t.Errorf("lattice initialize error: got %v, expected has 2 eos/bos nodes", la.list)
} else if len(la.list[0]) != 1 || *la.list[0][0] != boseos {
t.Errorf("lattice initialize error: got %v, expected %v", *la.list[0][0], boseos)
} else if len(la.list[1]) != 1 || *la.list[1][0] != boseos {
t.Errorf("lattice initialize error: got %v, expected %v", *la.list[1][0], boseos)
}
if len(la.Output) != 0 {
t.Errorf("lattice initialize error: got %v, expected empty", la.Output)
}
if la.dic == nil {
t.Errorf("lattice initialize error: dic is nil")
}
if la.udic != nil {
t.Errorf("lattice initialize error: got %v, expected empty", la.udic)
}
}
func Test_LatticeBuild(t *testing.T) {
la := New(ipa.Dict(), nil)
if la == nil {
t.Fatal("lattice new failed")
}
defer la.Free()
inp := "あ"
la.Build(inp)
if la.Input != inp {
t.Errorf("got %v, expected %v", la.Input, inp)
}
bos := node{ID: -1}
eos := node{ID: -1, Position: len(inp), Start: 1}
if len(la.list) != 3 {
t.Errorf("lattice initialize error: got %v, expected has 2 eos/bos nodes", la.list)
} else if len(la.list[0]) != 1 || *la.list[0][0] != bos {
t.Errorf("lattice initialize error: got %v, expected %v", *la.list[0][0], bos)
} else if len(la.list[2]) != 1 || *la.list[2][0] != eos {
t.Errorf("lattice initialize error: got %v, expected %v", *la.list[2][0], eos)
}
expected := 4
if len(la.list[1]) != expected {
t.Fatalf("lattice initialize error: got %v, expected %v", len(la.list[1]), expected)
}
l := la.list[1]
for _, v := range l {
if v.Surface != inp {
t.Errorf("lattice initialize error: got %+v, expected surface %s", v, inp)
}
if v.Class != KNOWN {
t.Errorf("lattice initialize error: got %+v, expected class KNOWN", v)
}
}
if len(la.Output) != 0 {
t.Errorf("lattice initialize error: got %v, expected empty", la.Output)
}
if la.dic == nil {
t.Errorf("lattice initialize error: dic is nil")
}
if la.udic != nil {
t.Errorf("lattice initialize error: got %v, expected empty", la.udic)
}
}
func Test_LatticeBuildWithUserDict(t *testing.T) {
const udictPath = "../../sample/userdict.txt"
udic, err := dict.NewUserDict(udictPath)
if err != nil {
t.Fatalf("unexpected error: cannot load user dic, %v", err)
}
la := New(ipa.Dict(), udic)
if la == nil {
t.Fatal("cannot new a lattice")
}
defer la.Free()
inp := "朝青龍"
la.Build(inp)
if la.Input != inp {
t.Errorf("got %v, expected %v", la.Input, inp)
}
if la.list[3][0].Class != USER {
t.Errorf("%+v", la)
}
if len(la.Output) != 0 {
t.Errorf("lattice initialize error: got %v, expected empty", la.Output)
}
if la.dic == nil {
t.Errorf("lattice initialize error: dic is nil")
}
if la.udic == nil {
t.Errorf("lattice initialize error: got %v, expected not empty", la.udic)
}
}
func Test_LatticeBuildUnknown(t *testing.T) {
la := New(ipa.Dict(), nil)
if la == nil {
t.Fatal("lattice new failed")
}
defer la.Free()
inp := "ポポピ"
la.Build(inp)
if la.Input != inp {
t.Errorf("got %v, expected %v", la.Input, inp)
}
bos := node{ID: -1}
eos := node{ID: -1, Position: len(inp), Start: 3}
if len(la.list) != 5 {
t.Errorf("lattice initialize error: got %v, expected has 2 eos/bos nodes", la.list)
} else if len(la.list[0]) != 1 || *la.list[0][0] != bos {
t.Errorf("lattice initialize error: got %v, expected %v", *la.list[0][0], bos)
} else if len(la.list[len(la.list)-1]) != 1 || *la.list[len(la.list)-1][0] != eos {
t.Errorf("lattice initialize error: got %v, expected %v", *la.list[len(la.list)-1][0], eos)
}
expected := 7
if len(la.list[1]) != expected {
t.Fatalf("lattice initialize error: got %v, expected %v", len(la.list[1]), expected)
}
l := la.list[1]
var known, unknown, undef int
for _, v := range l {
if v.Surface != string([]rune(inp)[0:1]) {
t.Errorf("lattice initialize error: got %+v, expected surface %c", v, []rune(inp)[0])
}
switch v.Class {
case KNOWN:
known++
case UNKNOWN:
unknown++
default:
undef++
}
}
if known != 1 {
t.Errorf("lattice initialize error: got KNOWN %d, expected 1, %+v", known, l)
}
if unknown != 6 {
t.Errorf("lattice initialize error: got UNKNOWN %d, expected 6, %+v", unknown, l)
}
if undef != 0 {
t.Errorf("lattice initialize error: got unexpected class %d, %+v", undef, l)
}
if len(la.Output) != 0 {
t.Errorf("lattice initialize error: got %v, expected empty", la.Output)
}
if la.dic == nil {
t.Errorf("lattice initialize error: dic is nil")
}
if la.udic != nil {
t.Errorf("lattice initialize error: got %v, expected empty", la.udic)
}
}
func Test_LatticeBuildMaximumUnknownWordLength(t *testing.T) {
la := New(ipa.Dict(), nil)
if la == nil {
t.Fatal("cannot new a lattice")
}
defer la.Free()
inp := "ポポピポンポコナーノ"
var b bytes.Buffer
for i, step := 0, utf8.RuneCountInString(inp); i < maximumUnknownWordLength; i = i + step {
if _, err := b.WriteString(inp); err != nil {
t.Fatalf("unexpected error: create the test input, %v", b.String())
}
}
la.Build(b.String())
for i := range la.list {
for j := range la.list[i] {
l := utf8.RuneCountInString(la.list[i][j].Surface)
if l > maximumUnknownWordLength {
t.Errorf("too long unknown word, %v", l)
}
}
}
}
func Test_LatticeBuildInvalidInput(t *testing.T) {
la := New(ipa.Dict(), nil)
if la == nil {
t.Fatal("cannot new a lattice")
}
defer la.Free()
inp := "\x96\x7b\x93\xfa" // sjis encoding for '日本'
la.Build(inp)
if la.Input != inp {
t.Errorf("got %v, expected %v", la.Input, inp)
}
bos := node{ID: -1}
eos := node{ID: -1, Position: len(inp), Start: 4}
if len(la.list) != 6 {
t.Errorf("lattice initialize error: got %v, expected has 2 eos/bos nodes", la.list)
} else if len(la.list[0]) != 1 || *la.list[0][0] != bos {
t.Errorf("lattice initialize error: got %v, expected %v", *la.list[0][0], bos)
} else if len(la.list[len(la.list)-1]) != 1 || *la.list[len(la.list)-1][0] != eos {
t.Errorf("lattice initialize error: got %v, expected %v", *la.list[len(la.list)-1][0], eos)
}
}
func Test_KanjiOnly(t *testing.T) {
callAndResponse := []struct {
in string
out bool
}{
{in: "ひらがな", out: false},
{in: "カタカナ", out: false},
{in: "漢字", out: true},
{in: "かな漢字交じり", out: false},
{in: "123", out: false},
{in: "#$%", out: false},
{in: "", out: false},
}
for _, cr := range callAndResponse {
if rsp := kanjiOnly(cr.in); rsp != cr.out {
t.Errorf("in: %v, got %v, expected: %v", cr.in, rsp, cr.out)
}
}
}
func Test_LatticeString(t *testing.T) {
la := New(ipa.Dict(), nil)
if la == nil {
t.Fatal("cannot new a lattice")
}
defer la.Free()
expected := ""
str := la.String()
if str != expected {
t.Errorf("got %v, expected: %v", str, expected)
}
la.Build("わたしまけましたわ")
str = la.String()
if str == "" {
t.Errorf("got empty string")
}
}
func Test_LatticeDot(t *testing.T) {
la := New(ipa.Dict(), nil)
if la == nil {
t.Fatal("cannot new a lattice")
}
defer la.Free()
expected := `graph lattice {
dpi=48;
graph [style=filled, splines=true, overlap=false, fontsize=30, rankdir=LR]
edge [fontname=Helvetica, fontcolor=red, color="#606060"]
node [shape=box, style=filled, fillcolor="#e8e8f0", fontname=Helvetica]
}
`
var b bytes.Buffer
la.Dot(&b)
if b.String() != expected {
t.Errorf("got %v, expected: %v", b.String(), expected)
}
b.Reset()
la.Build("わたしまけましたわポポピ")
la.Forward(Normal)
la.Backward(Normal)
la.Dot(&b)
if b.String() == "" {
t.Errorf("got empty string")
}
}
func Test_LatticeNewAndFree(t *testing.T) {
for i := 0; i < 100; i++ {
la := New(ipa.Dict(), nil)
if la == nil {
t.Fatal("unexpected error: cannot new a lattice")
}
if la.Input != "" {
t.Fatalf("unexpected error: lattice input initialize error, %+v", la.Input)
}
if len(la.Output) != 0 {
t.Fatalf("unexpected error: lattice output initialize error, %+v", la.Output)
}
if len(la.list) != 0 {
t.Fatalf("unexpected error: lattice list initialize error, %+v", la.list)
}
la.Build("すべては科学する心に宿るのだ")
la.Free()
// renew
la = New(ipa.Dict(), nil)
if la == nil {
t.Fatal("unexpected error: cannot new a lattice")
}
if la.Input != "" {
t.Fatalf("unexpected error: lattice input initialize error, %+v", la.Input)
}
if len(la.Output) != 0 {
t.Fatalf("unexpected error: lattice output initialize error, %+v", la.Output)
}
if len(la.list) != 0 {
t.Fatalf("unexpected error: lattice list initialize error, %+v", la.list)
}
la.Free()
}
}
func Test_Forward(t *testing.T) {
la := New(ipa.Dict(), nil)
if la == nil {
t.Fatal("unexpected error: cannot new a lattice")
}
la.Forward(Normal)
la.Forward(Search)
la.Forward(Extended)
for _, m := range []TokenizeMode{Normal, Search, Extended} {
la.Build("わたしまけましたわ.関西国際空港.ポポポポポポポポポポ.\U0001f363\U0001f363\U0001f363\U0001f363\U0001f363\U0001f363\U0001f363\U0001f363\U0001f363\U0001f363")
la.Forward(m)
}
}
func Test_Backward(t *testing.T) {
la := New(ipa.Dict(), nil)
if la == nil {
t.Fatal("unexpected error: cannot new a lattice")
}
// only run
la.Backward(Normal)
la.Backward(Search)
la.Backward(Extended)
for _, m := range []TokenizeMode{Normal, Search, Extended} {
la.Build("わたしまけましたわ.ポポピ")
la.Forward(m)
la.Backward(m)
}
}