Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <link href="https://fonts.googleapis.com/css?family=Josefin+Sans" rel="stylesheet">

<span onclick="toggleOptions()">Options</span>
<span onclick="gen()">Generate</span>
<a href="http://xaloez.com/" target="_blank">App by Ethan Alexander Shulman</a>


<div id="out">
Generating text...
</div>

<div id="opt">
  
Source:<br>
<textarea id="source" cols="20" rows="10">
I am Sam.
Sam I am.
That Sam-I-am!
That Sam-I-am!
I do not like
That Sam-I-am!
Do you like
Green eggs and ham?
I do not like them,
Sam-I-am.
I do not like
Green eggs and ham.
Would you like them
Here or there?
I would not like them
Here or there.
I would not like them
Anywhere.
I do not like
Green eggs and ham.
I do not like them,
Sam-I-am.
Would you like them
In a house?
Would you like them
With a mouse? 
I do not like them
In a house.
I do not like them
With a mouse.
I do not like them
Here or there.
I do not like them
Anywhere.
I do not like green eggs and ham.
I do not like them, Sam-I-am.
Would you eat them
In a box?
Would you eat them
With a fox?
Not in a box.
Not with a fox.
Not in a house.
Not with a mouse.
I would not eat them here or there.
I would not eat them anywhere.
I would not eat green eggs and ham.
I do not like them, Sam-I-am.
Would you? Could you?
In a car?
Eat them! Eat them!
Here they are.
I would not, could not,
In a car.
You may like them.
You will see.
You may like them
In a tree!
I would not, could not in a tree.
Not in a car! You let me be.
I do not like them in a box.
I do not like them with a fox.
I do not like them in a house.
I do not like them with a mouse.
I do not like them here or there.
I do not like them anywhere. I do not like green eggs and ham. 
I do not like them, Sam-I-am. 
</textarea><br><br>

Depth: <input id="depth" type="text" size="4" value="3"/><br><br>
Drunkenness: <input id="drunk" type="range" min="0" max="100" value="20"/><br><br>
Generated Text Length: <input id="len" type="text" size="8" value="512"/><br><br>
Word-space: <input id="wordspace" type="checkbox"/>
  
</div>
              
            
!

CSS

              
                body {
  background-color: #3265FF;
  
  font-family: 'Josefin Sans', sans-serif;
  font-size: 20px;
  color: #253052;
  text-shadow: 1px 1px 1px #079C43;
  
  cursor: default;
}

span,a {
   -webkit-touch-callout: none;
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  
  margin-left: 50px;
  
  color: #253000;
  cursor: pointer;
}
span:hover,a:hover {
  color: #2530FF;
}
a {
  position: absolute;
  bottom: 0px;
  right: 5px;
}

div {
  overflow-y: auto;
  
  text-align: center;
  
  position: absolute;
  left: 50px;
  top: 50px;
  right: 50px;
  bottom: 30px;
  
  padding: 12px;

  color: black;
  background-color: #1E56FF;
}
#out {
}
#opt {
  display: none;
}

input[type="text"],textarea {
    background-color: #3265FF;
}
#source {
  width: 100%;
  
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
              
            
!

JS

              
                //Ethan Alexander Shulman 2016
//Procedural text generation from source text using something like a Markov Chain


var optionsOpen = false, outEle, optEle,
    generating = false,
    
    ge = function(s) {return document.getElementById(s);};

//initialize on load
window.onload = function() {
  outEle = ge("out");
  optEle = ge("opt");
  
  //initial generation
  setTimeout(gen);
};


//toggle options menu
function toggleOptions() {
  var outD = "none",
       optD = "none";
  if (optionsOpen) outD = "block";
  else optD = "block";
  
  outEle.style.display = outD;
  optEle.style.display = optD;
  
  optionsOpen = !optionsOpen;
}


