mc

markov-chains

General markov chains for javascript

Showing:

Popularity

Downloads/wk

291

GitHub Stars

55

Maintenance

Last Commit

6yrs ago

Contributors

0

Package

Dependencies

1

License

MIT

Type Definitions

Tree-Shakeable

Yes?

Categories

Readme

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:

CommandUse to...
npm run cleanRemove the dist/ files
npm run lintLint the files in src/
npm run buildTranspile 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 ↑

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100
No reviews found
Be the first to rate

Alternatives

No alternatives found

Tutorials

No tutorials found
Add a tutorial