diff --git a/README.md b/README.md index dbf8e6c..72eba45 100644 --- a/README.md +++ b/README.md @@ -15,34 +15,45 @@ Here's my attempt at wrapping FastText C++ library with Golang CGO. - Clone the `FastText` git repository & compile it. ```Bash - $ git clone https://github.com/facebookresearch/fastText - # Cloning... - - $ cd fastText && make - # Compiling... + $ wget https://github.com/facebookresearch/fastText/archive/v0.9.2.zip + $ unzip v0.9.2.zip + $ cd fastText-0.9.2 + $ make ``` -- Clone this repository & copy all the `.o` from previous compile result into directory inside `fastText/obj`. +- Clone this repository & copy all the `.o` from `fastText-0.9.2` into directory inside `fasttext-go-wrapper/fastText/obj`. ```Bash - $ git clone https://github.com/taufik-rama/fasttext-go-wrapper - # Cloning... - + $ git clone https://github.com/fkurushin/fasttext-go-wrapper $ mkdir fastText/obj - - $ cp /path/to/previous/repo/*.o fastText/obj/ + $ cp fastText-0.9.2/*.o fasttext-go-wrapper/fastText/obj/ ``` - Compile the C project ```Bash - $ cd fastText && make - # Compiling... + $ cd fasttext-go-wrapper/fastText && make ``` - Build the Go package normally ```Bash $ go build - # Compiling... + ``` + +## Basic usage +- Initialization + ``` + model, err = fasttext.New(modelName) + if err != nil { + panic(err) + } + ``` + +- Predict vector + ``` + vec, err := model.GetSentenceVector(sentence) + if err != nil { + panic(err) + } ``` \ No newline at end of file diff --git a/app.go b/app.go deleted file mode 100644 index 02f025f..0000000 --- a/app.go +++ /dev/null @@ -1,27 +0,0 @@ -package main - -import "fmt" - -// Example implementation - -func main() { - - // Supply the FastText model file location - model, err := New("basic-model.bin") - if err != nil { - panic(err) - } - - // Label the sentence with that FastText model - sentence := "Sentence to predict" - // err = model.Predict(sentence) - // if err != nil { - // panic(err) - // } - - vec, err := model.GetSentenceVector(sentence) - if err != nil { - panic(err) - } - fmt.Println(vec) -} diff --git a/wrapper.go b/fasttext.go similarity index 75% rename from wrapper.go rename to fasttext.go index 87f6d49..00f99c0 100644 --- a/wrapper.go +++ b/fasttext.go @@ -1,4 +1,4 @@ -package main +package fasttext // #cgo LDFLAGS: -L${SRCDIR}/fastText/lib -lfasttext-wrapper -lstdc++ -lm -pthread // #include @@ -39,7 +39,7 @@ func New(file string) (*Model, error) { status := C.ft_load_model(C.CString(file)) if status != 0 { - return nil, fmt.Errorf("Cannot initialize model on `%s`", file) + return nil, fmt.Errorf("cannot initialize model on `%s`", file) } return &Model{ @@ -47,11 +47,19 @@ func New(file string) (*Model, error) { }, nil } +func (m *Model) GetDimension() (int, error) { + res := int(C.ft_get_vector_dimension()) + if res == -1 { + return res, fmt.Errorf("model is not initialized") + } + return res, nil +} + // Predict the `keyword` func (m *Model) Predict(keyword string) error { if !m.isInitialized { - return errors.New("The FastText model needs to be initialized first. It's should be done inside the `New()` function") + return errors.New("the fasttext model needs to be initialized first. it's should be done inside the `New()` function") } resultSize := 32 @@ -66,7 +74,7 @@ func (m *Model) Predict(keyword string) error { C.int(resultSize), ) if status != 0 { - return fmt.Errorf("Exception when predicting `%s`", keyword) + return fmt.Errorf("exception when predicting `%s`", keyword) } // Here's the result from C @@ -83,7 +91,7 @@ func (m *Model) Predict(keyword string) error { func (m *Model) GetSentenceVector(keyword string) ([]float64, error) { if !m.isInitialized { - return nil, errors.New("The FastText model needs to be initialized first. It's should be done inside the `New()` function") + return nil, errors.New("the fasttext model needs to be initialized first. it's should be done inside the `New()` function") } vecDim := C.ft_get_vector_dimension() @@ -97,7 +105,7 @@ func (m *Model) GetSentenceVector(keyword string) ([]float64, error) { ) if status != 0 { - return nil, fmt.Errorf("Exception when predicting `%s`", keyword) + return nil, fmt.Errorf("exception when predicting `%s`", keyword) } p2 := (*[1 << 30]C.float)(unsafe.Pointer(result)) ret := make([]float64, int(vecDim)) diff --git a/fasttext_test.go b/fasttext_test.go new file mode 100644 index 0000000..d0b5e68 --- /dev/null +++ b/fasttext_test.go @@ -0,0 +1,20 @@ +package fasttext + +import ( + "testing" +) + +func TestGetDimension(t *testing.T) { + model, err := New("model.bin") + if err != nil { + t.Errorf("error loading model: %v", err) + } + d, err := model.GetDimension() + if err != nil { + t.Errorf("error getting dimension: %v", err) + } + if d != 50 { + t.Errorf("wrong dimension") + } + +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..8e9791a --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/fkurushin/fasttext-go-wrapper + +go 1.19