https://github.com/vunb/node-fasttext
Nodejs binding for fasttext representation and classification.
https://github.com/vunb/node-fasttext
classifier facebook-fasttext fasttext node-fasttext text-classification vntk
Last synced: 2 months ago
JSON representation
Nodejs binding for fasttext representation and classification.
- Host: GitHub
- URL: https://github.com/vunb/node-fasttext
- Owner: vunb
- License: mit
- Created: 2017-11-23T03:08:06.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2024-02-26T07:59:39.000Z (over 1 year ago)
- Last Synced: 2025-06-25T06:09:48.096Z (3 months ago)
- Topics: classifier, facebook-fasttext, fasttext, node-fasttext, text-classification, vntk
- Language: C++
- Size: 40 MB
- Stars: 43
- Watchers: 1
- Forks: 14
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# node-fasttext
Nodejs binding for fasttext representation and classification.
[](./LICENSE)
[](https://www.npmjs.com/package/fasttext)
[](https://www.npmjs.com/package/fasttext)
[](https://travis-ci.org/vunb/node-fasttext)
[](https://ci.appveyor.com/project/vunb/node-fasttext/branch/master)> This is a link to the Facebook [fastText](https://github.com/facebookresearch/fastText). A Library for efficient text classification and representation learning.
* FASTTEXT_VERSION = 12;
* FASTTEXT_FILEFORMAT_MAGIC_INT32 = 793712314;# Installation
Using npm:
> npm install fasttext --save
# fastText Classifier
According to [fasttext.cc](https://fasttext.cc/docs/en/supervised-tutorial.html). We have a simple classifier for executing prediction models about `cooking` from stackexchange questions:
```js
const path = require('path');
const fastText = require('fasttext');const model = path.resolve(__dirname, './model_cooking.bin');
const classifier = new fastText.Classifier(model);classifier.predict('Why not put knives in the dishwasher?', 5)
.then((res) => {
if (res.length > 0) {
let tag = res[0].label; // __label__knives
let confidence = res[0].value // 0.8787146210670471
console.log('classify', tag, confidence, res);
} else {
console.log('No matches');
}
});
```The model haved trained before with the followings params:
```js
const path = require('path');
const fastText = require('fasttext');let data = path.resolve(path.join(__dirname, '../data/cooking.train.txt'));
let model = path.resolve(path.join(__dirname, '../data/cooking.model'));let classifier = new fastText.Classifier();
let options = {
input: data,
output: model,
loss: "softmax",
dim: 200,
bucket: 2000000
}classifier.train('supervised', options)
.then((res) => {
console.log('model info after training:', res)
// Input <<<<< C:\projects\node-fasttext\test\data\cooking.train.txt
// Output >>>>> C:\projects\node-fasttext\test\data\cooking.model.bin
// Output >>>>> C:\projects\node-fasttext\test\data\cooking.model.vec
});
```Or you can train directly from the command line with fasttext builded from official source:
```bash
# Training
~/fastText/data$ ./fasttext supervised -input cooking.train -output model_cooking -lr 1.0 -epoch 25 -wordNgrams 2 -bucket 200000 -dim 50 -loss hs
Read 0M words
Number of words: 8952
Number of labels: 735
Progress: 100.0% words/sec/thread: 1687554 lr: 0.000000 loss: 5.247591 eta: 0h0m 4m# Testing
~/fastText/data$ ./fasttext test model_cooking.bin cooking.valid
N 3000
P@1 0.587
R@1 0.254
Number of examples: 3000
```# Nearest neighbor
Simple class for searching nearest neighbors:
```js
const path = require('path');
const fastText = require('fasttext');const model = path.resolve(__dirname, './skipgram.bin');
const query = new fastText.Query(model);query.nn('word', 5, (err, res) => {
if (err) {
console.error(err);
} else if (res.length > 0) {
let tag = res[0].label; // letter
let confidence = res[0].value // 0.99992
console.log('Nearest neighbor', tag, confidence, res);
} else {
console.log('No matches');
}
});
```# Build from source
See [Installation Prerequisites](https://github.com/nodejs/node-gyp#installation).
```bash
# install dependencies and tools
npm install# build node-fasttext from source
npm run build# run unit-test
npm test
```# Contributing
Pull requests and stars are highly welcome.
For bugs and feature requests, please [create an issue](https://github.com/vunb/node-fasttext/issues/new).