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

node-markov-generator

v0.4.3

Published

Simple text generator for Node.js that uses a Markov chain

Downloads

21

Readme

node-markov-generator

This simple generator emits short sentences based on the given text corpus using a Markov chain.

Using a what?

To put it simply - it works kinda like word suggestions that you have while typing messages in your smartphone. It analyzes which word is followed by which in the given corpus and how often. And then, for any given word it tries to predict what the next one might be.

Usage

Basic usage

import {TextGenerator} from 'node-markov-generator';

/* array of your strings which will be used to "train" the generator */
const corpus = ['This is my text.', 'Markov chains are great', 'Yet another string! This is just awesome.'];
const generator = new TextGenerator(corpus);

const result = generator.generateSentence();
console.log(result);

Here you create an instance of TextGenerator passing an array of strings to it - it represents your text corpus which will be used to "train" the generator. The more strings/sentences you pass, the more diverse results you get, so you would better pass like hundreds of them - or even more!

TextGenerator.generateSentence() returns a string or null in case it was unable to generate a sentence.

Reading the text corpus from an external file

If you have your texts in an external file, you can pass the path to it as an argument for TextGenerator's constructor like this:

import * as path from 'path';
import {TextGenerator} from 'node-markov-generator';

// in this example my texts are located in corpus.txt
const corpusPath = path.join(__dirname, 'corpus.txt');
const generator = new TextGenerator(corpusPath);

Getting result as a raw array of strings

If you do not need your result to look like a sentence (i.e. a string starting with a capital and ending with a '.'), consider using TextGenerator.generate() method instead of generateSentence(). It returns the result sentence as an array of words - or null if the generation process failed.

Then you might want to join the items or apply any other transformation you like.

Options

Both TextGenerator.generateSentence() and TextGenerator.generate() methods accept options parameter that you might use to control the generation process. You can use the following optional parameters:

  1. wordToStart - which word should be used to start the Markov chain - and therefore the result sentence. If unspecified, a random word is used;
  2. minWordCount - minimum number of words that are supposed to be in the generated sentence. Default is 7;
  3. maxWordCount - maximum number of words that are supposed to be in the generated sentence. Default is 20;
  4. retryCount - since the generation process is rather probabilistic, sometimes the generator might not be able to get a result on the first try, so it may need some more attempts. Default is 100;
  5. contextUsageDegree - a number from 0 to 1 To avoid diving into details, this parameter defines the degree of similarity between the generated sentences and the sentences in the source text corpus. The less the number is, the more nonsence sentences you get. Default is 0.5.

In case you want to specify any of these parameters, do it like this:

const result = generator.generateSentence({
    wordToStart: 'word',
    minWordCount: 5,
    contextUsageDegree: 0.75
});

Credits

regexpu is used for transpiling regular expressions with unicode property escapes into good old and nodejs8-compatible ES5 format.