Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

markov-strings

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

markov-strings

A Markov string generator

  • 2.0.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Build Status Coverage Status npm version dep

Markov-strings

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

This module was created for the Twitter bot @BelgicaNews.

Prerequisites

This module makes use of ES6 features.

Installing

npm install --save markov-strings

Usage

const Markov = require('markov-strings')
// or
import Markov from 'markov-strings'

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

// Build the Markov generator
const markov = new Markov(data, { stateSize: 2 })
markov.buildCorpus()

const options = {
  maxTries: 20, // Give up if I don't have a sentence after 20 tries (default is 10)
  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 = markov.generate(options)
console.log(result)
/*
{
  string: 'lorem ipsum dolor sit amet etc.',
  score: 42,
  scorePerWord: 7,
  tries: 5,
  refs: [ an array of objects ]
}
*/

API

new Markov(data, [options])

Create a generator instance.

data
string[] | Array<{ string: string }

data is an array of strings (sentences), or an array of objects. If you wish to use objects, each one must have a string attribute. The bigger the array, the better and more various the results.

Examples:

[ 'lorem ipsum', 'dolor sit amet' ]
or
[ { string: 'lorem ipsum', attr: 'value' }, { string: 'dolor sit amet', attr: 'other value' } ]

options
{
  stateSize: number
}

The stateSize is the number of words for each state. 1 will output gibberish sentences without much sense. 2 is a sensible default. 3 and more can create good sentences if you have a corpus that allows it.

.buildCorpus()

This function must be called to build the corpus for Markov generation. It will iterate over all words from your data parameter to create an internal optimized structure.

Since .buildCorpus() can take some time, a non-blocking variant .buildCorpusAsync() is conveniently available if you need it.

.generate([options])

Returns an object of type:

{
  string: string,
  score: number,
  refs: Array<{ string: string }>,
  tries: number
}

The refs array will contain all objects that have been used to build the sentence. May be useful to fetch some meta data or make some stats.

options
{
  maxTries: number
  filter: (result: MarkovResult) => boolean
}

Changelog

2.0.0
  • Refactoring with breaking changes
  • The constructor and generator take two different options objects
  • Most of generator options are gone, except filter and maxTries
  • Tests have been rewritten with jest, in TypeScript
1.5.0
  • Code rewritten in TypeScript. You can now import MarkovGenerator from 'markov-strings'
1.4.0
  • New filter() method, thanks @flpvsk
1.3.4 - 1.3.5
  • Dependencies update
1.3.3
  • Updated README. Version bump for npm
1.3.2
  • Fixed an infinite loop bug
  • Performance improvement
1.3.1
  • Updated README example
  • Removed a useless line
1.3.0
  • New feature: the generator now accepts arrays of objects, and tells the user which objects were used to build a sentence
  • Fixed all unit tests
  • Added a changelog

Running the tests

npm test

Keywords

FAQs

Package last updated on 21 Nov 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc