A general purpose markov chain generator for Node and the browser
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).
- Simple API that's easy to customize
- Chains can be serialized to and hydrated from JSON
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' } ]
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.
npm install --save markov-chains
import Chain from 'markov-chains';
const chain = new Chain(/* corpus: Array<Array<any>> */);
var Chain = require('markov-chains').default;
var chain = new Chain(/* corpus: Array<Array<any>> */);
Coming Soon
Pull requests are always welcome!
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 |
markov-chains
uses tape
for testing.
To run the tests, just run npm test
at the command line.
markovify
- The excellent python library that inspiredmarkov-chains
markovchain
general-markov
markov
markov-chains
is licensed under the MIT License.
For details, please see the LICENSE
file.