forked from shogo82148/go-mecab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmecab_test.go
222 lines (193 loc) · 5.17 KB
/
mecab_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
package mecab
import (
"fmt"
"os"
"runtime"
"strings"
"testing"
)
var mecabrcPath string
func init() {
// get mecabrc path from mecab-config from the environment value.
if path := os.Getenv("MECABRC_PATH"); path != "" {
mecabrcPath = path
}
}
func rcfile(config map[string]string) map[string]string {
if mecabrcPath != "" {
config["rcfile"] = mecabrcPath
}
return config
}
func TestNewMeCab(t *testing.T) {
mecab, err := New(rcfile(map[string]string{
"output-format-type": "wakati",
"all-morphs": "",
}))
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
defer mecab.Destroy()
}
func TestNewMeCab_error(t *testing.T) {
_, err := New(rcfile(map[string]string{
"output-format-type": "unknown format",
}))
if err == nil {
t.Errorf("expected error, but not")
return
}
if !strings.Contains(err.Error(), "unknown format type [unknown format]") {
t.Errorf("want %q error, got %q", "unknown format type [unknown format]", err.Error())
}
}
func TestParse(t *testing.T) {
mecab, err := New(rcfile(map[string]string{
"output-format-type": "wakati",
}))
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
defer mecab.Destroy()
result, err := mecab.Parse("こんにちは世界")
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
expected := "こんにちは 世界 \n"
if result != expected {
t.Errorf("want `%s`, but `%s`", expected, result)
}
}
func BenchmarkParse(b *testing.B) {
mecab, _ := New(rcfile(map[string]string{
"output-format-type": "wakati",
}))
defer mecab.Destroy()
for i := 0; i < b.N; i++ {
mecab.Parse("こんにちは世界")
}
}
func TestParseLattice(t *testing.T) {
mecab, err := New(rcfile(map[string]string{
"output-format-type": "wakati",
}))
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
defer mecab.Destroy()
lattice, err := NewLattice()
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
defer lattice.Destroy()
lattice.SetSentence("こんにちは世界")
err = mecab.ParseLattice(lattice)
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
expected := "こんにちは\t感動詞,*,*,*,*,*,こんにちは,コンニチハ,コンニチワ\n" +
"世界\t名詞,一般,*,*,*,*,世界,セカイ,セカイ\n" +
"EOS\n"
if lattice.String() != expected {
t.Errorf("expected %s, but %s", expected, lattice.String())
}
}
func BenchmarkParseLattice(b *testing.B) {
mecab, _ := New(rcfile(map[string]string{
"output-format-type": "wakati",
}))
defer mecab.Destroy()
lattice, _ := NewLattice()
defer lattice.Destroy()
for i := 0; i < b.N; i++ {
lattice.SetSentence("こんにちは世界")
mecab.ParseLattice(lattice)
lattice.String()
}
}
func TestParseToNode(t *testing.T) {
mecab, err := New(rcfile(map[string]string{}))
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
defer mecab.Destroy()
// XXX: avoid GC, MeCab 0.996 has GC problem (see https://github.com/taku910/mecab/pull/24)
mecab.Parse("")
node, err := mecab.ParseToNode("こんにちは世界")
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
node = node.Next()
if node.Surface() != "こんにちは" {
t.Errorf("want こんにちは, but %s", node.Surface())
}
node = node.Next()
if node.Surface() != "世界" {
t.Errorf("want 世界, but %s", node.Surface())
}
}
func TestParseToWordNodes(t *testing.T) {
mecab, err := New(rcfile(map[string]string{}))
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
defer mecab.Destroy()
// XXX: avoid GC, MeCab 0.996 has GC problem (see https://github.com/taku910/mecab/pull/24)
mecab.Parse("")
wordNodeList, err := mecab.ParseToWordNodes("こんにちは世界 こんばんは世界")
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
if len(wordNodeList) != 2 {
t.Errorf("want 2, but %d", len(wordNodeList))
}
if len(wordNodeList[0].Nodes) != 2 {
t.Errorf("want 2, but %d", len(wordNodeList[0].Nodes))
}
if len(wordNodeList[1].Nodes) != 2 {
t.Errorf("want 2, but %d", len(wordNodeList[1].Nodes))
}
wordNodeStrings := fmt.Sprintf("%s|%s", wordNodeList[0].Word, wordNodeList[1].Word)
if wordNodeStrings != "こんにちは世界|こんばんは世界" {
t.Errorf("want こんにちは世界|こんばんは世界, but %s", wordNodeStrings)
}
surfaceStrings := fmt.Sprintf(
"%s|%s|%s|%s",
wordNodeList[0].Nodes[0].Surface,
wordNodeList[0].Nodes[1].Surface,
wordNodeList[1].Nodes[0].Surface,
wordNodeList[1].Nodes[1].Surface,
)
if surfaceStrings != "こんにちは|世界|こんばんは|世界" {
t.Errorf("want こんにちは|世界|こんばんは|世界, but %s", surfaceStrings)
}
}
func TestMeCabFinalizer(t *testing.T) {
for i := 0; i < 10000; i++ {
New(rcfile(map[string]string{}))
}
runtime.GC()
runtime.GC()
runtime.GC()
}
func BenchmarkParseToNode(b *testing.B) {
mecab, _ := New(rcfile(map[string]string{}))
defer mecab.Destroy()
// XXX: avoid GC, MeCab 0.996 has GC problem (see https://github.com/taku910/mecab/pull/24)
mecab.Parse("")
for i := 0; i < b.N; i++ {
for node, _ := mecab.ParseToNode("こんにちは世界"); !node.IsZero(); node = node.Next() {
node.Surface()
}
}
}