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-simple

v0.0.111

Published

yet another markov chain tool for text, very simple, no tokenization; degree-n memos predict individual characters

Downloads

5

Readme

markov-simple

yet another markov chain tool for text, very simple, no tokenization; degree-n memos predict individual characters; includes utils to predict probabilities.

Installation

npm i markov-simple

Usage

var MarkovChain = require('markov-simple').MarkovChain;

//a story about a cat who loves markov chains

const inputText = `Once upon a time in March, in a cozy little house on the outskirts of town, there lived a cat named Whiskers. Whiskers was a curious cat who loved to explore and discover new things. One day, while Whiskers was taking a nap on the windowsill, he overheard his owner talking about a new algorithm called "Markov Chains." Intrigued, Whiskers set out to learn more about this fascinating concept.
Whiskers spent hours reading books and articles about Markov Chains, and he became fascinated with the idea of using probability to predict the future. He learned that Markov Chains could be used to generate new sentences, music, and even images.
Whiskers was so fascinated by Markov Chains that he decided to create his own Markov Chain generator. He spent many long nights programming and tweaking his algorithm until it was perfect.
As he generated new phrases and sentences using his Markov Chain algorithm, Whiskers felt a sense of satisfaction that he had never felt before. He loved the way that the algorithm was able to take a simple input and generate something entirely new and unpredictable.
Eventually, Whiskers became so good at generating phrases and sentences using his Markov Chain algorithm that he started using it to communicate with his owner. He would generate random phrases and meows that his owner would try to interpret, and they would spend hours playing this game.
Whiskers continued to use his Markov Chain algorithm to explore and discover new things. He was constantly amazed at the new and unpredictable things that the algorithm could create. In the end, Whiskers realized that he loved Markov Chains more than anything else in the world, and he was grateful to have discovered this fascinating algorithm.`;

var markovOrder = 3; //number of letters per "record"
const markov = new MarkovChain(markovOrder);
var streamingMode = false; //default false; if true, prepends the end of the last training string [the last markovOrder-1 chars] to a copy of the input text before using it to train the transitions matrix further. this prevents us losing strings that 'bridge the chunks' in our training set
markov.train(inputText.toLowerCase(), streamingMode);

var startContext = "mar"; //we want strings that start with "a " [length of context must be same as markovOrder!] - default null
var lengthOfOutput = 50;
var nOutputsToGenerate = 3;
var generatedTextSingle = markov.generate(lengthOfOutput, startContext);
var generatedTextMulti = markov.generateMulti(lengthOfOutput, nOutputsToGenerate, startContext);
var nSteps = 3;
var probOfChange = markov.probabilityFromTo("mar", "v", nSteps); //0.9
console.log({generatedTextSingle, probOfChange});

// {
//     generatedTextSingle: 'markov chains talking his felt and discover felt a',
//     probOfChange: 0.9 //notice how "mar" occurs 10 times in the input text -- once "march" + 9 times "markov" -- 90% of the time "v" is within 3 steps of "mar" 
// }

// console.log(generatedTextMulti); //array of random outputs

// console.log(markov.transitions); //markov 'transitions' memory order-n mapped to individual chars

// {
//     onc: { e: 2 },
//     nce: { ' ': 1, p: 1, s: 3 },
//     'ce ': { u: 1 },
//     'e u': { p: 1, s: 1 },
//     ' up': { o: 1 },
//     upo: { n: 1 },
//     pon: { ' ': 1 },

Similar Tools

stonks