forked from shogo82148/go-mecab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmecab.go
221 lines (194 loc) · 4.86 KB
/
mecab.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
package mecab
// #include <mecab.h>
// #include <stdlib.h>
import "C"
import (
"errors"
"fmt"
"reflect"
"regexp"
"runtime"
"strings"
"unsafe"
)
var errMeCabNotAvailable = errors.New("mecab: mecab is not available")
// to introduce garbage-collection while maintaining backwards compatibility.
type mecab struct {
mecab *C.mecab_t
}
func newMeCab(m *C.mecab_t) *mecab {
ret := &mecab{
mecab: m,
}
runtime.SetFinalizer(ret, finalizeMeCab)
return ret
}
// It is a marker that a mecab must not be copied after the first use.
// See https://github.com/golang/go/issues/8005#issuecomment-190753527
// for details.
func (*mecab) Lock() {}
func finalizeMeCab(m *mecab) {
if m.mecab != nil {
C.mecab_destroy(m.mecab)
}
m.mecab = nil
}
// MeCab is a morphological parser.
type MeCab struct {
m *mecab
}
// New returns new MeCab parser.
func New(args map[string]string) (MeCab, error) {
// build the argument
opts := make([]*C.char, 0, len(args)+2)
opt := C.CString("mecab")
defer C.free(unsafe.Pointer(opt))
opts = append(opts, opt)
opt = C.CString("--allocate-sentence")
defer C.free(unsafe.Pointer(opt))
opts = append(opts, opt)
for k, v := range args {
var goopt string
if v != "" {
goopt = fmt.Sprintf("--%s=%s", k, v)
} else {
goopt = "--" + k
}
opt := C.CString(goopt)
defer C.free(unsafe.Pointer(opt))
opts = append(opts, opt)
}
// C.mecab_new sets an error in the thread local storage.
// so C.mecab_new and C.mecab_strerror must be call in same thread.
runtime.LockOSThread()
defer runtime.UnlockOSThread()
// create new MeCab
m := C.mecab_new(C.int(len(opts)), (**C.char)(&opts[0]))
if m == nil {
return MeCab{}, newError(nil)
}
return MeCab{
m: newMeCab(m),
}, nil
}
// Destroy frees the MeCab parser.
func (m MeCab) Destroy() {
runtime.SetFinalizer(m.m, nil) // clear the finalizer
if m.m.mecab != nil {
C.mecab_destroy(m.m.mecab)
}
m.m.mecab = nil
}
// Parse parses the string and returns the result as string
func (m MeCab) Parse(s string) (string, error) {
if m.m.mecab == nil {
panic(errMeCabNotAvailable)
}
length := C.size_t(len(s))
if s == "" {
s = "dummy"
}
header := (*reflect.StringHeader)(unsafe.Pointer(&s))
input := (*C.char)(unsafe.Pointer(header.Data))
result := C.mecab_sparse_tostr2(m.m.mecab, input, length)
if result == nil {
return "", newError(m.m.mecab)
}
runtime.KeepAlive(s)
runtime.KeepAlive(m.m)
return C.GoString(result), nil
}
// ParseToString is alias of Parse
func (m MeCab) ParseToString(s string) (string, error) {
if m.m.mecab == nil {
panic(errMeCabNotAvailable)
}
return m.Parse(s)
}
// ParseLattice parses the lattice and returns the result as string.
func (m MeCab) ParseLattice(lattice Lattice) error {
if m.m.mecab == nil {
panic(errMeCabNotAvailable)
}
if C.mecab_parse_lattice(m.m.mecab, lattice.l.lattice) == 0 {
return newError(m.m.mecab)
}
runtime.KeepAlive(m.m)
return nil
}
// ParseToNode parses the string and returns the result as Node
func (m MeCab) ParseToNode(s string) (Node, error) {
if m.m.mecab == nil {
panic(errMeCabNotAvailable)
}
length := C.size_t(len(s))
if s == "" {
s = "dummy"
}
header := (*reflect.StringHeader)(unsafe.Pointer(&s))
input := (*C.char)(unsafe.Pointer(header.Data))
node := C.mecab_sparse_tonode2(m.m.mecab, input, length)
if node == nil {
return Node{}, newError(m.m.mecab)
}
runtime.KeepAlive(s)
return Node{
node: node,
mecab: m.m,
}, nil
}
// ParseToWordNodes parses the string and returns the result as Node
func (m MeCab) ParseToWordNodes(s string) ([]WordNode, error) {
if m.m.mecab == nil {
panic(errMeCabNotAvailable)
}
wordNodeList := []WordNode{}
wordList := strings.Split(regexp.MustCompile(`\s+`).ReplaceAllString(s, " "), " ")
for _, word := range wordList {
wordNodeList = append(wordNodeList, WordNode{
Word: word,
Nodes: []TokenizedWord{},
WordLength: len(word),
NodeSurfaceLength: 0,
})
}
length := C.size_t(len(s))
if s == "" {
s = "dummy"
}
header := (*reflect.StringHeader)(unsafe.Pointer(&s))
input := (*C.char)(unsafe.Pointer(header.Data))
node := C.mecab_sparse_tonode2(m.m.mecab, input, length)
if node == nil {
return []WordNode{}, newError(m.m.mecab)
}
runtime.KeepAlive(s)
mecabNode := Node{
node: node,
mecab: m.m,
}
mecabNode = mecabNode.Next()
idx := 0
for idx < len(wordNodeList) {
surface, feature := mecabNode.Surface(), strings.Split(mecabNode.Feature(), ",")[0]
wordNodeList[idx].Nodes = append(
wordNodeList[idx].Nodes,
TokenizedWord{
Surface: surface,
Feature: feature,
},
)
wordNodeList[idx].NodeSurfaceLength += len(surface)
if wordNodeList[idx].WordLength == wordNodeList[idx].NodeSurfaceLength {
idx += 1
}
mecabNode = mecabNode.Next()
}
return wordNodeList, nil
}
func (m MeCab) Error() error {
if m.m.mecab == nil {
panic(errMeCabNotAvailable)
}
return newError(m.m.mecab)
}