Clarissa Pace

Experimenting with Markov Code

https://thecodingtrain.com/challenges/42-markov-chain-name-generator

  1. I started with the example from the Markov Chain Name Generator sketch. I wanted to create a recipe generator, so I replaced the unicorn text with a string of random recipes.
  2. I then adjusted the variable order to balance the randomness (I think the lower the variable the more random the results become?)

I made a for loop to loop over text n-grams of length order

for (var i = 0; i <= txt.length - order; i++) {
  var gram = txt.substring(i, i + order);

  if (!ngrams[gram]) {
    ngrams[gram] = [];
  }
  ngrams[gram].push(txt.charAt(i + order));
}

Each gram is added to each ngram as a key

Result of adjusted code so far:

Screenshot 2024-11-06 at 21.34.23.png

I needed to adjust markovIt to specify a different starting point so the recipes would have more variety. I also increased the maximum length so the recipe wouldn’t get cut off.

Screenshot 2024-11-06 at 21.40.00.png

I then came across the issue of it cutting off the start of sentences, also it was just so random and wasn’t really what I had in mind. I increased the order nnumber to see if that would help with the structure of the sentences more.

I also added this to improve readability using line breaks: