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

string-markov-js

v1.3.2

Published

A package for probabilistically generating text using markov chains

Downloads

27

Readme

string-markov-js

A nodejs package for probabilistically generating text using markov chains.

www.npmjs.com/package/string-markov-js

To install, enter the directory of your node package, and type

npm install string-markov-js

Including the module:

var markov = require('string-markov-js');

Creating new training data set

A data set can be trained and can generate text using its training texts. To initialize a new data set, use:

var dataset = markov.newDataSet();

This way, many different datasets can be trained on different texts, and used concurrently.

Training

From a string

var string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit";
var ngram = 2;
var preserveLineBreaks = true;

dataset.trainOnString(string, ngram, preserveLineBreaks);

From a file

var filename = 'training.txt';
var ngram = 3;
var preserveLineBreaks = true;

dataset.trainOnFile(filename, ngram, preserveLineBreaks, function() {
	console.log("Training complete.");
});

Line breaks can be preserved to maintain a similar structure to the training corpus (e.g. in the case of poetry), or they can be removed.

If you wish to train on a set of files, trainOnFile can also take in an array of filenames, as such:

dataset.trainOnFile(['beemoviescript.txt', 'constitution.txt'], 3, true, function() {
	console.log("Training complete.");
});

Clearing data

If you wish to remove all training data from a given data set, call:

dataset.clearData();

Generating Text

var startWithCapitalNGram = true;

// generate 100 words of text, beginning with an ngram that was capitalized in the training corpus
var text = dataset.generate(100, startWithCapitalNGram);

The capitalized option allows you to prevent starting the generated text in the middle of a sentence, if the training data is in such a format.

To generate a single complete sentence, use the sentence() function, which takes in a requested line length, and optionally, a variance in this line length.

var s = dataset.sentence(lineLength, lineLengthVariance);

Ensuring originality

To check whether or not a segment of generated text has accidentally copied the training corpus word-for-word, the checkOriginality function can be called:

dataset.checkOriginality("Is this string in the training corpus?");

Manually interacting with dataset

If you're looking for more direct interaction with a training set, you can use getPossibilities to get all the possible words that follow a given gram

dataset.getPossibilities(['words', 'that', 'follow', 'this']);

Or if you want to manually add an entry to the dataset, you can use updateGram

dataset.updateGram(['manually', 'added'], 'ngram');

which will add a new ngram or update a previous one.