Markov chain library for Node.js
An implementation of stationary Markov chains with optional memory.
The main goal of markov-chainer
is supporting the creation of simple chat bots.
The library is available on npm as markov-chainer
:
npm install markov-chainer
- Small API: at most 6 new methods to learn
- Seeding: model can grow after instantiation
- On topic: responses are often related to input
- JSON states: states can be any JSONable data type
- Serialisation: import/export a model from/to JSON
To create a new model from scratch, first provide a corpus, that is, a collection of example runs for your Markov process:
/* example of corpus: lines is sample runs
elements are valid states of your process */
const corpus = [
['Hello', 'world', 'of', 'Markov', 'chains'],
['Lines', 'are', 'a', 'sample', 'of', 'run'],
['Each', 'value', 'is', 1, 'valid', 'state'],
['This', 'can', 'be', 'any', 'JSON', 'type'],
['It', 'may', 'be', 'even', { a: 'object' }]
]
Then, create a Chain
object using the main constructor:
const { Chain } = require('markov-chainer')
// takes an object with settings
// one of which is called `corpus`
const chain = new Chain({ corpus })
Finally, request runs from your chain:
// `res` contains three lists of states:
// the `previous`, `initial`, and `next`
// states taken by the Markov chain run
const res = chain.run()
console.log(res)
Possible output:
[ [], [], [ 'It', 'may', 'be', 'any', 'JSON', 'type' ] ]
Runs can also begin at a given state:
console.log(chain.run({ tokens: ['sample'] }))
Possible output:
[ [ 'Lines', 'are', 'a' ], [ 'sample' ], [ 'of', 'Markov', 'chains' ] ]
API documentation can be found at vccortez.github.io/markov-chainer.
markovify
- Python implementation and main inspiration for this librarymarkov-chains
- JavaScript library inspired bymarkovify
with browser supportnode-markov
The vccortez/markov-chainer
project is licensed under the MIT licence.