# markov-text

> A simple procedural text generator implementing Markov chains

Latest version **0.1.7** (published 2019-01-07) · WTFPL license · 0 weekly downloads

## Install

```sh
npm install markov-text
pnpm add markov-text
yarn add markov-text
bun add markov-text
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.7 |
| Published | 2019-01-07 |
| First published | 2019-01-03 |
| Weekly downloads | 0 |
| License | WTFPL |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 55.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 7 |
| Author | Vilva |
| Maintainers | vilvadot |
| Keywords | markov, markov chain, procedural text |

## Links

- npm: https://www.npmjs.com/package/markov-text
- Repository: https://github.com/vilvadot/markov-text
- Homepage: https://github.com/vilvadot/markov-text#readme
- Issues: https://github.com/vilvadot/markov-text/issues
- npm.io page: https://npm.io/package/markov-text

## Dependencies (2)

- [chalk](https://npm.io/package/chalk.md) ^2.4.1
- [lodash](https://npm.io/package/lodash.md) ^4.17.11

## Recent versions

- 0.1.7 (latest) — 2019-01-07
- 0.1.4 — 2019-01-07
- 0.1.3 — 2019-01-03
- 0.1.2 — 2019-01-03
- 0.1.1 — 2019-01-03
- 0.1.0 — 2019-01-03

## README

Markov Text [![Build Status](https://travis-ci.org/vilvadot/markov-text.svg?branch=master)](https://travis-ci.org/vilvadot/markov-text)
=========

A simple procedural text generator implementing [Markov chains](http://setosa.io/ev/markov-chains/)

![Markov Chain](https://media.giphy.com/media/gH6I5QWnIZjy0/giphy.gif)

## Installation

    npm install markov-text


## Usage
```js
    var Markov = require('markov-text');

    const trainingText = 'Lorem ipsum dolor sit ammet'

    options = {...}
    
    const loremGenerator = new Markov(options) // Setup generator
    
    loremGenerator.seed(trainingText) // Seed chain with "training" text

    const generatedText = loremGenerator.generate(5) // Set length of the generated output.
```

| Method    | Arguments             | Returns        | Description                                                                                                                                                                   |
|-----------|-----------------------|----------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|  seed     | trainingText<String>  |        -       |  Seeds the generator with the training text. The generator splits the text into nGrams (pieces of n-characters or n-words length, depending on the mode)analog to chainlinks. |
|  generate | outputLength<Integer> | output<String> | Returns the generated text of the specified length. (Length is in ngrams not in charcters/words)                                                                              |

## Options

You can pass in an options object when instancing the generator that accepts the following options:

| Property |   Type  |         Options        | Default    | Description                                                                                                                                                                      |
|:--------:|:-------:|:----------------------:|------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|   order  | integer |           1-n          |     no     | Specifies the length of the ngrams (chainlinks). Longer chainlinks will produce more coherent text but less 'creativity'                                                         |
|   mode   |  string | 'single' | 'multiple'  | 'multiple' |  Single mode will generate single words and will use training text as single independent words. Multiple mode will generate sentences and will use training text as word blocks. |

## Debuggin

You can view the chain building step by setting up the enviroment variable DEBUG_CHAIN to true

    DEBUG_CHAIN=true node myTextGenerator

## Examples

Provided are two examples you can run using:

#### Metamorphosis

![metamorphosis-text](https://user-images.githubusercontent.com/8507571/50779400-819b0b80-12a0-11e9-960d-9900ccacf6b4.gif)

  Uses a excerpt of [Franz Kafka's Metamorphosis](http://www.gutenberg.org/ebooks/5200)  to generate sentences of the desired length.

    npm run example:metamorphosis


---

#### Lotr

![lotr-text](https://user-images.githubusercontent.com/8507571/50779159-ed30a900-129f-11e9-93b9-f06c035e5f02.gif)

  Uses all the names from [characters of LOTR](https://en.wikipedia.org/wiki/List_of_Middle-earth_characters) to generate a new one

    npm run example:lotr


  _Copyright for training texts is owned by their respetive authors and is not protected by the license of this library_

## Tests

    npm test
    
## Credit

This is mainly my best shot at implementating in Javascript what is explained in this great series by [starbeamrainbowlabs](https://starbeamrainbowlabs.com/blog/article.php?article=posts/236-Markov-Chain-Part-1-N-Grams.html)

---
_Source: https://npm.io/package/markov-text · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
