Created
August 6, 2014 16:05
-
-
Save ashaindlin/96a674ee7f20caa9a109 to your computer and use it in GitHub Desktop.
Proof-of-concept thing for a twitterbot that doesn't repeat words
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
word | |
more | |
sea | |
breeze | |
green | |
have | |
ocean | |
touch | |
file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run with
node quit-when-empty.js
. If you've madeunused.txt
in Vim or another editor that handles newlines correctly, then you'll need to reopen it withvim -b unused.txt
, do:set noeol
, and resave it. Otherwise, one of the "words" output bygo()
will be an empty string. (Blame JavaScript.)Since
unused.txt
andused.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.