etherC has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl use strict; use warnings; my @det; #determiner my @pro; #pronoun my @properNoun; #proper noun my @conj; #conjunction my @prep; #preposition my @intrans; #intransitive verb my @ditrans; #ditransitive verb my @trans; #transitive verb my @adv; #adverb my @adj; #adjective my @mass; #mass noun my @count; #count noun my @aux; #auxilliary my @noun; #regular noun my @pluralNoun; #plural noun @det = ( 'the', 'a', 'ninety' ); @pro = ( 'it', 'I', 'ya', 'theirs' ); @properNoun = ( 'Alpesh', 'Ann', 'China', 'Beijing' ); @conj = ( 'and', 'that', 'provided that' ); @prep = ( 'of', 'in', 'underneath', 'relative to' ); @intrans = ( 'die', 'sleep', 'paint', 'travel' ); @trans = ( 'edit', 'hold', 'make', 'like' ); @ditrans = ( 'advise', 'ask', 'tell', 'urge' ); @adv = ( 'accidentally', 'afterwards', 'yearly', 'yesterday' ); @adj = ( 'average', 'big', 'voiceless', 'whispering' ); @noun = ( 'assistance', 'attempt', 'breadwinner', 'breakfast' ); @pluralNoun = ( 'afternoons', 'airmail', 'breadwinners', 'breakfasts' ); @mass = ( 'wood', 'cloth', 'homework', 'advice' ); @count = ( 'pen', 'table', 'taboo', 'rest' ); @aux = ( 'will', 'would', 'could', 'must' ); print "How many sentences would you like to generate? "; chomp(my $num_to_gen = <STDIN>); print "\nYou entered: $num_to_gen"; my $randnum; my @np; my @vp; my @pp; my $adjp; my $sentence; sub createADJP{ $randnum = int(rand(2)+1); if ($randnum == 1) { $adjp = $adv[int(rand($#adv + 1))] . ' ' . $adj[int(rand($ +#adj + 1))]; return $adjp; } else { $adjp = $adj[int(rand($#adj + 1))]; return $adjp; } } sub createNP{ my(@adjp); while ((my$counter = 0) < 100) { $randnum = int(rand(5)+1); if ($randnum == 1) { $randnum = int(rand(2)+1); if ($randnum == 1) { $np[$counter] = $det[int(rand($#det + 1))] . ' ' . + $adjp . ' ' . $noun[int(rand($#noun + 1))]; } else { $np[$counter] = $det[int(rand($#det + 1))] . ' ' . + $noun[int(rand($#noun + 1))]; } } elsif ($randnum == 2) { $np[$counter] = $pro[int(rand($#pro + 1))]; } elsif ($randnum == 3) { $np[$counter] = $properNoun[int(rand($#properNoun + 1))]; } elsif ($randnum == 4) { $randnum = int(rand(4)+1); if ($randnum == 1) { $np[$counter] = $det[int(rand($#det + 1))] . ' ' . + $adjp . ' ' . $pluralNoun[int(rand($#pluralNoun + 1))]; } elsif ($randnum == 2) { $np[$counter] = $det[int(rand($#det + 1))] . ' ' . + $pluralNoun[int(rand($#pluralNoun + 1))]; } elsif ($randnum == 3) { $np[$counter] = $adjp . ' ' . $pluralNoun[int(rand +($#pluralNoun + 1))]; } else { $np[$counter] = $pluralNoun[int(rand($#pluralNoun ++ 1))]; } } else { $randnum = int(rand(4)+1); if ($randnum == 1) { $np[$counter] = $det[int(rand($#det + 1))] . ' ' . + $adjp . ' ' . $mass[int(rand($#mass + 1))]; } elsif ($randnum == 2) { $np[$counter] = $det[int(rand($#det + 1))] . ' ' . + $mass[int(rand($#mass + 1))]; } elsif ($randnum == 3) { $np[$counter] = $adjp . ' ' . $mass[int(rand($#mas +s + 1))]; } else { $np[$counter] = $mass[int(rand($#mass + 1))]; } } $counter += 1; } return @np; } sub createVP{ my(@np); while ((my$counter = 0) < 100) { $randnum = int(rand(3)+1); if ($randnum == 1) { $vp[$counter] = $intrans[int(rand($#intrans + 1))]; } elsif ($randnum == 2) { $vp[$counter] = $trans[int(rand($#trans + 1))] . ' ' . $np +[int(rand($#np + 1))]; } else { $vp[$counter] = $ditrans[int(rand($#ditrans + 1))] . ' ' . + $np[int(rand($#np + 1))] . ' ' . $np[int(rand($#np + 1))]; } $counter+= 1; } return @vp; } sub createPP{ my(@np); while ((my $counter = 0) < 100) { $pp[$counter] = $prep[int(rand($#prep + 1))] . ' ' . $np[i +nt(rand($#np + 1))]; $counter += 1; } return @pp; } sub createSentence{ $randnum = int(rand(2)+1); my(@np,@vp); if ($randnum == 1) { $sentence = ($np[int(rand($#np + 1))] . ' ' . $vp[int(rand +($#vp + 1))]); return $sentence; } else { $sentence = ($np[int(rand($#np + 1))] . ' ' . $vp[int(rand +($#vp + 1))] . ' ' . $conj[int(rand($#conj + 1))] . ' ' . $np[int(ran +d($#np + 1))] . ' ' . $vp[int(rand($#vp + 1))]); return $sentence; } } &createADJP; &createNP($adjp); &createVP(@np); &createPP(@np); &createSentence(@np, @vp); my$counter; while (($counter = 0) < 100) # adds PPs to NP array, every 7th +one { $np[$counter] = $np[$counter] . ' ' . createPP($pp[int(rand($#pp + + 1))]); ; # $counter +=7; } while (($counter = 1) < 100) # adds CONJs to NP array, every 7t +h one { $np[$counter] = $np[$counter] . ' ' . $conj[int(rand($#conj + 1))] + . ' ' . $np[int(rand($#np + 1))]; $counter +=7; } while (($counter = 0) < 100) # adds AUXs to VP array, every 5th + one { $vp[$counter] = $aux[int(rand($#aux + 1))] . ' ' . $vp[$counter]; $counter +=5; } while (($counter = 1) < 100) # adds PPs to VP array, every 5th +one { $vp[$counter] = $vp[$counter] . ' ' . createPP($pp[int(rand($#pp + + 1))]); $counter +=5; } if (int(rand(1))) { print &createSentence; } else { print "\n" . $sentence . ' ' . $conj[int(rand($#conj + 1))] . ' ' +. $sentence; } <STDIN>
2005-01-19 Janitored by Arunbear - added readmore tags, as per Monastery guidelines
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: hanging at odd place, can still input but see nothing on screen (newbie)
by g0n (Priest) on Jan 19, 2005 at 11:03 UTC | |
by etherC (Initiate) on Jan 19, 2005 at 13:19 UTC | |
|
Re: hanging at odd place, can still input but see nothing on screen (newbie)
by osunderdog (Deacon) on Jan 19, 2005 at 15:04 UTC | |
|
Re: hanging at odd place, can still input but see nothing on screen (newbie)
by sasikumar (Monk) on Jan 19, 2005 at 10:52 UTC |