Skip to content

Instantly share code, notes, and snippets.

@james-irwin
Created July 14, 2018 07:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save james-irwin/3d3755127b5de1d4ea52453f9bb1db97 to your computer and use it in GitHub Desktop.
Save james-irwin/3d3755127b5de1d4ea52453f9bb1db97 to your computer and use it in GitHub Desktop.
Walk a couple of n-gram dictionaries to generate a 'blurt'.
var fs=require('fs');
var opensbuf = fs.readFileSync(process.argv[2]).toString().split('\n');
var middles = JSON.parse(fs.readFileSync(process.argv[3]).toString());
var closes = JSON.parse(fs.readFileSync(process.argv[4]).toString());
var window_length = 2;
var select_start=function(starts){
var select_index = Math.floor(Math.random()*opensbuf.length);
return select_index;
}
var get_key=function(string){
var i = 0;
var key = '';
for (i=string.split(' ').length-(window_length);
i<string.split(' ').length;
i++) {
if (key!='') {key+=' ';}
key+=string.split(' ')[i];
}
return key;
}
var select_follow=function(string, dict){
var key=get_key(string);
try {
var select_index = Math.floor(Math.random()*dict[key].length);
return select_index;
}
catch (e) {throw e;}
}
var completed = false;
while (!completed) {
try {
var tweet='';
var more_middles = Math.floor(Math.random()*4);
tweet+=opensbuf[select_start(opensbuf)];
while (more_middles--) {
tweet+=' '+middles[get_key(tweet)][select_follow(tweet, middles)];
}
tweet+=' '+closes[get_key(tweet)][select_follow(tweet, closes)];
completed = true;
} catch (e){};
}
console.log(tweet);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment