MarkovChain

MarkovChain

A Markov chain.

Constructor

new MarkovChain(baseopt)

Source:
Create a new MarkovChain.
Example
// Create a new chain
const chain = new MarkovChain()

// Add text
chain.update('some text')

// Export it to string or to JSON
const str = chain.toString()
const json = chain.toJSON()

// Create new chains based on our saves
const fromStr = new MarkovChain(str)
const fromJSON = new markovChain(json)
Parameters:
Name Type Attributes Description
base MarkovChainResolvable <optional>
A saved chain from .toJSON or .toString.

Members

config :GenerationConfig

Source:
The default config to use for generation
Type:

corpus :Array.<Array.<string>>

Source:
The sentences learnt by the Markov chain.
Type:
  • Array.<Array.<string>>

dictionary :Array.<string>

Source:
A list of all the words learnt.
Type:
  • Array.<string>

Methods

contains(word) → {boolean}

Source:
Search if a word is in the corpus.
Example
const chain = new MarkovChain()

console.log(chain.contains('OwO')) // false

chain.update('hi OwO')

console.log(chain.contains('OwO')) // true
Parameters:
Name Type Description
word string The word to search for.
Returns:
Type
boolean

generate(configopt) → {string}

Source:
Generate a new sentence.
Example

Generate a sentence

// Create a Markov chain
const chain = new MarkovChain()

// Add some words to it
chain.update('Some words')
chain.update('Some more words')
chain.update('Some OwO')

// Generate a sentence
const sentence = chain.generate()
console.log(sentence)

// Generate a sentence starting with 'OwO'
const owo = chain.generate({ from: 'OwO' })
console.log(owo)

// Generate a sentence backward
const backwardOwO = chain.generate({ from: 'OwO', backward: true })
console.log(backwardOwO)
Parameters:
Name Type Attributes Default Description
config GenerationConfig <optional>
this.config The config to use for the generation.
Returns:
Type
string

toJSON() → {MarkovChainJSON}

Source:
Get a JSON version of the chain object.
Returns:
Type
MarkovChainJSON

toString() → {string}

Source:
Get a string version of the chain object.
Returns:
Type
string

update(sentence)

Source:
Add a sentence to the Markov chain.
Example
const chain = new MarkovChain()

chain.update('Some text')
Parameters:
Name Type Description
sentence string The sentence to add.