Skip to content

Commit

Permalink
[NewJieba] check if the dictionary files exist
Browse files Browse the repository at this point in the history
  • Loading branch information
yanyiwu committed Sep 14, 2024
1 parent caea370 commit 4574b93
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions jieba.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import "C"
import (
"runtime"
"unsafe"
"os"
"fmt"
)

type TokenizeMode int
Expand All @@ -30,6 +32,14 @@ type Jieba struct {

func NewJieba(paths ...string) *Jieba {
dictpaths := getDictPaths(paths...)

// check if the dictionary files exist
for _, path := range dictpaths {
if _, err := os.Stat(path); os.IsNotExist(err) {
panic(fmt.Sprintf("Dictionary file does not exist: %s", path))
}
}

dpath, hpath, upath, ipath, spath := C.CString(dictpaths[0]), C.CString(dictpaths[1]), C.CString(dictpaths[2]), C.CString(dictpaths[3]), C.CString(dictpaths[4])
defer C.free(unsafe.Pointer(dpath))
defer C.free(unsafe.Pointer(hpath))
Expand All @@ -45,6 +55,7 @@ func NewJieba(paths ...string) *Jieba {
spath,
),
}
// set finalizer to free the memory when the object is garbage collected
runtime.SetFinalizer(jieba, (*Jieba).Free)
return jieba
}
Expand Down

0 comments on commit 4574b93

Please sign in to comment.