-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathexample_test.go
144 lines (126 loc) · 3.44 KB
/
example_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
package mecab_test
import (
"fmt"
"os"
"github.com/shogo82148/go-mecab"
)
func ExampleMeCab_Parse() {
options := map[string]string{}
if path := os.Getenv("MECABRC_PATH"); path != "" {
options["rcfile"] = path
}
tagger, err := mecab.New(options)
if err != nil {
panic(err)
}
defer tagger.Destroy()
result, err := tagger.Parse("こんにちは世界")
if err != nil {
panic(err)
}
fmt.Println(result)
// Output:
// こんにちは 感動詞,*,*,*,*,*,こんにちは,コンニチハ,コンニチワ
// 世界 名詞,一般,*,*,*,*,世界,セカイ,セカイ
// EOS
}
func ExampleMeCab_ParseLattice() {
options := map[string]string{}
if path := os.Getenv("MECABRC_PATH"); path != "" {
options["rcfile"] = path
}
tagger, err := mecab.New(options)
if err != nil {
panic(err)
}
defer tagger.Destroy()
lattice, err := mecab.NewLattice()
if err != nil {
panic(err)
}
lattice.SetSentence("こんにちは世界")
err = tagger.ParseLattice(lattice)
if err != nil {
panic(err)
}
fmt.Println(lattice.String())
// Output:
// こんにちは 感動詞,*,*,*,*,*,こんにちは,コンニチハ,コンニチワ
// 世界 名詞,一般,*,*,*,*,世界,セカイ,セカイ
// EOS
}
func ExampleMeCab_ParseLattice_nBest() {
options := map[string]string{}
if path := os.Getenv("MECABRC_PATH"); path != "" {
options["rcfile"] = path
}
tagger, err := mecab.New(options)
if err != nil {
panic(err)
}
defer tagger.Destroy()
lattice, err := mecab.NewLattice()
if err != nil {
panic(err)
}
lattice.SetSentence("こんにちは世界")
lattice.AddRequestType(mecab.RequestTypeNBest)
err = tagger.ParseLattice(lattice)
if err != nil {
panic(err)
}
for i := 0; i < 5; i++ {
fmt.Println(lattice.String())
if !lattice.Next() {
break
}
}
// Output:
// こんにちは 感動詞,*,*,*,*,*,こんにちは,コンニチハ,コンニチワ
// 世界 名詞,一般,*,*,*,*,世界,セカイ,セカイ
// EOS
//
// こんにちは 感動詞,*,*,*,*,*,こんにちは,コンニチハ,コンニチワ
// 世界 名詞,一般,*,*,*,*,世界,セカイ,セカイ
// EOS
//
// こんにちは 感動詞,*,*,*,*,*,こんにちは,コンニチハ,コンニチワ
// 世 名詞,一般,*,*,*,*,世,ヨ,ヨ
// 界 名詞,接尾,一般,*,*,*,界,カイ,カイ
// EOS
//
// こんにちは 感動詞,*,*,*,*,*,こんにちは,コンニチハ,コンニチワ
// 世 名詞,一般,*,*,*,*,世,ヨ,ヨ
// 界 名詞,固有名詞,地域,一般,*,*,界,サカイ,サカイ
// EOS
//
// こんにちは 感動詞,*,*,*,*,*,こんにちは,コンニチハ,コンニチワ
// 世 名詞,接尾,助数詞,*,*,*,世,セイ,セイ
// 界 名詞,接尾,一般,*,*,*,界,カイ,カイ
// EOS
}
func ExampleMeCab_ParseToNode() {
options := map[string]string{}
if path := os.Getenv("MECABRC_PATH"); path != "" {
options["rcfile"] = path
}
tagger, err := mecab.New(options)
if err != nil {
panic(err)
}
defer tagger.Destroy()
// XXX: avoid GC problem with MeCab 0.996 (see https://github.com/taku910/mecab/pull/24)
tagger.Parse("")
node, err := tagger.ParseToNode("こんにちは世界")
if err != nil {
panic(err)
}
for ; !node.IsZero(); node = node.Next() {
fmt.Printf("%s\t%s\n", node.Surface(), node.Feature())
}
// Output:
// BOS/EOS,*,*,*,*,*,*,*,*
// こんにちは 感動詞,*,*,*,*,*,こんにちは,コンニチハ,コンニチワ
// 世界 名詞,一般,*,*,*,*,世界,セカイ,セカイ
// BOS/EOS,*,*,*,*,*,*,*,*
}