Garbage generator

When your teacher asks you to write a script that randomly mixes grammatical parts of a sentence because you didn’t did your homework is what causes this kind of post.

Actually there were more restrictions. I had to do it in less than 10 minutes, the resulting sentence needed to be understandable despite the possible genre errors and finally, the sentence should be generated in Spanish.

The script is made in Perl. It was easy to build and luckily took me less than 10 minutes to entirely write both the code and the feeders (the files with the words to build the sentence).

[perl]
#!/usr/bin/perl

use warnings;
use strict;

sub main()
{
my @articles = open_file($ARGV[0]);
my @subjects = open_file($ARGV[1]);
my @verbs = open_file($ARGV[2]);
my @complements = open_file($ARGV[3]);

my %rules = ( ‘a’ => @articles,
‘s’ => @subjects,
‘v’ => @verbs,
‘c’ => @complements);

my @expr = split(/+/, $ARGV[4]);

for my $token ( @expr )
{
my $index = int(rand(scalar(@{$rules{$token}})));

my $word = @{$rules{$token}}[$index];
$word =~ s/n+/ /g;
print $word;
}

print “n”;

}

sub open_file($)
{
my $file = shift;

open(FILE, “< $file") || die "Error: $!n"; return ;
}

&main();
[/perl]

The program receives 5 parameters from command line: the articles file, the subjects file, the verbs file, the complements file and the expression which determines how to build the sentences combining the words.

[bash]

$ perl random-sentence.pl articles subjects verbs complements “a+s+v+c”

[/bash]

The above call produces mostly nonsense and funny sentences like:

– el edificio duerme todo al pedo
– un alumno estudia como la mierda
– la puerta rie
– las perro habla todo al pedo
– una profesor come cualquier cosa

It was a funny thing to do. I don’t know if the script worth a post in my blog but here it is and I laughed a lot 😀

7 thoughts on “Garbage generator

  1. Hello there! This publish couldn’t be composed any far better! Looking at by way of this publish reminds me of my earlier area mate! He often stored discussing this. I’ll ahead this short article to him. Rather certain he’ll have a very excellent examine. Thanks for sharing!

Leave a Reply

Your email address will not be published. Required fields are marked *