Skip to content

Instantly share code, notes, and snippets.

@ashaindlin
Created August 6, 2014 16:05
Show Gist options
  • Save ashaindlin/96a674ee7f20caa9a109 to your computer and use it in GitHub Desktop.
Save ashaindlin/96a674ee7f20caa9a109 to your computer and use it in GitHub Desktop.
Proof-of-concept thing for a twitterbot that doesn't repeat words
var fs = require('fs');
function go() {
fs.readFile('./unused.txt', function(err, data) {
if (err) throw err;
var items = data.toString().split('\n');
var index = Math.floor(Math.random()*items.length);
var item = items[index];
items.splice(index, 1);
console.log(item);
fs.writeFileSync('./unused.txt', items.join('\n'));
fs.appendFileSync('./used.txt', (item + '\n'));
if (items.length == 0)
process.exit(0);
});
}
setInterval(function() {
go();
}, 1000);
word
more
sea
breeze
green
have
ocean
touch
file
@ashaindlin
Copy link
Author

Run with node quit-when-empty.js. If you've made unused.txt in Vim or another editor that handles newlines correctly, then you'll need to reopen it with vim -b unused.txt, do :set noeol, and resave it. Otherwise, one of the "words" output by go() will be an empty string. (Blame JavaScript.)

Since unused.txt and used.txt are updated on every iteration, the program can pick up where it left off if execution is halted before it outputs the entire word list.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment