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

Feature add distance #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
39 changes: 25 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
```
27 changes: 0 additions & 27 deletions app.go

This file was deleted.

20 changes: 14 additions & 6 deletions wrapper.go → fasttext.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package fasttext

// #cgo LDFLAGS: -L${SRCDIR}/fastText/lib -lfasttext-wrapper -lstdc++ -lm -pthread
// #include <stdlib.h>
Expand Down Expand Up @@ -39,19 +39,27 @@ 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{
isInitialized: true,
}, 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
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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))
Expand Down
20 changes: 20 additions & 0 deletions fasttext_test.go
Original file line number Diff line number Diff line change
@@ -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")
}

}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/fkurushin/fasttext-go-wrapper

go 1.19