MadLadz Movie Generator

by Sam Fereday

HTML

<div id="generated_title"></div>
<button id="generate">Generate</button>

CSS

body {
    font-size: 1.3em;
}

div {
    padding: 1em;
}

JavaScript

// http://stackoverflow.com/questions/4081662/explain-markov-chain-algorithm-in-laymans-terms
// http://pcg.wikidot.com/pcg-algorithm:markov-chain
var words = [
    "Gri", // These words have a 100% probability of appearing first, but never again
    "Sag",
    "Thal",
    "dim", // These following words have 100% chance to never appear first, but always after
    "ani",
    "elle",
    "duil",
    "dang"
];

// Now the actual code
var terminals = {};
var startwords = [];
var wordstats = {};

for (var i = 0; i < titles.length; i++) {

		// Split each title with spaces in it
		var words = titles[i].split(' ');
    
    // Assign last word of title to terminal (presumably aka last), this makes sure (I think) that we have a record of all the first words so they don't get used twice.
    // Since we might not have words longer than two barrels, we cater for this.
    if(words.length > 1) {
	    terminals[words[words.length-1]] = true;
    } else {
    	terminals[words] = true;
    }
    
    // Assign first word from words split above to possible start words (now that we've sorted out duplicate prevention)
    startwords.push(words[0]);
    
    // For each word that's been found
    if(words.length > 1) {
    
      for (var j = 0; j < words.length - 1; j++) {
          if (wordstats.hasOwnProperty(words[j])) {
              // If words does exist, push word next to that one in to here instead.
              wordstats[words[j]].push(words[j+1]);
          } else {
              // If it doesn't exist, assign word next to first set in to word stats.
              wordstats[words[j]] = [words[j+1]];
          }
      }
      
    }
    
}

// console.log(terminals, startwords, wordstats);

// A helper function that will find you a random word from a collection of words
var choice = function (a) {
    var i = Math.floor(a.length * Math.random());
    return a[i];
};

// Now you should have a set of words to use below.
var make_title = function (min_length) {
    
   ...