#!/usr/local/bin/perl -w use strict; use Data::Dumper; =pod # # ok, the following is the location of the grammar files at # Julie Zelenski's webpage at stanford's computer science faculty. # I was thinking about using WWW::Mechanize or LWP::Agent to # fetch the grammar files, but couldn't be bothered. # my $rsg_grammar_url = "http://www-cs-faculty.stanford.edu/~zelenski/rsg/grammars/"; =cut # load the grammar definition file my $grammar = do { local $/; <DATA> }; my %grammar = %{parse_grammar($grammar)}; # print Dumper(\%grammar); my $text = random_text_from_grammar("start"); $text =~ s/(.{50,60}(?<=\s\b))/$1\n/mg; print "$text\n"; # parse the simple grammar and build a hash table sub parse_grammar { my $grammar = shift; my %grammar = $grammar =~ /^{\s*<([^>]+)>\s*([^}]+)}/mg; foreach (keys %grammar) { $grammar{$_} = [ map { s/[^\S\n]+/ /g; s/\s([.?,;!])/$1/g; $_ } split /\s*;\s*/, $grammar{$_} ]; } return \%grammar; } # generate the text recursively sub random_text_from_grammar { my $token = shift; my $selection = $grammar{$token}; my $phrase = $selection->[rand ($#$selection+1)]; $phrase =~ s/<([^>]+)>/random_text_from_grammar($1)/ge; return $phrase; } __DATA__
The haiku grammar. by Garret B. Kaminaga scorpio@leland.Stanford.EDU { <start> <5-line> / <7-line> / <5-line> - <OrientalName> ; } { <5-line> <1> <4> ; <1> <3> <1> ; <1> <1> <3> ; <1> <2> <2> ; <1> <2> <1> <1> ; <1> <1> <2> <1> ; <1> <1> <1> <2> ; <1> <1> <1> <1> <1> ; <2> <3> ; <2> <2> <1> ; <2> <1> <2> ; <2> <1> <1> <1> ; <3> <2> ; <3> <1> <1> ; <4> <1> ; <5> ; } { <7-line> <1> <1> <5-line> ; <2> <5-line> ; <5-line> <1> <1> ; <5-line> <2> ; } { <OrientalName> Basho ; Hokusai ; Makimoto ; Toshiba ; Matsushida ; Yamaha ; } { <1> red ; white ; black ; sky ; dawns ; breaks ; falls ; leaf ; rain ; pool ; my ; your ; sun ; clouds ; blue ; green ; night ; day ; dawn ; dusk ; birds ; fly ; grass ; tree ; branch ; through ; hell ; Ada ; Nick ; smile ; grey ; wave ; sea ; to ; through ; in ; no ; sound ; mind ; smoke ; cranes ; Zen ; } { <2> drifting ; purple ; mountains ; skyline ; city ; subway ; faces ; empty ; Buddhist ; temple ; Japan ; ocean ; thinking ; zooming ; rushing ; over ; rice fields ; rising ; falling ; hard disk ; sushi ; sparkling ; Tokyo ; Kyushu ; Kyoto ; } { <3> Parlante ; sunrises ; peasant farms ; computer ; sashimi ; juniper ; fishing boats ; far away ; ethernet ; Mac IP ; kimonos ; samurai ; } { <4> CD Player ; Aluminum ; yakitori ; Mitsukoshi ; pampas grass ; chrysanthemums ; cherry blossoms ; } { <5> resolutional ; non-elemental ; rolling foothills rise ; toward mountains higher ; }
Kyoto smile Nick through / day Kyoto drifting subway / temple my ocean - Hokusai rising computer / dusk Mitsukoshi day Nick / hell samurai tree - Toshiba wave white Tokyo wave / toward mountains higher empty / computer faces - Hokusai Mitsukoshi pool / sushi wave dusk Parlante / white subway sound birds - Toshiba Buddhist cranes cranes fly / Kyushu sun rice fields sea through / temple white thinking - Basho
In reply to Re: Seeking Perl poetry code that writes poems
by Roger
in thread Seeking Perl poetry code that writes poems
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |