https://github.com/vilvadot/markov-text
⛓ A simple procedural text generator implementing Markov chains
https://github.com/vilvadot/markov-text
markov-chain procedural-generation procgen text-generation
Last synced: 7 months ago
JSON representation
⛓ A simple procedural text generator implementing Markov chains
- Host: GitHub
- URL: https://github.com/vilvadot/markov-text
- Owner: vilvadot
- License: wtfpl
- Created: 2019-01-02T22:00:51.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-01-08T19:28:17.000Z (about 7 years ago)
- Last Synced: 2025-07-31T18:40:03.108Z (8 months ago)
- Topics: markov-chain, procedural-generation, procgen, text-generation
- Language: JavaScript
- Homepage:
- Size: 550 KB
- Stars: 7
- Watchers: 0
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Markov Text [](https://travis-ci.org/vilvadot/markov-text) [](https://coveralls.io/github/vilvadot/markov-text)
=========
A simple procedural text generator implementing [Markov chains](http://setosa.io/ev/markov-chains/)

## 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 | - | 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 | output | 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

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

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)