Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: func Close should be idempotent #17

Merged
merged 2 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions kiwi.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,15 @@ func (k *Kiwi) Analyze(text string, topN int, options AnalyzeOption) ([]TokenRes

// Close frees the resource allocated for Kiwi and returns the exit status.
// This must be called after New.
//
// Returns 0 if successful.
// Safe to call multiple times.
func (k *Kiwi) Close() int {
return int(C.kiwi_close(k.handler))
if k.handler != nil {
out := int(C.kiwi_close(k.handler))
k.handler = nil
return out
}
return 0
}

// KiwiBuilder is a wrapper for the kiwi C library.
Expand All @@ -130,28 +135,33 @@ func NewBuilder(modelPath string, numThread int, options BuildOption) *KiwiBuild
}

// AddWord set custom word with word, pos, score.
func (k *KiwiBuilder) AddWord(word string, pos POSType, score float32) int {
return int(C.kiwi_builder_add_word(k.handler, C.CString(word), C.CString(string(pos)), C.float(score)))
func (kb *KiwiBuilder) AddWord(word string, pos POSType, score float32) int {
return int(C.kiwi_builder_add_word(kb.handler, C.CString(word), C.CString(string(pos)), C.float(score)))
}

// LoadDict loads user dict with dict file path.
func (k *KiwiBuilder) LoadDict(dictPath string) int {
return int(C.kiwi_builder_load_dict(k.handler, C.CString(dictPath)))
func (kb *KiwiBuilder) LoadDict(dictPath string) int {
return int(C.kiwi_builder_load_dict(kb.handler, C.CString(dictPath)))
}

// Build creates kiwi instance with user word etc.
func (kb *KiwiBuilder) Build() *Kiwi {
h := C.kiwi_builder_build(kb.handler)
defer C.kiwi_builder_close(kb.handler)
defer kb.Close()
return &Kiwi{
handler: h,
}
}

// Close frees the resource allocated for KiwiBuilder and returns the exit status.
// This must be called after New but not need to called after Build.
//
// Returns 0 if successful.
func (k *KiwiBuilder) Close() int {
return int(C.kiwi_builder_close(k.handler))
// Safe to call multiple times.
func (kb *KiwiBuilder) Close() int {
if kb.handler != nil {
out := int(C.kiwi_builder_close(kb.handler))
kb.handler = nil
return out
}
return 0
}
7 changes: 7 additions & 0 deletions kiwi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ func TestAddWord(t *testing.T) {
kiwi := kb.Build()
res, _ := kiwi.Analyze("아버지가 방에 들어가신다", 1, KIWI_MATCH_ALL)

// kb should have been closed.
assert.Equal(t, 0, kb.Close())

expected := []TokenResult{
{
Tokens: []TokenInfo{
Expand Down Expand Up @@ -130,6 +133,10 @@ func TestLoadDict(t *testing.T) {
assert.Equal(t, "", err)

kiwi := kb.Build()

// kb should have been closed already.
assert.Equal(t, 0, kb.Close())

res, _ := kiwi.Analyze("아버지가 방에 들어가신다", 1, KIWI_MATCH_ALL)

expected := []TokenResult{
Expand Down