/fasttext-go-wrapper

A simple Golang wrapper for fastText text classification library.

Primary LanguageC++MIT LicenseMIT

FastText Golang Wrapper

Overview

Here's my attempt at wrapping FastText C++ library with Golang CGO.

Requirements

Compiling

  • Clone the FastText git repository & compile it.

    $ 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 fastText-0.9.2 into directory inside fasttext-go-wrapper/fastText/obj.

    $ git clone https://github.com/fkurushin/fasttext-go-wrapper
    $ mkdir fasttext-go-wrapper/fastText/obj
    $ cp fastText-0.9.2/*.o fasttext-go-wrapper/fastText/obj
    $ cp fastText-0.9.2/src/*.h fasttext-go-wrapper/fastText/include/fastText
  • Compile the C project

    $ cd fasttext-go-wrapper/fastText && make
  • Build the Go package normally (in the fasttext-go-wrapper/ dir)

    $ go build
  • If error with linker occures in MacOS

    $ sudo cp fasttext-go-wrapper/fastText/lib/libfasttext-wrapper.a /usr/local/lib/

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)
    }
    

be aware that this method returns a non-normalized vector

  • Get model dimension
    d, err := model.GetDimension()
    if err != nil {
    	panic(err)
    }
    

Example of Dockerfile

WORKDIR /src
RUN wget https://github.com/facebookresearch/fastText/archive/v0.9.2.zip && \
    unzip v0.9.2.zip

WORKDIR /src/fastText-0.9.2
RUN make

WORKDIR /src
RUN git clone https://github.com/fkurushin/fasttext-go-wrapper && \
    rm v0.9.2.zip

WORKDIR /src/fasttext-go-wrapper/fastText/obj
RUN cp ../../../fastText-0.9.2/*.o .

WORKDIR /src/fasttext-go-wrapper/fastText
RUN make