Skip to content

Instantly share code, notes, and snippets.

@jabyrd3
Created May 12, 2014 19:56
Show Gist options
  • Save jabyrd3/efc58f9a9b580aa98ea2 to your computer and use it in GitHub Desktop.
Save jabyrd3/efc58f9a9b580aa98ea2 to your computer and use it in GitHub Desktop.
say a frasier
var util = require('util');
var fs = require('fs');
var markov = require('markov');
function theCleaner(theText) {
if (theText.slice(-1) !== ('!' || '?' || '.')) {
theText = theText.slice(0, theText.length - 1);
theText += '.';
}
return theText.toLowerCase()
.replace(/(^\s*\w|[\.\!\?]\s*\w)/g, function(c) {
return c.toUpperCase()
})
.replace(/( i )/g, function() {
return ' I '
})
.replace(/([.,!?])+\S/g, function(c) {
var output = [c.slice(0, 1), " ", c.slice(1)].join('');
return output;
})
.replace(/[.!,?][.!,?]/g, function(c) {
return c.split(0, 1);
})
.replace(/[.!,?]\s[^A-Z || ^ a-z]/, function(c) {
return c.split(0, 1);
})
.replace(/(roz)/g, function(c) {
return 'Roz';
})
.replace(/(niles)/g, function(c) {
return 'Niles';
})
.replace(/[^A-Z ^a-z][^A-Z ^a-z] | [ ]/g, function() {
return ""
})
.replace(/[,.?!]\s[,.?!][,.?!]/g, function(c) {
return c.slice(0, 1)
});
}
say = function() {
var m = markov(3);
var blurp = "";
var littleCorpus = "";
var blurp = fs.readFileSync('./drfrasier.txt', {
encoding: 'utf-8'
});
var mlength = 100;
var said = "";
var saidClump = "";
blurp = blurp.split('\n');
var corpseSeed = Math.floor(Math.random() * (blurp.length - 0 + 1)) + 0;
for (i = 0; i < mlength; i++) {
littleCorpus += blurp[corpseSeed + i];
}
var said = m.seed(littleCorpus, function() {
var lines = m.fill(m.pick(), 8);
for (i = 0; i < lines.length; i++) {
saidClump += lines[i] + " ";
}
});
saidClump = theCleaner(saidClump);
return saidClump;
}
//console.log(say()); for testing uncomment this line and console log.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment