'use strict';

var markovGenerator = (function() {

	var EOS = [ "." , "?", "!" ];
	var min_sentence_words = 5;
	var max_sentence_words = 30;

	function getDictItemByKey(dict, key) {
		for (var i = 0, len = dict.length; i < len; i++) {
			if (dict[i].key === key) return dict[i];
		}
		return null;
	}

	function generateSentence(dict, chainSize) {
		console.log("generating sentence");

		// find a seed that starts with a capital letter
		var seedAttempt = 0;
		while (true) {
			seedAttempt++;
			var seed = dict[Math.floor(Math.random() * dict.length)].words;

			var firstChar = seed[0].charAt(0);
			if (firstChar === firstChar.toUpperCase())
			{
				var generatedWords = seed;
				break;
			}

			// too many loops, just use this one and move on.
			if (seedAttempt > 20)
			{
				var generatedWords = seed;
				break;
			}
		}

		while (true) {
			var last_words = generatedWords.slice(-1 * chainSize);
			var match = getDictItemByKey(dict, last_words.join('/'));

			if (match === null) break;

			var next_opts = match.next;
			var rand_next = next_opts[Math.floor(Math.random() * next_opts.length)];

			if (typeof(rand_next) === 'undefined') break;

			generatedWords.push(rand_next);
			if (isFinalCharEOS(rand_next)) break;
		}

		var sentence = generatedWords.join(' ');

		var wordCount= generatedWords.length;
		if (wordCount < min_sentence_words || wordCount > max_sentence_words) {
			sentence = generateSentence(dict, chainSize);
		}

		if (!isFinalCharEOS(sentence)) {
			sentence = sentence + "."; // append a . to finish it
		}
		return sentence;
	}

	function isFinalCharEOS(str) {
		var final_char = str.charAt(str.length - 1); 
		if (EOS.indexOf(final_char) > -1) {
			return true;
		}
		return false;
	}

	return { generateSentence : generateSentence }

})();

'use strict';

var markovGenerator = (function() {

	var EOS = [ "." , "?", "!" ];
	var min_sentence_words = 5;
	var max_sentence_words = 30;

	function getDictItemByKey(dict, key) {
		for (var i = 0, len = dict.length; i < len; i++) {
			if (dict[i].key === key) return dict[i];
		}
		return null;
	}
	
	function getSeed(dict, chainSize) {
		// find a seed that starts with a capital letter
		var seed = [];
		var seedAttempt = 0;
		while (true) {
			seedAttempt++;
			var seed = dict[Math.floor(Math.random() * dict.length)].words;

			var firstChar = seed[0].charAt(0);
			if (firstChar === firstChar.toUpperCase()) {
				var generatedWords = seed;
				break;
			}

			// too many loops, just use this one and move on.
			if (seedAttempt > 20) {
				var generatedWords = seed;
				break;
			}
		}
		return seed;
	}
	
	function getWords(words, dict, chainSize) {
		while (true) {
			var last_words = words.slice(-1 * chainSize);
			var match = getDictItemByKey(dict, last_words.join('/'));

			if (match === null) break;

			var next_opts = match.next;
			var rand_next = next_opts[Math.floor(Math.random() * next_opts.length)];

			if (typeof(rand_next) === 'undefined') break;

			words.push(rand_next);
			if (isFinalCharEOS(rand_next)) break;
		}
		return words;
	}

	function generateSentence(dict, chainSize) {
		console.log("generating sentence");

		var seed = getSeed(dict, chainSize);
		var generatedWords = getWords(seed, dict, chainSize);

		var sentence = generatedWords.join(' ');

		var wordCount= generatedWords.length;
		if (wordCount < min_sentence_words || wordCount > max_sentence_words) {
			sentence = generateSentence(dict, chainSize);
		}

		if (!isFinalCharEOS(sentence)) {
			sentence = sentence + "."; // append a . to finish it
		}
		return sentence;
	}

	function isFinalCharEOS(str) {
		var final_char = str.charAt(str.length - 1); 
		if (EOS.indexOf(final_char) > -1) {
			return true;
		}
		return false;
	}

	return { generateSentence : generateSentence }

})();

Refactor generateSentence into generateSentence, getSeed, and getWords