Skip to content

add enumerate option #1

Merged
merged 1 commit into from
Nov 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 44 additions & 31 deletions chicken.pl
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@

usage: $0 [options]

--numberofwords --nw : number of words
--numberofwords -nw <n>: number of words
--enumerate -en <n>: create a list of arguments

eof

our %OPT;

my $numberOfWords = 0;
my $opt_numberOfWords = 0;
my $opt_enumerate = 0;

GetOptions( 'numberofwords|nw=i' => \$numberOfWords, ) or die USAGE;
GetOptions(
'numberofwords|nw=i' => \$opt_numberOfWords,
'enumerate|en=i' => \$opt_enumerate,
) or die USAGE;

my @WORDS = (
"puk", "pukaaak", "cluck", "cluck-cluck-cluck", "cluckity", "bwak",
Expand All @@ -24,44 +29,52 @@

my @PARAGRAPH_SIZES = ( 15, 30, 75 );

unless ($numberOfWords) {
$numberOfWords = $PARAGRAPH_SIZES[ int( rand($#PARAGRAPH_SIZES) ) ];
}

# generate paragraph, with random words, capitalization, and punctuation.
my $paragraph = "";
my $enumerate = ($opt_enumerate) ? $opt_enumerate : 1;

my $previousWordHadPunctuation = 0;
for ( my $e = 0; $e < $enumerate; $e++ ) {
if ($opt_numberOfWords) {
$numberOfWords = $opt_numberOfWords;
}
else {
$numberOfWords = $PARAGRAPH_SIZES[ int( rand( scalar @PARAGRAPH_SIZES ) ) ];
}

for ( my $i = 0; $i < $numberOfWords; $i++ ) {
# generate paragraph, with random words, capitalization, and punctuation.
my $paragraph = "";
my $previousWordHadPunctuation = 0;
for ( my $i = 0; $i < $numberOfWords; $i++ ) {

# pick a random word
# pick a random word

my $word = $WORDS[ int( rand($#WORDS) ) ];
my $word = $WORDS[ int( rand( scalar @WORDS ) ) ];

# optionally add punctuation
# note: always add punctuation if it's the last word.
# 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($#PUNCTUATIONS) ) ] : "";
$word = $word . $punctuation;
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;
# 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;
$word = $shouldCapitalize ? uc( substr( $word, 0, 1 ) ) . substr( $word, 1 ) : $word;

# preserve info for next iteration
$previousWordHadPunctuation = $shouldAddPunctuation;
# preserve info for next iteration
$previousWordHadPunctuation = $shouldAddPunctuation;

# add word to paragraph
$paragraph = $paragraph . $word;
# add word to paragraph
$paragraph = $paragraph . $word;

# if not last one, add a space
$paragraph = $i == $numberOfWords - 1 ? $paragraph : $paragraph . " ";
}
# if not last one, add a space
$paragraph = $i == $numberOfWords - 1 ? $paragraph : $paragraph . " ";
}

print $paragraph;
print "\n";
if ($opt_enumerate) {
printf "%d. ", $e + 1;
}
print $paragraph;
print "\n";

}