1.0.2 • Published 2 years ago

ngram-model v1.0.2

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
2 years ago

N-Gram Model npm-shield ts-shield

JavaScript implementation of N-Gram model for text generation. You can train the model and it can guess next N tokens of a token sequence.

Demo

Live demo here

Installation

npm i ngram-model

or

yarn add ngram-model

Usage

import NGramModel from 'ngram-model';

// create a 4-gram model
const NGram = new NGramModel(4);

// pass the model a long text to learn
NGram.train('SOME LONG TRAINER TEXT');

// train more...
NGram.train('EVEN MORE TRAINING');

// guess next 3 tokens, based on the experience from training
NGram.guess('I need a', 3);

// guess as many next tokens as possible, based on the experience from training
NGram.guess('She is');

// guess as many next tokens as possible, the starting text is up to what the 
// model has seen on training texts
NGram.guess();

new NGramModel(N)

  • N number N in N-gram model

Creates an N-Gram model

NGram.train(text)

  • text <string> text to train the model

Trains the model using a training text

NGram.guess([starterText, tokenCount])

  • starterText <string> starter text to be used for guessing next tokens
  • tokenCount <number> maximum number of tokens to be guessed
  • Returns: <string[]> array of guessed tokens

Guesses next tokens of the sequence based on experience from trainings (will throw error if no training is done before)