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

markov-chain-nlg

v1.0.3

Published

a simple markov chain text generator, with backoff

Downloads

14

Readme

markov-chain-nlg

A simple Markov Chain text generator developed in Node.JS.

installation

npm install markov-chain-mlg

usage

The code can be required by using require('markov-chain-mlg'). From there, any of the following methods, listed below, can be used to generate text.

testing

Run '''npm test''' to test the generator. This will train on a very small training set, and output the generated result.

DOCS

train

Usage: chain.train (data: Array of String, shouldBackoff: boolean)

Purpose: Trains the Markov Chain. The data should be various sentences the chain should be trained on. This must be called at least once before any text can be generated.

Arguments:

      data: The data to train the generator on. More data will provide more accurate results.

      shouldBackoff: A flag to determine if the generator should perform a backing off process or not. See BACKING OFF, which will be described in detail later.

Returns: None; work will be done in the chain's data.

trainTxt

Usage: chain.trainTxt (filepath: String, split: String, [shouldBackoff: boolean, [numWordsInChain: int]])

Purpose: Trains the Markov Chain Generator on data from a file. Split should be some value to split the text on; by default, will split on new lines.

Arguments:

      filepath: A path to the file to use for training.

      split: A character/characters to split the file data on. Defaults to '\n'

      shouldBackoff: A flag to determine if the generator should perform a backing off process or not. See BACKING OFF, which will be described in detail later. Defaults to true.

      numWordsInChain: The number of words on which to base the next word on. By default, this is 5.

Returns: None; work will be done in the chain's data.

generate

Usage: chain.generate(maxLength : int) : String

Purpose: Generates a new line of text.

Arguments:

      maxLength: The maximum number of words to be generated. This is an upper bound & the result may be shorter than this.

Returns: A string representing the sequence of words that was generated.

Backing Off

By default, the Markov Chain generator will determine the next word to be generated based on the previous 5 words generated. For example, if the current sequence is "This is an example result of the Markov", then the next word will be determined based on the sequence "example result of the Markov".

This generates more accurate results, but can often lead to copy-and-paste results - if only a single option can be found for the sequence, then it will choose that option. This is mediated by the Backing Off process, which will generate instead based off the last 4 words, last 3 words, ..., until a number of selections can be found.

In short, backing off will generate less accurate results but with a variety of results; not backing off will provide more accurate results at the risk of few variations.

Example

const MarkovChain = require('markov-chain-nlg');
MarkovChain.train (["Testing testing testing", "Even more testing", "A final test"], true);
console.log (MarkovChain.generate (10));
const MarkovChain = require('markov-chain-nlg');
MarkovChain.trainTxt ("some-path/some-text-file.txt", "\n");
MarkovChain.trainTxt ("some-path/some-other-text-file.txt", "\n");
console.log (MarkovChain.generate (150));