I was looking at one of those "Shakespearian Insult Kits" that has three rows of words which you can randomly slap together and add "Thou" to the beginning to make a quick insult. The end result is usually pretty funny, but got sort of boring after a while. That got me thinking about using something like a genetic algorithm to come up with better ones using peoples ratings of previous ones. So what this script does is present the visitor to the website with a insult generated by taking words from three files and adding "Thou" to the beginning. Then they can rate the insult good,okay, or bad. If they rate it good the words used in the insult will become more likely to show up next-time. If they rate it bad, the words used will be less likely to show up. If they rate it okay, the words remain just as likely to show up as before. Theoretically this should lead to the perfect Shakespearian insult accoording to the combined opinions of the visitors. It can also deliver an insult to your palm, using avantgo, so you can try it out on people during the day and then rate it from your palm. You can get the avantgo channel here. If you'd like to check it out with the normal web interface and rate a couple go here. The code that runs it is:
#!/usr/bin/perl use CGI qw(:standard); #Shakespear Lives! my $mode = param("mode"); if ($mode eq "palm") { print header; open(ONE,"/home/perlmonk/public_html/shakespear/one"); open(TWO,"/home/perlmonk/public_html/shakespear/two"); open(THREE,"/home/perlmonk/public_html/shakespear/three"); chomp(my @one = <ONE>); chomp(my @two = <TWO>); chomp(my @three = <THREE>); print "<HTML>\n"; print "Thou "; my $one = $one[rand @one]; print $one; print " "; my $two = $two[rand @two]; print $two; print " "; my $three = $three[rand @three]; print $three; print <<ENDFORM; <BR> <BR> <form action="http://www.takiweb.com/~perlmonk/shakespear/insult.cgi" +method="get" name="thingymabob"> <input type="hidden" name="mode" value="eval"> <input type="radio" name="rating" value="good" checked><b>Good</b><br> <input type="radio" name="rating" value="okay"><b>Okay</b><br> <input type="radio" name="rating" value="bad"><b>Bad</b><br> ENDFORM print "<input type=\"hidden\" name=\"one\" value=\"$one\">"; print "<input type=\"hidden\" name=\"two\" value=\"$two\">"; print "<input type=\"hidden\" name=\"three\" value=\"$three\">"; print <<FINALE; <input type="Submit" value="Rate it!" align="MIDDLE"> </form> FINALE print "</HTML>"; } if ($mode eq "gen") { open(ONE,"/home/perlmonk/public_html/shakespear/one"); open(TWO,"/home/perlmonk/public_html/shakespear/two"); open(THREE,"/home/perlmonk/public_html/shakespear/three"); chomp(my @one = <ONE>); chomp(my @two = <TWO>); chomp(my @three = <THREE>); print header; print <<EOT; <HTML> <BODY BGCOLOR=#000000 TEXT=#FF9900 LINK=#FF9900 VLINK=#FF9900> <CENTER><IMG SRC="shakespear.gif" ALT="Shakespear the talking head!">< +/CENTER> <CENTER><H3> EOT print "Thou "; my $one = $one[rand @one]; print $one; print " "; my $two = $two[rand @two]; print $two; print " "; my $three = $three[rand @three]; print $three; print "</H3>"; print "<BR>"; print "<BR>"; print "<H4>So was that,"; my $option; foreach $option("good","okay","bad") { print "<A HREF=\"insult.cgi?mode=eval&one=$one&two=$two&three=$three&r +ating=$option\">$option</A>"; print ","; } print "or are you paralyzed by its brilliance so much that you can't c +lick</H4>"; print <<END; </H4> </BODY> </HTML> END } if ($mode eq "eval") { my $one = param("one"); $one =~ s/\0//g; my $two = param("two"); $two =~ s/\0//g; my $three = param("three"); $three =~ s/\0//g; my $rating = param("rating"); if ($rating eq "good") { $/ = undef; open(ONE,"/home/perlmonk/public_html/shakespear/one"); my $checkone = <ONE>; if ($checkone =~ /$one\n/) { close(ONE); open(ONE,">>/home/perlmonk/public_html/shakespear/one"); print ONE "$one\n"; } open(TWO,"/home/perlmonk/public_html/shakespear/two"); my $checktwo = <TWO>; if ($checktwo =~ /$two\n/) { close(TWO); open(TWO,">>/home/perlmonk/public_html/shakespear/two"); print TWO "$two\n"; } open(THREE,"/home/perlmonk/public_html/shakespear/three"); my $checkthree = <THREE>; if ($checkthree =~ /$three\n/) { close(THREE); open(THREE,">>/home/perlmonk/public_html/shakespear/three"); print THREE "$three\n"; } } if ($rating eq "bad") { $/ = undef; open(ONE,"/home/perlmonk/public_html/shakespear/one"); open(TWO,"/home/perlmonk/public_html/shakespear/two"); open(THREE,"/home/perlmonk/public_html/shakespear/three"); my $listone = <ONE>; my $listtwo = <TWO>; my $listthree = <THREE>; close ONE; $listone =~ s/$one\n//; open (ONE,">/home/perlmonk/public_html/shakespear/one"); print ONE "$listone"; close TWO; $listtwo =~ s/$two\n//; open (TWO,">/home/perlmonk/public_html/shakespear/two"); print TWO "$listtwo"; close THREE; $listthree =~ s/$three\n//; open (THREE,">/home/perlmonk/public_html/shakespear/three"); print THREE "$listthree"; } print header; print"<HTML>\n<BODY BGCOLOR=#000000 TEXT=#FF9900 LINK=#FF9900 VLINK=#F +F9900>"; print "<H3>So that was $rating, eh?"; print '<A HREF="insult.cgi?mode=gen">rate another</A> </BODY></HTML>'; print "</H3>\n</BODY>\n</HTML>"; }


"Sanity is the playground of the unimaginative" -Unknown

Edit Petruchio Tue Dec 25 17:05:08 UTC 2001 - Added READMORE tag, changed links to square-bracket notation.

Replies are listed 'Best First'.
Re: Breeding Shakesperian Insults
by stefp (Vicar) on Dec 29, 2001 at 11:59 UTC
    There is now a very generic content generator framework written as Perl script you should check before boiling your own. This is nonsense. It makes actually a lot of sense. :)

    -- stefp

    I am ashamed, I answered without reading closely enough and did not get it. Even if off the mark here, nonsense is good pointer anyway.

      This was not meant to be a content generator but rather a genetic algorithm aimed at producing better and better results until it finds the best. Although Nonsense is pretty cool, its not what I was aiming for.

      "Sanity is the playground of the unimaginative" -Unknown