Home

Awesome

markovchain

markovchain generates a markov chain based on text passed into it.

build status

Requirements

Install

Example

var MarkovChain = require('markovchain')
  , fs = require('fs')
  , quotes = new MarkovChain(fs.readFileSync('./quotes.txt', 'utf8'))

console.log(quotes.start('The').end(5).process())

This will read a file, "quotes.txt", generate a word chain, then attempt to generate sentences starting with the word

"The" that are 5 words long, and then output to console.

Methods

constructor([string: content][, function: normalizeFn])

content: the content that you want passed into the markov chain normalizeFn: the function to apply to every word (generally to clean it up, e.g. removing commas and other non-letter words)

start([string: str|function: func])

The start method can take in either a string str, in which case it will look to use that word to start the sentence.

If you instead pass a function func with one parameter, wordList, you will be given the entire list of word chains in which you can decide what words to use to start a sentence. For example, you can generate sentences based on the number of times a word occurs, or if the word starts with a capital letter.

Example:


var useUpperCase = function(wordList) {
  var tmpList = Object.keys(wordList).filter(function(word) {
    return word[0] >= 'A' && word[0] <= 'Z'
  })
  return tmpList[~~(Math.random()*tmpList.length)]
}

console.log(quotes.start(useUpperCase).end().process())

end([string: str|function: func|integer: int])

The end method can take a String, Integer, or Function

Example:

// same as passing value, 5 to end function
var stopAfterFiveWords = function(sentence) {
  return sentence.split(" ").length >= 5
}

console.log(quotes.start(useUpperCase).end(stopAfterFiveWords).process())

parse(string: content[, parseBy:regex/string])

The parse function adds more content to the markov chain. This allows you to content later on, rather than needing all the next when you instantiate the markov chain.

If you pass a second parameter, parseBy, it specifies how the content will be parsed into sentences (which are then further parsed down into words). By default parseBy will parse into newlines, periods, and question marks.

Example:

var m = new MarkovChain('some text here')

m.parse('add additional text')

console.log(m.parse('more and more text').end(5).process())

Author

CHANGELOG

1.0.0

NOTE

0.0.6 IS PROBABLY THE LAST VERSION WITH THIS API.

0.0.6

0.0.5

0.0.4

0.0.3

0.0.2

0.0.1

LICENSE