#Markov-strings
A simplistic Markov chain text generator.
Give it an array of strings, and it will output a randomly generated string.
##Prerequisites
This module makes use of ES6 features.
##Installing
npm install --save markov-strings
##Usage
let Markov = require('markov-strings');
let data = [];
let options = {
maxLength: 140,
minWords: 10,
minScore: 25,
};
let markov = new Markov(data, options);
let tweets = [];
for (let i = 0; i < 10; i++) {
tweets.push(markov.generateSentence());
}
let shorterTweet = markov.generateSentence({
maxLength: 140-24
});
shorterTweet += ' https://github.com/scambier/markov-strings';
console.log(shorterTweet);
API
Markov(data, [options])
Create a generator instance.
data
Type: array
data
is an array of strings (sentences). The bigger the array, the better and more various the results.
options
Type: object
You can provide options during the generator instantiation, and/or while calling generateSentence()
.
The options
object will alter the quality, length, etc. of the generated sentences.
Options given to generateSentence()
overwrite those given during instantiation.
It can be useful if you wish to generate multiple sentences with slight variations each time.
stateSize
Type: integer
Default: 2
The number of words for each state.
1
will output gibberish sentences without much sense.
2
is a sensible default.
3
and more could create good sentences, at the expense of randomness. You'll need a good corpus, though.
maxLength
Type: integer
Default: 0
Maximum characters.
minWords
Type: integer
Default: 5
Minimum number of words.
maxWords
Type: integer
Default: 0
Maximum number of words.
minScore
Type: integer
Default: 0
Each generated sentence will be associated to a score. The highest this score, the more random the sentence should be.
A good minScore
value totally depends of your corpus, and the number of words of the sentence, so you'll have to try yourself.
markov.generateSentence([options])
Generate a random sentence.
options
Type: object
If set, these options will take precedence over those set in the constructor.
Todo
The generator should return a Promise, to not hang the thread.
Running the tests
npm test