Skip to content

vccortez/markov-chainer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

56 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

markov-chainer

Markov chain library for Node.js

npm version

An implementation of stationary Markov chains with optional memory. The main goal of markov-chainer is supporting the creation of simple chat bots.

Installation

The library is available on npm as markov-chainer:

npm install markov-chainer

Features

  • 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

Usage

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

API documentation can be found at vccortez.github.io/markov-chainer.

Acknowledgements

Licence

The vccortez/markov-chainer project is licensed under the MIT licence.