Markov Chain Sentence Generator (with Sh*tposting capabilities)
Tags: node, javascript, markov, algorithms
Published: 2020-07-10 20:30:17

In the look for a challenge I stumbled upon the pro/g/ramming challenges v4.0 image. And I loved it, I think it has some legit great challenges that I want to make no matter how much it takes, just for the sake of learning.

![Popular image with around 150 programming challenges to develop.][1]

Pro/g/ramming challenges v4.0

The first challenge that took mi attention was the Markov chain sentence generator, maybe is because that is one of the first challenges, maybe was the interesting name that I've never heard of before. I don't really know why but it get my attention, and I dived directly to the challenge.

![Russian mathematician Andrey Markov][2]

Russian mathematician [Andrey Markov][3]

What is a Markov chain? I thought to myself. And Medium solved my question with [this story][14], which I recommend to read first. From now on, I will assume that you read the story or know what a Markov chain is and how it works. Also the program I built is based on the code of the mentioned story. But I rewrote it on JavaScript, and added some interesting changes.

The action plan was very simple:

1. First thing is splitting the text. And deleting empty strings, generated by blank lines on text. Then separate words array in two different arrays, one of regular words and another to words before a full stop. (The last words).
2. Than I need to pick a random word as initial word, to start constructing the sentence with n-grams. I pick one word, add it to the sentence array, add the next word to it to the sentence array, and pick the third word as the next initial word.
3. Make adjustments so it can take up to three words next to the initial word.
4. Add a method for constructing a paragraph, calling inside that method multiple times the sentence generator method, and join all the sentences with an "n".
5. Add a method for constructing a essay, where inside will be called multiple times the paragraph generator method, and join all the paragraphs with an "nn".

Also, as a bonus mentioned in the challenges image, add sh*tposting capabilities.

If you want to know every detail of how the program developed, I will leave the [code here.][4]

* * *

# Issues.

The first issue that I ran into, was that sentences were repeating themselves, as if they looped.

![Repeated sentences](https://dev-to-uploads.s3.amazonaws.com/i/821zitcukyxhsi7zw4m4.png)

Quickly, I discovered the issue was on the sentence maker method. And it happened because the function to find the initial word position in the array always picked the first match of the word as its position.

The solution was to grab all the matches, and selecting the initial word position randomly.

```js
_getNextWords(sentence) {
  let index;
  if (this.currentWord !== "") {
    let indexes = [];
    for (let i = 0; i < this.allWords.length; i++) {
      if (this.allWords[i] === this.currentWord) indexes.push(i);
    }

    index = indexes[Math.floor(Math.random() * indexes.length)];
  } else {
    index = this._randomIntFromInterval(1, this.allWords.length - 1);
  }

  let wordsToBePushed = this._randomIntFromInterval(1, 3);

  if (index < this.allWords.length - wordsToBePushed) {
    for (let loopIndex = 0; loopIndex < wordsToBePushed; loopIndex++) {
      sentence.push(this.allWords[index + loopIndex].toLowerCase());
    }

    this.currentWord = this.allWords[index + wordsToBePushed];
  } else {
    sentence.push(this.allWords[index].toLowerCase());
    this.currentWord = "";
  }

  return sentence;
}
```

As you can see, if the currentWord (initial word) isn't a empty string, it saves all the indexes, and select one randomly. Then it saves the next words into the sentence array.

Another issue I ran into was the sh*tposting capabilities, I was dry at the moment and didn't know what to do. Actually, I did split it into a different class, and the best idea I had was to generate the sentence backwards…

I took the currentWord (initial word) and added the words before it to the sentence array, instead of the words after. Also modified the sentence to LoOk LiKe ThIs.

![Essay generated with Markov chains][5]

Markov chains essay generation.

![Shitpost Essay generated with Markov chains][6]

Markov chains essay generation with sh*tpost capabilities.

Maybe it was a little lazy on my part, but I couldn't think of a better idea. I encourage you to [contribute][15] if you have a better sh*tposting idea. :)

Thanks for reading! What challenge should I take now? I want to [go slowly,][7] no Bootloader pls.

[1]: https://miro.medium.com/max/7680/1*5KQlCmSrE8V13p0IE-oeUA.png
[2]: https://miro.medium.com/max/872/0*18U4lnR7dFDiA0Ku.jpg
[3]: https://en.wikipedia.org/wiki/Andrey_Markov
[4]: https://github.com/pepetorres1998/markov-chain-sentence-generator
[5]: https://miro.medium.com/max/2856/1*Nbo_St1apGEtiwzaTpveVw.png
[6]: https://miro.medium.com/max/2932/1*5IQ8LWhyAz-OY0t0xFqbBA.png
[7]: https://www.youtube.com/watch?v=LU7nE1e8WbE