fasttext-js

Node.js interface for fastText

Usage no npm install needed!

<script type="module">
  import fasttextJs from 'https://cdn.skypack.dev/fasttext-js';
</script>

README

fasttext-js

Build Status

Node.js interface for fastText

Setup

We can install fasttext-js locally by running:

npm install --save fasttext-js

or

yarn add fasttext-js

Usage

import { predictAsync, trainAsync } from 'fasttext-js';

const dataPath = './inbound.txt';
/* inbound.txt content
Opt in __label__N
Opt out __label_Y
Hello __label__N
...
*/
const modelPath = './model.bin';

// Train supervised text classifiers
trainAsync(dataPath, modelPath)
  .then(() => {
    const messages = [
      'Opt in',
      'Opt out'
    ];

    // Obtain the most likely label for an array of of sentences with confidence rate
    return predictAsync(modelPath, messages);
  })
  .then(({ predictions }) => {
    console.log(predictions);
    /*
    "predictions": [
        {
            "label": "N",
            "confidence": 0.986328
        },
        {
            "label": "Y",
            "confidence": 0.693359
        }
    ]
    */
  })
  .catch((err) => {
    console.log(err);
  });

References

Train supervised text classifiers with txt file at dataPath and store model to modelPath.

trainAsync(dataPath, modelPath) // > Promise

Obtain the most likely label for an array of of sentences with confidence rate using model at modelPath.

predictAsync(modelPath, sentences) // > Promise