npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

markov-json

v1.1.7

Published

simplest markov chain reader/writer/exporter

Downloads

31

Readme

MARKOV-JSON

Maintenance status published on npm! Stability
Known Vulnerabilities Testing Status Test Coverage Maintainability


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 = require('markov-json');
  const twoMonths = require('./twoMonths.json');

  const tweeter = new Markov();

  const twoMonthsOfTweets = twoMonths
    .map(tweet => tweet.text)
    .join('. ');
  tweeter.train(twoMonthsOfTweets);

  tweeter.output('./map.json');

  console.log(tweeter.sentence(50));

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:

  import default as Markov from 'markov-json';
  // also supports `import Markov from 'markov-json';`
  // *and* `const Markov = require('markov-json');`
  const mkjs = new Markov();

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 = new Markov('./thatcrazymarkov.json');
  // or
  const mk2s = new Markov({ 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 = new Markov('lol_random.json',{ complexity: 0 });
  // or
  const mkjstronk = new Markov(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 = new Markov();
  arnkjs.train('Listen to the sound of my voice! Get to the chopper!');
  arnkjs.setComplexity(0.123);

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.train('some cool words');
  mkv.train(`I wanna train ${itSomeMore}`);

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.blob(NUMBER_OF_WORDS_YOUD_LIKE)

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.sentence(NUMBER_OF_SENTENCES_YOUD_LIKE)

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.sentences(20) ~= mk.sentence(20);
  mk.words(39) ~= mk.blob(39);

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:

  1. Add transpilation for ie11+, and add more node versions to .travis.yml