-
Notifications
You must be signed in to change notification settings - Fork 1
/
mitie.go
43 lines (33 loc) · 783 Bytes
/
mitie.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
package gomitie
/*
#cgo LDFLAGS: -lmitie
#include <stdlib.h>
#include <stdio.h>
#include "mitie.h"
*/
import "C"
import (
"sync"
"unsafe"
"github.com/michlabs/cgoutil"
)
var mutex *sync.Mutex = &sync.Mutex{}
// lock MITIE
// To access MITIE in multiple goroutines,
// this function MUST be called for thread safety
func Lock() {
mutex.Lock()
}
// unlock MITIE
func Unlock() {
mutex.Unlock()
}
// Tokenize returns a slice that contains a tokenized copy of the input text.
func Tokenize(text string) []string {
cstr := C.CString(text)
defer C.free(unsafe.Pointer(cstr))
ctokens := C.mitie_tokenize(cstr)
defer C.mitie_free(unsafe.Pointer(ctokens))
cArrString := cgoutil.NewCStringArrayFromPointer(unsafe.Pointer(ctokens))
return cArrString.ToSlice()
}