Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
mxchicken/chicken.pl
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
executable file
152 lines (115 sloc)
4.43 KB
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
#!/usr/bin/perl -w | |
use strict; | |
use Getopt::Long; | |
my $VERSION = '8.0'; | |
sub USAGE { | |
print <<"eof"; | |
usage: $0 [options] | |
--numberofwords -nw <n[-m]> : number of words, ranged from n to m | |
--maxlength -ml <n> : maximun length | |
--enumerate -en <n> : create a list of arguments | |
--namethischicken : tell the chickens name | |
--printchickenversion : print out "$VERSION" | |
--seed <n> : feed the random chicken | |
eof | |
exit; | |
} | |
our %OPT; | |
my $opt_version = 0; | |
my $opt_numberOfWords = -1; | |
my $opt_numberOfWordsRange = '15-75'; | |
my $opt_numberOfWordsRangeStart = 0; | |
my $opt_maxlength = 0; | |
my $numberOfWords = 0; | |
my $opt_namethischicken = 0; | |
my $opt_seed = 0; | |
my ( $f, $t ) = ( 0, 0 ); | |
my $opt_enumerate = 0; | |
GetOptions( | |
'numberofwords|nw=s' => \$opt_numberOfWordsRange, | |
'maxlength|ml=i' => \$opt_maxlength, | |
'enumerate|en=i' => \$opt_enumerate, | |
'namethischicken' => \$opt_namethischicken, | |
'printchickenversion' => \$opt_version, | |
'seed=i' => \$opt_seed, | |
) or USAGE; | |
if ($opt_version) { | |
print $VERSION. "\n"; | |
exit; | |
} | |
srand( $opt_seed ? $opt_seed : time() ); | |
my @WORDS = ( "puk", "pukaak", "cluck", "cluckity", "bwak", "waak", "bok", "bwok", "cluck-a-buh-gawk", "cock-a-doodle-doo", "bwwwaak", "gobble", "honk" ); | |
my @PUNCTUATIONS = ( ".", "...", "!", "?" ); | |
if ($opt_namethischicken) { | |
my %ChickenPopulation; | |
my @ChickenNames = grep { !/-/ } @WORDS; | |
for my $pn (@ChickenNames) { | |
for my $ln (@ChickenNames) { | |
$ChickenPopulation{ ucfirst($pn) . ' ' . ucfirst($ln) } = $pn eq $ln ? 1 : 2; | |
} | |
} | |
# "Waak Bok" could also be married to "Cluck" which would end up in "Waak Bok-Cluck" | |
for my $pn (@ChickenNames) { | |
for ( keys %ChickenPopulation ) { | |
push @_, ucfirst($pn) . ' ' . $_ unless $ChickenPopulation{$_} == 1; | |
} | |
} | |
for (@_) { s/( \w+) /$1-/; $ChickenPopulation{$_} = 3 } | |
my @CN = keys %ChickenPopulation; | |
my $chickensname = $CN[ rand( scalar @CN ) ]; | |
print "$chickensname\n"; | |
exit; | |
} | |
if ( $opt_numberOfWordsRange =~ m/^\d+$/ ) { | |
$opt_numberOfWords = $opt_numberOfWordsRange; | |
} | |
elsif ( $opt_numberOfWordsRange =~ m/^\d+-\d+$/ ) { | |
( $f, $t ) = split( /-/, $opt_numberOfWordsRange, 2 ); | |
} | |
else { | |
print STDERR "wrong format for --numberofwords\n"; | |
print STDERR "should be either 'n' or 'n-m'. ( '-nw 50' or '-nw 30-90''\n"; | |
exit; | |
} | |
my $enumerate = ($opt_enumerate) ? $opt_enumerate : 1; | |
for ( my $e = 0; $e < $enumerate; $e++ ) { | |
if ( $opt_numberOfWords >= 0 ) { | |
$numberOfWords = $opt_numberOfWords; | |
} | |
else { | |
$numberOfWords = int( rand( abs( $f - $t ) + 1 ) + ( ( $f < $t ) ? $f : $t ) ); | |
} | |
# generate paragraph, with random words, capitalization, and punctuation. | |
my $paragraph = ""; | |
my $previousWordHadPunctuation = 0; | |
for ( my $i = 0; $i < $numberOfWords; $i++ ) { | |
# pick a random word | |
my $word = $WORDS[ int( rand( scalar @WORDS ) ) ]; | |
# magic multiplier | |
$word = join( "-", ($word) x ( 4 - int( ( log( rand(7) + 1 ) / log(2) ) + 1 ) ) ); | |
# even more magical multiplier | |
$word =~ s/([aou]){2,}?/$1 x (rand(7)+1)/ge; | |
# optionally add punctuation | |
# note: always add punctuation if it's the last word. | |
my $shouldAddPunctuation = $i == $numberOfWords - 1 || rand() > 0.9; | |
my $punctuation = $shouldAddPunctuation ? $PUNCTUATIONS[ int( rand( scalar @PUNCTUATIONS ) ) ] : ""; | |
$word = $word . $punctuation; | |
# optionally capitalize the word | |
# note: always capitalize if it's the first words, or if preceded by punctuation | |
my $shouldCapitalize = $i == 0 || $previousWordHadPunctuation || rand() > 0.7; | |
$word = $shouldCapitalize ? uc( substr( $word, 0, 1 ) ) . substr( $word, 1 ) : $word; | |
# preserve info for next iteration | |
$previousWordHadPunctuation = $shouldAddPunctuation; | |
if ( $opt_maxlength == 0 or length($paragraph) + length($word) - 1 < $opt_maxlength ) { | |
# add word to paragraph | |
$paragraph = $paragraph . $word; | |
# if not last one, add a space | |
$paragraph = $i == $numberOfWords - 1 ? $paragraph : $paragraph . " "; | |
} | |
} | |
if ($opt_enumerate) { | |
printf "%d. ", $e + 1; | |
} | |
print $paragraph; | |
print "\n"; | |
} |