4.0 KiB
markov-chains
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).
Table of Contents
Features
- Simple API that's easy to customize
- Chains can be serialized to and hydrated from JSON
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' } ]
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>> */);
API Reference
Coming Soon
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.
See Also
markovify
- The excellent python library that inspiredmarkov-chains
markovchain
general-markov
markov
License
markov-chains
is licensed under the MIT License.
For details, please see the LICENSE
file.