Skip to content
This repository has been archived by the owner on Jan 14, 2020. It is now read-only.

Commit

Permalink
bin for generating random phrases without duplicates.
Browse files Browse the repository at this point in the history
  • Loading branch information
brianloveswords committed Nov 6, 2012
1 parent a358762 commit 6031fb6
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Makefile
Expand Up @@ -8,10 +8,10 @@ lint:
@jshint *.js lib/*.js models/*.js routes/*.js @jshint *.js lib/*.js models/*.js routes/*.js


test: test:
@node test && node_modules/.bin/tap tests/*.test.js @node tests && node_modules/.bin/tap tests/*.test.js


verbose-test: verbose-test:
@node test/*.test.js @node tests/*.test.js


heroku: heroku:
@git push heroku master && heroku open @git push heroku master && heroku open
Expand Down
68 changes: 68 additions & 0 deletions bin/randwiches.js
@@ -0,0 +1,68 @@
#!/usr/bin/env node
const argv = require('optimist').argv;
const phrases = require('../lib/phrases');
const iteratorStream = require('iterator-stream');
const fs = require('fs');
const pathutil = require('path');
const util = require('util');

function log() {
const args = [].slice.call(arguments);
const str = util.format.apply(util, args);
process.stderr.write(str + '\n');
}

function usage(err) {
if (err) log('\nError:', err, '\n');
log('%s -n <count> [files-to-exclude, ...]', argv.$0);
log(' Prints a bunch of random phrases to stdout.');
log(' If given files, will avoid any phrases that appear in those files');
process.exit(1);
}

function fileToArray(file) {
return (fs.readFileSync(pathutil.join(process.cwd(), file))
.toString()
.trim()
.split('\n'));
}

function inArrayFilter(array) {
return function (v) {
if (array.indexOf(v) > -1) log('DUPLICATE:', v);
return array.indexOf(v) == -1;
};
}

function main() {
const count = argv.n || argv.count
const excludeFiles = argv._;
var exclude = [];
if (!count)
return usage('requires -n');

log('generating', count, 'phrases');

var filter = function (x) { return true };

if (excludeFiles.length) {
log('excluding', excludeFiles);
exclude = (excludeFiles
.map(fileToArray)
.reduce(function (exclude, phrases) {
return exclude.concat(phrases);
}, exclude));
}

iteratorStream(phrases.iterator, {
transform: function (v) { return v.join('-') },
filter: inArrayFilter(exclude),
take: 1000,
separator: '\n',
method: 'random'
}).pipe(process.stdout);

}

if (!module.parent) main();

10 changes: 6 additions & 4 deletions lib/phrases/index.js
Expand Up @@ -12,19 +12,21 @@ const fs = require('fs');
const ADVERBS = fileToArray('adverbs.txt'); const ADVERBS = fileToArray('adverbs.txt');
const ADJECTIVES = fileToArray('adjectives.txt'); const ADJECTIVES = fileToArray('adjectives.txt');
const NOUNS = fileToArray('nouns.txt'); const NOUNS = fileToArray('nouns.txt');
const generator = sandwich(ADVERBS, ADJECTIVES, NOUNS); const iterator = sandwich(ADVERBS, ADJECTIVES, NOUNS);


module.exports = function phrases(count) { function phrases(count) {
const matches = {} const matches = {}
const results = [] const results = []
var length = 0; var length = 0;
var word; var word;
while (length < count) { while (length < count) {
word = generator.random().join('-'); word = iterator.random().join('-');
if (!matches[word]) { if (!matches[word]) {
matches[word] = results.push(word); matches[word] = results.push(word);
length++; length++;
} }
} }
return results; return results;
}; };
phrases.iterator = iterator;
module.exports = phrases;
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -19,7 +19,9 @@
"up": "0.2.2", "up": "0.2.2",
"connect-flash": "0.1.0", "connect-flash": "0.1.0",
"jwt-simple": "0.1.0", "jwt-simple": "0.1.0",
"sandwich": "~0.1.1" "sandwich": "~0.1.1",
"optimist": "~0.3.5",
"iterator-stream": "~0.4.0"
}, },
"devDependencies": { "devDependencies": {
"tap": "~0.3.1" "tap": "~0.3.1"
Expand Down

0 comments on commit 6031fb6

Please sign in to comment.