示例#1
0
// Create a new stemmer, ready for use with the specified language
func NewStemmer(language string) (*Stemmer, error) {
	clang := C.CString(strings.ToLower(language))
	defer C.free(unsafe.Pointer(clang))
	cchar := C.CString("UTF_8")
	defer C.free(unsafe.Pointer(cchar))
	tmp := C.sb_stemmer_new(clang, cchar)
	if tmp == nil {
		return nil, fmt.Errorf("Unable to create stemmer, please ensure you are using a valid language")
	}
	stemmer := &Stemmer{
		stemmer: tmp,
		lock:    new(sync.Mutex),
	}
	return stemmer, nil
}
示例#2
0
// New creates a new stemmer for lang
func New(lang string) (*Stemmer, error) {
	clang := C.CString(lang)
	defer C.free(unsafe.Pointer(clang))

	stmr := &Stemmer{
		lang,
		C.sb_stemmer_new(clang, nil),
	}

	if stmr.stmr == nil {
		return nil, fmt.Errorf("can't create stemmer for lang %s", lang)
	}

	runtime.SetFinalizer(stmr, free)

	return stmr, nil
}
示例#3
0
文件: stem.go 项目: goodsign/snowball
// Creates new word stemmer with specified algorithm and encoding identifiers. If it is successfully created, it
// must be closed as it needs to free native Snowball resources.
//
// NOTE: Check libstemmer/modules.txt for allowed names of algorithms and encodings.
func NewWordStemmer(algorithm string, encoding string) (*WordStemmer, error) {
	ws := new(WordStemmer)

	algStr := C.CString(algorithm)
	encStr := C.CString(encoding)

	defer C.free(unsafe.Pointer(algStr))
	defer C.free(unsafe.Pointer(encStr))

	ws.stemmer = C.sb_stemmer_new(algStr, encStr)

	if nil == ws.stemmer {
		return nil, fmt.Errorf("Cannot create word stemmer with algorithm: '%s' and encoding: '%s'", algorithm, encoding)
	}

	ws.algorithm = algorithm
	ws.encoding = encoding

	return ws, nil
}