in reply to Random Bumper Sticker Generator
I'm educated dumb, how about you?
Sample output:
A teacher kills a suicidal child before a dog. A teacher enslaves. A lie evolves simultaneously.I wrote this a early in my Perl-learning effort, but I'd love comments on my coding style and implementation, if you are willing to offer them. This was before I discovered use strict and -w.
#! /usr/bin/perl # Sentence Maker # Read a Grammar and randomly generate sentences. my %rule; # symbols # ------------------------ # Q - sentence # S Subject # P predicate # N Noun # I Intransitive Verb # T Transitive Verb # A Adjective # D Adverb # R Article (a the an) # E Prepositional Phrase # F Preposition $rule{"Q"}="SP|SPE"; $rule{"S"}="N|RN|RAN|"; $rule{"P"}="I|ID|T|TD|TS"; $rule{"E"}="FRN|FRAN"; $s_map{"N"}=\@noun_list; $s_map{"T"}=\@t_verb_list; $s_map{"I"}=\@i_verb_list; $s_map{"A"}=\@adj_list; $s_map{"D"}=\@adv_list; $s_map{"R"}=\@article_list; $s_map{"F"}=\@prep_list; @noun_list= ( "Timecube", "teacher", "teacher", "God", "dog", "gods", "Gene Ray", "evil", '\'Cube Spirit\'', "Evil", "religion", "word", "lie", "scam", "adult", "child", '"Forbidden Truth"', "Nature\'s Harmonic Simultaneous 4-Day Perpetual Time Cube Creatio +n" ); # ' @t_verb_list= ( "enslaves", "educates", "outlaws", "kills", "teaches", "eats", "lies", "equates", "indicts" ); @i_verb_list= ( "evolves", "rotates", "dies", ); @adv_list= ( "slowly", "quickly", "simultaneously" ); @adj_list= ( "stupid", "retarded", "queer", "4-cornered", "1-corner", "4/16", "suicidal", "evil", "brainwashed", "indoctrinated" ); @article_list= ( "the", "a", ); @prep_list= ( "above", "below", "at", "beyond", "before", "inside", ); srand time; # How many sentences? $num_of_sentences = rand(4)+2; for ($i=1; $i<$num_of_sentences; $i++) { $output = exec_rule("Q"); @word_list = split //, $output; foreach $word (@word_list) { $word = choose_word($word); } $word_list[0]="\u$word_list[0]"; $final .= join " ", @word_list; $final .= ". "; } print $final; # --------------------------------- sub exec_rule { my $input_symbol = shift; my $symbol; my @rules; if (exists $rule{$input_symbol}) # Get list of rules { @rules = split /\|/, $rule{$input_symbol}; } else # Bottoming out condition { return $input_symbol; } # Pick rule my $new_symbols = $rules[rand(@rules)]; # Call exec_rule for each symbol, combine results my @symbol_list = split //, $new_symbols; foreach $symbol (@symbol_list) { $symbol = exec_rule($symbol); } # return results return join( "", @symbol_list) } sub choose_word { my $input_symbol = shift; my $word = ${$s_map{$input_symbol}}[rand(@{$s_map{$input_symbol}}) +]; return $word; }
|
|---|