General markov chains for javascript
Switch branches/tags
Clone or download

README.md

markov-chains

A general purpose markov chain generator for Node and the browser

npm version


markov-chains is a simple, general purpose markov chain generator written in JavaScript, and designed for both Node and the browser.

Unlike many markov chain generators in JavaScript, markov-chains can handle and generate non-textual data just as easily as it handles text (see example).

Table of Contents


Features

  • Simple API that's easy to customize
  • Chains can be serialized to and hydrated from JSON

Back to Top ↑


Example

import Chain from 'markov-chains';

// our states (an array of arrays)
const states = [
  // week 1
  [
    { temp: 'hot',  weather: 'sunny' },
    { temp: 'hot',  weather: 'cloudy' },
    { temp: 'warm', weather: 'cloudy' },
    { temp: 'warm', weather: 'cloudy' },
    { temp: 'warm', weather: 'rainy' },
    { temp: 'cool', weather: 'cloudy' },
    { temp: 'warm', weather: 'sunny' },
  ],

  // week 2
  [
    { temp: 'warm', weather: 'sunny' },
    { temp: 'warm', weather: 'cloudy' },
    { temp: 'warm', weather: 'cloudy' },
    { temp: 'warm', weather: 'sunny' },
    { temp: 'hot',  weather: 'sunny' },
    { temp: 'hot',  weather: 'sunny' },
    { temp: 'warm', weather: 'sunny' },
  ],

  // etc.
];

// build the chain
const chain = new Chain(states);

// generate a forecast
const forecast = chain.walk();

console.log(forecast);

// Example output:
//
// [ { temp: 'warm', weather: 'sunny' },
//   { temp: 'warm', weather: 'cloudy' },
//   { temp: 'warm', weather: 'rainy' },
//   { temp: 'cool', weather: 'cloudy' },
//   { temp: 'warm', weather: 'sunny' } ]

Back to Top ↑


Installation & Usage

Requirements

markov-chains relies on Maps and Generators, which are available natively in Node v4.0 and above, and in modern versions of many browsers.

For a list of JavaScript environments that support these features, see the ECMAScript Compatability Table.

Downloading

npm install --save markov-chains

Usage (ES6+)

import Chain from 'markov-chains';
const chain = new Chain(/* corpus: Array<Array<any>> */);

Usage (CommonJS)

var Chain = require('markov-chains').default;
var chain = new Chain(/* corpus: Array<Array<any>> */);

Back to Top ↑


API Reference

Coming Soon

Back to Top ↑


Contributing

Pull requests are always welcome!

Building

The following npm scripts are available for use during development:

Command Use to...
npm run clean Remove the dist/ files
npm run lint Lint the files in src/
npm run build Transpile the code with babel

Tests

markov-chains uses tape for testing.

To run the tests, just run npm test at the command line.

Back to Top ↑


See Also

Back to Top ↑


License

markov-chains is licensed under the MIT License.

For details, please see the LICENSE file.

Back to Top ↑