Skip to content

Commit

Permalink
Refactor string handling in lattice.go and mecab.go
Browse files Browse the repository at this point in the history
  • Loading branch information
shogo82148 committed Jan 15, 2024
1 parent 641edc6 commit 3d40bc8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
6 changes: 2 additions & 4 deletions lattice.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,9 @@ func (l Lattice) SetSentence(s string) {
panic(errLatticeNotAvailable)
}
length := C.size_t(len(s))
if s == "" {
s = "dummy"
}
input := C.CString(s)
defer C.free(unsafe.Pointer(input))

input := (*C.char)(unsafe.Pointer(unsafe.StringData(s)))
C.mecab_lattice_add_request_type(l.l.lattice, 64) // MECAB_ALLOCATE_SENTENCE = 64
C.mecab_lattice_set_sentence2(l.l.lattice, input, length)
runtime.KeepAlive(l.l)
Expand Down
12 changes: 4 additions & 8 deletions mecab.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,9 @@ func (m MeCab) Parse(s string) (string, error) {
panic(errMeCabNotAvailable)
}
length := C.size_t(len(s))
if s == "" {
s = "dummy"
}
input := C.CString(s)
defer C.free(unsafe.Pointer(input))

input := (*C.char)(unsafe.Pointer(unsafe.StringData(s)))
result := C.mecab_sparse_tostr2(m.m.mecab, input, length)
if result == nil {
return "", newError(m.m.mecab)
Expand Down Expand Up @@ -136,11 +134,9 @@ func (m MeCab) ParseToNode(s string) (Node, error) {
panic(errMeCabNotAvailable)
}
length := C.size_t(len(s))
if s == "" {
s = "dummy"
}
input := C.CString(s)
defer C.free(unsafe.Pointer(input))

input := (*C.char)(unsafe.Pointer(unsafe.StringData(s)))
node := C.mecab_sparse_tonode2(m.m.mecab, input, length)
if node == nil {
return Node{}, newError(m.m.mecab)
Expand Down

0 comments on commit 3d40bc8

Please sign in to comment.