This text generator works by creating a Markov chain from a given corpus. This generator uses the following algorithm:
- Create an empty hash
- For each word in the provided corpus:
- Make that word a key in the hash
- Place each word that comes after it in the corpus into an array, then map that array to the original word
Consider the sentence 'John had many cats and dogs and fishes'. After this program runs, it returns an object that looks as follows:
{John => [had], had => [many], many => [cats], cats => [and], and => [dogs, fishes], dogs => [and], fishes => []}
Using this object, we can generate random text through the following algorithm
- Pick a random key from the hash
- Append this key to our sentence
- Choose a random word from the array mapped to this key
- Make this word our new key and return to step 2
Note that we are not limited to using single words as keys. This generator can also use bigrams (two words) and trigrams (three words) as keys.