DEV Community

Jose Antonio Torres Garibay
Jose Antonio Torres Garibay

Posted on • Originally published at Medium

Markov Chain Sentence Generator (with Sh*tposting capabilities)

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.

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

Russian mathematician Andrey Markov

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.


Issues.

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

Repeated sentences

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.

_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

Markov chains essay generation.

Shitpost Essay generated with Markov chains

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, no Bootloader pls.

Top comments (0)