Markov Chain Text Generation

Take the input text and create some output text in the style of the original.

by Dan Shahin

HTML

<script src="http://www.goodoldfashioned.com/js/markovmaker.js"></script>
<h1>Markov Chain Text Generation</h1>

<div>
    <p>Transform the input text into interesting output text in the style of the original text.</p>

    <a href="https://github.com/dshahin/markovmaker" target="_blank">Markov Maker JQuery plugin</a>

</div>
<textarea name="input" id="input" cols="80" rows="5">The unanimous Declaration of the thirteen united States of America, When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation. We hold these truths to be self-evident, that all men are created equal, that they are endowed by their Creator with certain unalienable Rights, that among these are Life, Liberty and the pursuit of Happiness.  That to secure these rights, Governments are instituted among Men, deriving their just powers from the consent of the governed, that whenever any Form of Government becomes destructive of these ends, it is the Right of the People to alter or to abolish it, and to institute new Government, laying its foundation on such principles and organizing its powers in such form, as to them shall seem most likely to effect their Safety and Happiness. Prudence, indeed, will dictate that Governments long established should not be changed for light and transient causes; and accordingly all experience hath shewn, that mankind are more disposed to suffer, while evils are sufferable, than to right themselves by abolishing the forms to which they are accustomed. But when a long train of abuses and usurpations, pursuing invariably the same Object evinces a design to reduce them under absolute Despotism, it is their right, it is their...

CSS

body {
    text-align: center;
background: #d60a0e; /* Old browsers */
background: -moz-linear-gradient(left,  #d60a0e 0%, #e3f2ee 33%, #e3f2ee 66%, #3180ce 100%, #3180ce 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, right top, color-stop(0%,#d60a0e), color-stop(33%,#e3f2ee), color-stop(66%,#e3f2ee), color-stop(100%,#3180ce), color-stop(100%,#3180ce)); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(left,  #d60a0e 0%,#e3f2ee 33%,#e3f2ee 66%,#3180ce 100%,#3180ce 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(left,  #d60a0e 0%,#e3f2ee 33%,#e3f2ee 66%,#3180ce 100%,#3180ce 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(left,  #d60a0e 0%,#e3f2ee 33%,#e3f2ee 66%,#3180ce 100%,#3180ce 100%); /* IE10+ */
background: linear-gradient(to right,  #d60a0e 0%,#e3f2ee 33%,#e3f2ee 66%,#3180ce 100%,#3180ce 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#d60a0e', endColorstr='#3180ce',GradientType=1 ); /* IE6-9 */


}
#input {

    font-size: 18px;
}
#output {
    background-image: url("http://s25.postimg.org/tt4ml3dzj/img_noise_361x370_1.png");
    background-color:white;
    opacity: 1;
    border: 1px solid black;
    margin: 40px;
    padding: 40px;
    display:none;
    font-family: cursive;
    font-size: 24px;
    -webkit-box-shadow: 10px 10px 51px 0px rgba(0,0,0,0.64);
-moz-box-shadow: 10px 10px 51px 0px rgba(0,0,0,0.64);
box-shadow: 10px 10px 51px 0px rgba(0,0,0,0.64);
}
#json{
    display: none;
    font-family: monospace;
    font-size: 18px;
    margin-top:20px;
}
#min{
    width: 3em;    
}

#generate{
    
}

.generate {
	-moz-box-shadow: 0px 0px 0px 2px #9fb4f2;
	-webkit-box-shadow: 0px 0px 0px 2px #9fb4f2;
	box-shadow: 0px 0px 0px 2px #9fb4f2;
	background:-webkit-gradient(linear, left top, left bottom, color-stop(0.05, #7892c2), color-stop(1, #476e9e));
	background:-moz-linear-gradient(top, #7892c2 5%, #476e9e 100%);
	background:-webkit-linear-gradient(top, #7892c2 5%,...

JavaScript

/*
 * markovmaker
 * 
 *
 * Copyright (c) 2014 dan shahin
 * Licensed under the MIT license.
 */

(function ($) {
  var terminals = {};
  var startwords = [];
  var wordstats = {};
  var sentences = [];

  // Static method.
  $.markovmaker = function (input,options) {
    // Override default options with passed-in options.
    options = $.extend({}, $.markovmaker.options, options);
    // Return the name of your plugin plus a punctuation character.
    var newString = markov(options,input);
    return newString ;
  };

  // Static method default options.
  $.markovmaker.options = {
    punctuation: '.',
    min_length : 2,
    punctuation_marks: /\.|\;|\?|\:|\n/,
    word_splitter: /\s/
  };

  function markov(options,input){
    //options = $.extend({}, $.markovmaker.options, options);
    terminals = {}; //properties are last words in sentences
    startwords = []; //values are first words in sentences
    wordstats = {}; // the markov chain 
    
    //split the input text into sentences
    sentences = input.split(options.punctuation_marks);
    //for each sentence
    for (var i = 0; i < sentences.length; i++) {
        //split sentence into words
        var words = sentences[i].split(options.word_splitter);
        //take note of last word in sentence
        terminals[words[words.length - 1]] = true;
        //take note of first word
        startwords.push(words[0]);
        //for each word
        for (var j = 0; j < words.length - 1; j++) {
            //build an object with word as property
            //pointing to an arry of words that come after it
            var thisWord = words[j],
                nextWord = words[j + 1];
            //optimization
            if(thisWord !== '' && nextWord !== null){
                if (!wordstats.hasOwnProperty(thisWord)) {
                    //first time we've seen this word
                    //create an array with the next word in it
                    wordstats[thisWord] = [nextWord];
                }...