-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathkagome_test.go
73 lines (66 loc) · 1.58 KB
/
kagome_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
package main_test
import (
"bufio"
"fmt"
"os"
"testing"
"github.com/ikawaha/kagome-dict/dict"
"github.com/ikawaha/kagome/v2/filter"
"github.com/ikawaha/kagome/v2/tokenizer"
)
func TestKagomeTokenizerGolden(t *testing.T) {
const (
inputText = "./testdata/bocchan.txt"
goldenText = "./testdata/bocchan.golden"
dumpText = "./testdata/bocchan.dump"
)
d, err := dict.LoadDictFile("testdata/ipa.dict")
if err != nil {
t.Fatalf("unexpected error, %v", err)
}
kagome, err := tokenizer.New(d)
if err != nil {
t.Fatalf("unexpected error, %v", err)
}
in, err := os.Open(inputText)
if err != nil {
t.Fatalf("unexpected error, %v", err)
}
defer in.Close()
golden, err := os.Open(goldenText)
if err != nil {
t.Fatalf("unexpected error, %v", err)
}
defer golden.Close()
dump, err := os.OpenFile(dumpText, os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0o600)
if err != nil {
t.Fatalf("unexpected error, %v", err)
}
defer dump.Close()
gs := bufio.NewScanner(golden)
is := bufio.NewScanner(in)
is.Split(filter.ScanSentences)
for is.Scan() {
tokens := kagome.Tokenize(is.Text())
for _, v := range tokens {
var want string
if gs.Scan() {
want = gs.Text()
} else {
t.Errorf("unexpected golden EOF")
}
got := fmt.Sprintf("%s %s", v, v.POS())
if want != got {
t.Errorf("got %v, want %v", got, want)
}
dump.WriteString(got)
dump.WriteString("\n")
}
}
if err := is.Err(); err != nil {
t.Errorf("unexpected input file scanning error, %v", err)
}
if err := gs.Err(); err != nil {
t.Errorf("unexpected golden file scanning error, %v", err)
}
}