http://www.perlmonks.org?node_id=95239


in reply to Transcramble - Random text generator

Quoting some random web source whose URL I didn't bother to note:

There's an example of a travesty generator implemented in Perl in the *first* edition of the manual by Larry Wall and Randal L. Schwartz, on page 326 (sadly, it's been deleted from the 2nd ed).
So we see that those who forget history are doomed to reinvent it. ;)

        - tye (but my friends call me "Tye")

Replies are listed 'Best First'.
Re: (tye)Re: Transcramble - Random text generator
by blakem (Monsignor) on Jul 10, 2001 at 11:40 UTC
    And here it is:

    from the first Programming Perl (back when it was still pink)

    #!/usr/bin/perl # Usage travesty [files] # First analyse the input. while(<>) { # handle article headers and ">>>" quotes. next if /^\./; next if /^From / .. /^$/; next if /^Path: / .. /^$/; s/^\W+//; # Do each word. push(@ary,split(' ')); while ($#ary > 1) { $a = $p; $p = $n; $w = shift(@ary); $n = $num{$w}; if ($n eq '') { push(@word,$w); $n = pack('S',$#word); $num{$w} = $n; } $lookup{$a . $p} .= $n; } } # Now spew out the words, based on the frequencies. If there # is more than one possibility to choose from choose one # randomly. for (;;) { $n = $lookup{$a . $b}; ($foo,$n) = each(lookup) if $n eq ''; # A bootstrap. $n = substr($n,int(rand(length($n))) & 0177776,2); $a = $p; $p = $n; ($w) = unpack('S',$n); $w = $word[$w]; # See if word fits on line; $col += length($w) +1; if ($col >= 65) { $col = 0; print "\n"; } else { print ' '; } print $w; # Paragraph every 10 sentences or so. if ($w =~ /\.$/) { if (rand() < .1) { print "\n"; $col = 80; } } }
    p.s. These little gems are the reason I keep that old pink book around.
      I tried this using a large text file from Writing of the Early Church Father", at Christian Classics Ethereal Library. The results were pretty amazing! Nice that you kept the "old book" around.