npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fasttext

v1.0.0

Published

Nodejs binding for Fasttext representation and classification

Downloads

8,024

Readme

node-fasttext

Nodejs binding for fasttext representation and classification.

MIT License npm version downloads Travis Appveyor

This is a link to the Facebook 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. We have a simple classifier for executing prediction models about cooking from stackexchange questions:

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:

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:

# 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:

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.

# 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.