Skip to content

petabyte/JFastText_Win

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Notes for this specific version of the repo

This is a fork of the JFastText repository from github. This is specifically configured to compile the FastText cpp code in windows. Please ask george.sanchez@tr.com if you have any questions about this repo.

Instructions and notes to compile this on windows

  1. Download mingw with a gcc compiler suite - Use this one it already comes with gcc (https://mingw-w64.org/doku.php) if you want you can download mingw and setup gcc separately

  2. Make sure the gcc is in your windows PATH e.g. C:\msys64\usr\bin;C:\mingw-w64\mingw64\bin;

  3. Now you can trigger the maven clean install for this project

  4. Modified fastext.cc to use the mingw.thread.h header instead of the standard thread.h This is to address the error std::thread when using the g++ plus compiler on mingw (https://github.com/meganz/mingw-std-threads)

    Notes: if you want to compile this for another platform- change the following build plugin properties in the pom.

             <configuration>
                 <properties>windows-x86_64-mingw</properties>
             </configuration>
    

Introduction

JFastText is a Java wrapper for Facebook's fastText, a library for efficient learning of word embeddings and fast sentence classification. The JNI interface is built using javacpp.

The library provides full fastText's command line interface. It also provides the API for loading trained model from file to do label prediction in memory. Model training and quantization are supported via the command line interface.

JFastText is ideal for building fast text classifiers in Java.

Maven dependency

<dependency>
  <groupId>com.github.vinhkhuc</groupId>
  <artifactId>jfasttext</artifactId>
  <version>0.3</version>
</dependency>

The Jar package on Maven Central is bundled with precompiled fastText library for Windows, Linux and MacOSX 64bit.

Building

C++ compiler (g++ on Mac/Linux or cl.exe on Windows) is required to compile fastText's code.

git clone --recursive https://github.com/vinhkhuc/JFastText
cd JFastText
mvn package

Examples

Examples on how to use JFastText can be found at examples/api and examples/cmd.

How to use

Initialization

import com.github.jfasttext.JFastText;
...
JFastText jft = new JFastText();

Word embedding learning

jft.runCmd(new String[] {
        "skipgram",
        "-input", "src/test/resources/data/unlabeled_data.txt",
        "-output", "src/test/resources/models/skipgram.model",
        "-bucket", "100",
        "-minCount", "1"
});

Text classification

// Train supervised model
jft.runCmd(new String[] {
        "supervised",
        "-input", "src/test/resources/data/labeled_data.txt",
        "-output", "src/test/resources/models/supervised.model"
});

// Load model from file
jft.loadModel("src/test/resources/models/supervised.model.bin");

// Do label prediction
String text = "What is the most popular sport in the US ?";
JFastText.ProbLabel probLabel = jft.predictProba(text);
System.out.printf("\nThe label of '%s' is '%s' with probability %f\n",
        text, probLabel.label, Math.exp(probLabel.logProb));

FastText's command line

$ java -jar target/jfasttext-*-jar-with-dependencies.jar
usage: fasttext <command> <args>

The commands supported by fasttext are:

  supervised              train a supervised classifier
  quantize                quantize a model to reduce the memory usage
  test                    evaluate a supervised classifier
  predict                 predict most likely labels
  predict-prob            predict most likely labels with probabilities
  skipgram                train a skipgram model
  cbow                    train a cbow model
  print-word-vectors      print word vectors given a trained model
  print-sentence-vectors  print sentence vectors given a trained model
  nn                      query for nearest neighbors
  analogies               query for analogies

For example:

$ java -jar target/jfasttext-*-jar-with-dependencies.jar quantize -h

License

BSD

References

(From fastText's references)

Please cite 1 if using this code for learning word representations or 2 if using for text classification.

Enriching Word Vectors with Subword Information

[1] P. Bojanowski*, E. Grave*, A. Joulin, T. Mikolov, Enriching Word Vectors with Subword Information

@article{bojanowski2016enriching,
  title={Enriching Word Vectors with Subword Information},
  author={Bojanowski, Piotr and Grave, Edouard and Joulin, Armand and Mikolov, Tomas},
  journal={arXiv preprint arXiv:1607.04606},
  year={2016}
}

Bag of Tricks for Efficient Text Classification

[2] A. Joulin, E. Grave, P. Bojanowski, T. Mikolov, Bag of Tricks for Efficient Text Classification

@article{joulin2016bag,
  title={Bag of Tricks for Efficient Text Classification},
  author={Joulin, Armand and Grave, Edouard and Bojanowski, Piotr and Mikolov, Tomas},
  journal={arXiv preprint arXiv:1607.01759},
  year={2016}
}

FastText.zip: Compressing text classification models

[3] A. Joulin, E. Grave, P. Bojanowski, M. Douze, H. Jégou, T. Mikolov, FastText.zip: Compressing text classification models

@article{joulin2016fasttext,
  title={FastText.zip: Compressing text classification models},
  author={Joulin, Armand and Grave, Edouard and Bojanowski, Piotr and Douze, Matthijs and J{\'e}gou, H{\'e}rve and Mikolov, Tomas},
  journal={arXiv preprint arXiv:1612.03651},
  year={2016}
}

(* These authors contributed equally.)

About

Fork of JFastText for windows (win-x86_64-mingw) compile for the cpp fastText portion of it.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published