A simplistic Markov chain text generator.
Give it an array of strings, and it will output a randomly generated string.
This module was created for the Twitter bot @BelgicaNews.
This module makes use of ES6 features.
npm install --save markov-strings
const Markov = require('markov-strings').default
// or
import Markov from 'markov-strings'
const data = [/* insert a few hundreds/thousands sentences here */]
// Build the Markov generator
const markov = new Markov(data, { stateSize: 2 })
markov.buildCorpus()
const options = {
maxTries: 20, // Give up if I don't have a sentence after 20 tries (default is 10)
prng: Math.random, // An external Pseudo Random Number Generator if you want to get seeded results
filter: (result) => {
return
result.string.split(' ').length >= 5 && // At least 5 words
result.string.endsWith('.') // End sentences with a dot.
}
}
// Generate a sentence
const result = markov.generate(options)
console.log(result)
/*
{
string: 'lorem ipsum dolor sit amet etc.',
score: 42,
tries: 5,
refs: [ an array of objects ]
}
*/
Create a generator instance.
string[] | Array<{ string: string }>
data
is an array of strings (sentences), or an array of objects. If you wish to use objects, each one must have a string
attribute. The bigger the array, the better and more various the results.
Examples:
[ 'lorem ipsum', 'dolor sit amet' ]
or
[
{ string: 'lorem ipsum', attr: 'value' },
{ string: 'dolor sit amet', attr: 'other value' }
]
{
stateSize: number
}
The stateSize
is the number of words for each "link" of the generated sentence. 1
will output gibberish sentences without much sense. 2
is a sensible default for most cases. 3
and more can create good sentences if you have a corpus that allows it.
This function must be called to build the corpus for Markov generation.
It will iterate over all words from your data
parameter to create an internal optimized structure.
Since .buildCorpus()
can take some time (it loops for each word of each string), a non-blocking variant .buildCorpusAsync()
is conveniently available if you need it.
Returns an object of type MarkovResult
:
{
string: string, // The resulting sentence
score: number, // A relative "score" based on the number of possible permutations. Higher is "better", but the actual value depends on your corpus
refs: Array<{ string: string }>, // The array of references used to build the sentence
tries: number // The number of tries it took to output this result
}
The refs
array will contain all objects that have been used to build the sentence. May be useful to fetch some meta data or make some stats.
Since .generate()
can potentially take several seconds or more, a non-blocking variant .generateAsync()
is conveniently available if you need it.
{
maxTries: number // The max number of tentatives before giving up (default is 10)
prng: Math.random, // An external Pseudo Random Number Generator if you want to get seeded results
filter: (result: MarkovResult) => boolean // A callback to filter results (see example above)
}
- Add an optionnal
prng
parameter at generation to use a specific Pseudo Random Number Generator
- Dependencies update
- Refactoring with breaking changes
- The constructor and generator take two different options objects
- Most of generator options are gone, except
filter
andmaxTries
- Tests have been rewritten with jest, in TypeScript
- Code rewritten in TypeScript. You can now
import MarkovGenerator from 'markov-strings'
- New
filter()
method, thanks @flpvsk
- Dependencies update
- Updated README. Version bump for npm
- Fixed an infinite loop bug
- Performance improvement
- Updated README example
- Removed a useless line
- New feature: the generator now accepts arrays of objects, and tells the user which objects were used to build a sentence
- Fixed all unit tests
- Added a changelog
npm test