Skip to content

Instantly share code, notes, and snippets.

@sbeam
Created May 27, 2015 17:53
Show Gist options
  • Save sbeam/b24b315d6c847109026e to your computer and use it in GitHub Desktop.
Save sbeam/b24b315d6c847109026e to your computer and use it in GitHub Desktop.
chbs
#!/usr/bin/perl -w
my $dict = "/usr/share/dict/words";
my $maxlen = shift @ARGV;
my $wordmin = 3;
my $wordmax = 12;
my $num_words = 4;
my $skip_possessives = 1; # db contains 's forms for every noun
use List::Util qw(shuffle);
srand;
open(WORDS, $dict) or die($!);
# two random words from dict
my $shorts = 0;
my @parts;
while (<WORDS>) {
chop;
next if (/'s$/ && $skip_possessives);
if (length() >= $wordmin and length() <= $wordmax) {
$shorts++;
for ($i=0; $i<$num_words; $i++) {
(rand($shorts) < 1) && ($parts[$i] = ((rand(2)<1)? ucfirst($_) : $_) );
}
}
}
print join(' + ', @parts) . "\n";
# stir and serve chilled.
my $whole = join(' ', shuffle @parts);
print "$whole\n";
if ($maxlen && $maxlen < length($whole)) {
$whole = substr($whole, rand(length($whole)-$maxlen), $maxlen);
print "$whole\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment