Giter Site home page Giter Site logo

node-markov-generator's Introduction

node-markov-generator

This simple generator emits short sentences based on the given text corpus using a Markov chain.

Using a what?

To put it simply - it works kinda like word suggestions that you have while typing messages in your smartphone. It analyzes which word is followed by which in the given corpus and how often. And then, for any given word it tries to predict what the next one might be.

Usage

Basic usage

import {TextGenerator} from 'node-markov-generator';

/* array of your strings which will be used to "train" the generator */
const corpus = ['This is my text.', 'Markov chains are great', 'Yet another string! This is just awesome.'];
const generator = new TextGenerator(corpus);

const result = generator.generateSentence();
console.log(result);

Here you create an instance of TextGenerator passing an array of strings to it - it represents your text corpus which will be used to "train" the generator. The more strings/sentences you pass, the more diverse results you get, so you'd better pass like hundreds of them - or even more!

TextGenerator.generateSentence() returns a string or null in case it was unable to generate a sentence.

Reading the text corpus from an external file

If you have your texts in an external file, you can pass the path to it as an argument for TextGenerator's constructor like this:

import * as path from 'path';
import {TextGenerator} from 'node-markov-generator';

// in this example my texts are located in corpus.txt
const corpusPath = path.join(__dirname, 'corpus.txt');
const generator = new TextGenerator(corpusPath);

Getting result as a raw array of strings

If you do not need your result to look like a sentence (i.e. a string starting with a capital and ending with a '.'), consider using TextGenerator.generate() method instead of generateSentence(). It returns the result sentence as an array of words - or null if the generation process failed.

Then you might want to join the items or apply any other transformation you like.

Options

Both TextGenerator.generateSentence() and TextGenerator.generate() methods accept options parameter that you might use to control the generation process. You can use the following optional parameters:

  1. wordToStart - which word should be used to start the Markov chain - and therefore the result sentence. If unspecified, a random word is used;
  2. minWordCount - minimum number of words that are supposed to be in the generated sentence. Default is 7;
  3. maxWordCount - maximum number of words that are supposed to be in the generated sentence. Default is 20;
  4. retryCount - since the generation process is rather probabilistic, sometimes the generator might not be able to get a result on the first try, so it may need some more attempts. Default is 100;
  5. contextUsageDegree - a number from 0 to 1 To avoid diving into details, this parameter defines the degree of similarity between the generated sentences and the sentences in the source text corpus. The smaller the number is, the more nonsense sentences you get. Default is 0.5.

In case you want to specify any of these parameters, do it like this:

const result = generator.generateSentence({
    wordToStart: 'word',
    minWordCount: 5,
    contextUsageDegree: 0.75
});

Credits

regexpu is used for transpiling regular expressions with unicode property escapes into good old and nodejs8-compatible ES5 format.

node-markov-generator's People

Contributors

mixaildudin avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.