//generate text
function gen() {
  if (generating) return;//only allow 1 generation at a time
  generating = true;
  
  var src = ge("source").value,
      wordSpace = ge("wordspace").checked,
      drunkenness = parseInt(ge("drunk").value)/100.0,
      seqDepth = parseInt(ge("depth").value),
      vocab = [], sa = new Sequence(seqDepth), ss;
  
  //build vocabulary
  if (wordSpace) {
    //word vocab
    ss = src.replace("\n","").split(" ");
    var i = ss.length;
    while (i--) {
      var ind = vocab.indexOf(ss[i]);
      if (ind === -1) {
        var ind = vocab.length;
        vocab.push(ss[i]);
        ss[i] = ind;
      } else {
        ss[i] = ind;
      }
    }
  } else {
    //letter vocab
    ss = new Uint32Array(src.length);
    var i = src.length;
    while (i--) {
      var ind = vocab.indexOf(src[i]);
      if (ind === -1) {
        ss[i] = vocab.length;
        vocab.push(src[i]);
      } else {
        ss[i] = ind;
      }
    }
  }
 
  //train/learn probabilities from source text
  var i = -1,
      l = ss.length;
  while (++i < l) {
      sa.In(String.fromCharCode(ss[i]));
  }

  
  //generate new text
  sa.Reset();
  var i = parseInt(ge("len").value),
      g = new Uint32Array(i),
      c = -1;
  while (++c < i) {
    var k = -1,
        r = sa.Next(drunkenness);
    
    if (r === -1) {
      r = ~~(Math.random()*vocab.length);//no next guess because there is no current memory state, seed with random vocab character
    }
      
    g[c] = r;
    sa.In(String.fromCharCode(r),true);
  }
  
  //convert generated new text from vocab to words/letters
  var out = "";
  var i = -1,
      l = g.length;
  while (++i < l) {
    out += vocab[g[i]];
    if (wordSpace) out += " ";
  }
  
  ge("out").innerHTML = out;
  generating = false;
}


function Sequence(memorySize) {
  this.memorySize = memorySize;
  this.memory = "";
  this.singleMemory = "";
  this.currentMemoryId = -1;
  this.singleMemoryId = -1;
  this.memories = [];
  this.probabilities = [];
  this.sums = [];
  this.nonWeightedSums = [];
}
//reset and clear memory
Sequence.prototype.Reset = function() {
  this.memory = "";
  this.currentMemoryId = -1;
}
//process input i
Sequence.prototype.In = function(i,dontLearn) {
  if (this.memory.length === this.memorySize) this.memory = this.memory.slice(1)+i;
  else this.memory += i;
  this.singleMemory = i;
  
  if (!dontLearn) {
    if (this.currentMemoryId !== -1 && this.singleMemoryId !== this.currentMemoryId) {
      var p = this.probabilities[this.currentMemoryId][i.charCodeAt(0)]++;
      if (p === 0) this.nonWeightedSums[this.currentMemoryId]++;
      this.sums[this.currentMemoryId]++;
    }
    if (this.singleMemoryId !== -1) {
      var p = this.probabilities[this.singleMemoryId][i.charCodeAt(0)]++;
      if (p === 0) this.nonWeightedSums[this.singleMemoryId]++;
      this.sums[this.singleMemoryId]++;
    }
  }
  
  this.currentMemoryId = this.memories.indexOf(this.memory);
  this.singleMemoryId = this.memories.indexOf(this.singleMemory);
  
  if (!dontLearn) {
    if (this.currentMemoryId === -1 && this.currentMemoryId !== this.singleMemoryId) {
      this.currentMemoryId = this.memories.length;
      this.probabilities.push(new Uint32Array(65565));
      this.sums.push(0);
      this.nonWeightedSums.push(0);
      this.memories.push(this.memory);
    }
    if (this.singleMemoryId === -1) {
      this.singleMemoryId = this.memories.length;
      this.probabilities.push(new Uint32Array(65565));
      this.sums.push(0);
      this.nonWeightedSums.push(0);
      this.memories.push(this.singleMemory);
    }
  }
}
//guess next character in sequence based on probabilities with a certain randomness(0-1)
Sequence.prototype.Next = function(randomness) { 
  //no current memory to guess next from
  var mId = this.currentMemoryId === -1 ? this.singleMemoryId : this.currentMemoryId;
  if (mId === -1) return -1;
  
  var rand = Math.random(),
      min = 0.0,
      i = -1,
      pa = this.probabilities[mId],
      ps = this.sums[mId],
      ds = randomness/this.nonWeightedSums[mId];
  if (ps === 0) return -1;//no probabilities to guess next from
  
  while (++i < 65565) {
    var len = (pa[i]/ps)*(1-randomness)+ds*(pa[i]>0?1:0),
        max = min+len;

    if (rand >= min && rand < max+1e-6) return i;
    min = max;
  }
  return 65564;
}

              
            
!
999px

Console