MARKOV-JSON
What is this?
A markov generator of 2 depth and variable complexity. It's made to be really really simple to use!
Why should I care?
It has a whopping zero deps. It's blisteringly fast, it's 100% tested, written in typescript (javascript with type safety), and keeps its state as a simple simple, easily manipulatable object.
It's super small and powerful. Two months of tweets is parsed, saved to disk as a re-usable json state map, and turned into random 50 sentences in less than 50ms in the following code snippet:
const Markov = ; const twoMonths = ; const tweeter = ; const twoMonthsOfTweets = twoMonths ; tweeter; tweeter; console;
It's also fully tested to be hideously accurate. When given Frankenstein, it outputs a distribution of characters including punctuation less than 5% off of the input novel when outputting 50,000 words, check out the test! This test that would qualify for nanowrimo is usually output in less than half a second!! (Check out what little was changed to make memoization happen and boost performance by double!, and then the PR that quintupled that :o)
Other libs just can't live up.
USEAGE:
Install it:
npm i markov-json #OR yarn add markov-json
Then use it:
; // also supports `import Markov from 'markov-json';` // *and* `const Markov = require('markov-json');` const mkjs = ;
It also accepts json objects output by itself someplace else! Just pass it a file location or the object, since all it is inside is an object!:
const mkjs = './thatcrazymarkov.json'; // or const mk2s = not: very: valid: lol: 1 );
Complexity:
You may also pass it the complexity as the second instantiation argument, thereby changing how rigid or random you'd like the sentence construction to be: (it supports numbers >= 0). This expresses itself as (#-of-sequences-of-these-two-words ^ complexity)
const mkjs = 'lol_random.json' complexity: 0 ; // or const mkjstronk = undefined complexity: 3 ;
You can even reset the complexity on the fly! The internal model is, and will remain compatible between any instance of mkjs, no matter what the complexity was set to when you're training it!
const arnkjs = ; arnkjs; arnkjs;
Then the cool stuff starts!
TRAINING:
Markov generators are only really good if they're trained. Training this one is super simple!
It's just a function!
Pass it text that vaguely looks like a language, and this package does the rest. It doesn't matter if you're passing it books, paragraphs, sentences, tweets, words, or gibberish! all you have to do is call it as many or few times as you'd like!:
mkv; mkv;
Markov-json is SMRT. It'll just work. Train it as many or as few times as you like. markov-json will just interpret everything, and continually add to its internal state object.
WHAT THEN!?
Then, all that's left is to get it to spit words back at you! It'll vaguely look like whatever you trained it on. Give it Shakespeare, and it'll shake a spear at you back.
mkv
Alternatively, you could ask it for sentences. If the text you give it doesn't contain anything resembling a sentence-end (including the one at the end of your training string input), it'll never output more than 2000 words.
mkv
Also, it has a little syntax sugar. You can get sentences and words and not get tripped up as easily because the methods are hard to remember!
mk ~= mk; mk ~= mk;
AFTERWARDS:
Finally, if you'd like to store your training model someplace, it's as simple as mkv.output('./filepath_someplace.json')
, or alternatively, you could instead get the state object by not passing it any arguments (like, say, if you wanted to send it someplace on the internets) var state = mkv.output()
.
BEHOLD!
The first sentences generated by my package after training on hamlet with absolutely no massaging done to the text taken from mit
ma.sentence();
'What, a dew!'
ma.sentence();'A king claudius we doubt it was sick almost to see you.'
ma.sentence(5);'So hallow\'d and i pray thee do mine ear that lives must hold my tongue. Hamlet not for thy asking? Marcellus. Horatio a man might be and the extravagant and bed-rid, for so. This portentous figure like a guilty thing to france and thy nighted colour off, colleagued with remembrance of our duty.'
TODO:
- Add transpilation for
ie11+
, and add more node versions to .travis.yml