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-strings-db

v4.2.0

Published

A Markov string generator that utilizes a relational SQL database

Downloads

40

Readme

markov-strings-db

GitHub Workflow Status Coverage Status npm

A rewrite of scambier/markov-strings made to utilize a relational SQL database rather than an in-memory object. The goal is to reduce memory usage, disk reads/writes, remove batch corpus generation, and increase performance.

This should be mostly functionally compatible with markov-strings v3, however the API usage varies. This version supports importing data exports from markov-strings v3 for easy migration.


A simplistic Markov chain text generator. Give it an array of strings, and it will output a randomly generated string.

This rewrite was created for the Discord bot markov-discord.

Prerequisites

  • NodeJS 14+
  • The host app must be configured with a TypeORM-compatible database driver

Installing

  • npm i markov-strings-db
  • Setup a database driver (this project was tested with better-sqlite3)
  • Setup a data source config

Usage

import Markov from 'markov-strings-db';
import ormconfig from './ormconfig';
import { DataSource } from 'typeorm';

const data = [/* insert a few hundreds/thousands sentences here */];


// Instantiate the Markov generator
const markov = new Markov({ options: { stateSize: 2 }});

// If you have your own database you'd like to combine with Markov's, make sure to extend your data source options
const dataSourceOptions = await Markov.extendDataSourceOptions(ormconfig);
// Required: create a data source before using a markov instance. You only need to do this once.
const dataSource = new DataSource(dataSourceOptions);
await dataSource.initialize();

// If you have a non-default data source (you probably don't), pass it in here. Otherwise, setup() is called implicitly on any async function.
await markov.setup(dataSource);

// Add data for the generator
await markov.addData(data)

const generateOptions = {
  maxTries: 20, // Give up if I don't have a sentence after 20 tries (default is 10)

  // You may need to manually filter raw results to get something that fits your needs.
  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 = await markov.generate(generateOptions)
console.log(result)
/*
  {
    string: 'lorem ipsum dolor sit amet etc.',
    score: 42,
    tries: 5,
    refs: [ an array of objects ]
  }
*/

await markov.destroy();

API

Each function is documented via JSDoc and its types. Hover over a function in your IDE for details on it.