in reply to Ode to a random number

This modified version avoids proper nouns and applies some meter to the verses by counting (half-)syllables.
#!/usr/bin/perl #ode to a random number v0.2 open my $dict, "</usr/share/dict/words" or open my $dict, "</usr/share/lib/dict/words" ; my @words = <$dict>; my $count = 70*rand(); { use integer; $count /= 1; } my $syllables = 0; for ( 'a', 'e', 'i', 'o', 'u' ) { $vowel{ $_ } = 1; } for ( 0..$count ) { my $ptr; my $thissyll; do { $ptr = rand() * $#words; $thissyll = Syllables( $words[$ptr] ); } until ( $words[$ptr] eq lc( $words[$ptr] ) and ((20-$syllables-$thissyll) >= 0 )); $syllables += $thissyll; chomp $words[$ptr]; unless( $_ ) { $words[ $ptr ] =~ /^(.)(.*)$/; my $b = uc( $1 ); $words[$ptr ] = $b . $2; } if ( $_ < $count ) { print $words[$ptr]; if ( $syllables >= 20 ) { print ";\n"; $syllables = 0; } else { print ' '; } } else { print $words[$ptr] . ".\n"; } } sub Syllables { my $word = shift; chomp $word; my $lv = 0; my $tv; my $syll = 0; for my $letter( split '', $word ) { $tv = $vowel{ $letter }; ( $tv == $lv ) or $syll++; $lv = $tv; } return $syll; }
First output:
Policeman nereid spokesman trendy; fragmentary magnetite enunciate; dyspeptic lean recombinant decile myrtle; afterlife disdain derange redneck; devious attentive condemnatory nymph 2nd sou; astigmatism shine zagging sage auk; incentive tattle woodrow deposit; boolean stagnant setback cull objet dust; elysian stuck temper freethink octile my yea; trauma endogamous cohesive hay; applejack ramp squalid beatitude a; byline novitiate noun rawhide guile; cutset pig sundown puff stablemen max; meteoritic sword dose epiphyseal vie; ceremony.

-M

Free your mind

Replies are listed 'Best First'.
Re^2: Ode to a random number
by CountZero (Bishop) on Jun 21, 2006 at 06:10 UTC
    If only it would rhyme!

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

      duly scheduled for v0.3 - although it starts to look like it needs some command line options to control features, wordcounts etc. which should also come in v0.3

      -M

      Free your mind