#!/usr/local/bin/perl -Tw ##Load up the Modules## use CGI; use LWP::UserAgent; use CGI::Carp "fatalsToBrowser"; use URI::URL; ##Get everything started up use strict; my $base; my $cgi = new CGI; my $ua = new LWP::UserAgent; my @words = qw(mucho taco grande enchilada burrito salsa); print $cgi->header(); ##Make sure they filled one of the forms out unless ($cgi->param('text') or $cgi->param('url')) { print< Spanglish Translator

Spanglish Translator

Please type the English text to be translated into Spanglish below.
Note: The Spanglish Translator works best with larger ammounts of text.


Or enter a webpage URL (Pages linked to by the initial page may or may not work, depending on the phase of the moon, the alignment of the planets, and the color of the t-shirt you wore last Thursday...)


Original Copyright: Mr_Person
Modified by: OvrLrd-Q

EOT } else { ##Looks like they filled the textarea one out if($cgi->param('text')) { ##Grab the Text and run it through the translator my $text = $cgi->param('text'); my $newtext = &translate($text); print< Spanglish Translator

Spanglish Translator

EOT print $newtext; print<
Original Copyright: Mr_Person
Modified by: OvrLrd-Q

EOT } else { ##Oh noes, they wanted a webpage! ##Start up a HTTP Request $ua->agent("Spanglish Translator/0.1 " . $ua->agent); my $req = new HTTP::Request GET => $cgi->param('url'); my $res = $ua->request($req); ##Check to see if we got it ok if ($res->is_success) { my $text = $res->content; ##Use the URL requested to form the base for converting from relative to absolute URLS $base = $cgi->param('url'); ##Do the URL Conversions $text =~ s#src=\"(.*?)\"#&resolveimg($1)#sige; $text =~ s#href\=\"(.*?)\"#&resolvehref($1)#sige; ##Shove the resulting page through the translate routine and print it my $newtext = &translate($text); print $newtext; } else { print "Unable to retrieve webpage.\n"; } } } sub translate { my $text = $_[0]; my $newtext; ##Ignore HTML tags, run everything else through the translation engine while($text =~ m/(.*?)((?:<.*?>)|(?:&.*?;))/scg) { $newtext .= &trans_engine($1); $newtext .= $2; } $text =~ m/(.*)/sg; $newtext .= &trans_engine($1); return $newtext; } sub trans_engine { my $text = $_[0]; my $newtext; ##Loop through every word we find while($text =~ m/(\W*)(\w*)(\W*)/g) { my $notword1 = $1; my $word = $2; my $notword2 = $3; ##If it's a short word, just ignore it if(length($word) < 3) { $newtext .= $notword1 . $word . $notword2; next; } ##one in three chance of getting el or la shoved in front of it if(int(rand(3)) == 0) { ##Try and Guess the masculine or femenine forms of words if($word =~ /(l|o|n|e|r|s)$/i) { $word = "el $word"; } elsif($word =~ /(d|ion|z|a)$/i) { $word = "la $word"; } else { if(int(rand(2)) == 0) { $word = "el $word"; } else { $word = "la $word"; } } ##For everything else, just shove an o or an a at the end } else { if(int(rand(3)) != 0) { $word .= "o"; } else { $word .= "a"; } } ##Recombine the new word back where it belongs $newtext .= $notword1 . $word . $notword2; ##one in ten chance of getting one of those magical words appended to the end if(int(rand(10)) == 0) { $newtext .= " " . $words[rand(@words)] . " "; } } return $newtext; } ##Subs to convert urls to absolute sub resolveimg { my $relative = shift; my $path = URI::URL->new($relative)->abs( $base, 1 ); $path = "src=\"$path\""; return $path; } sub resolvehref { my $relative = shift; my $path = URI::URL->new($relative)->abs( $base, 1 ); $path = "href=\"http://thedarkcitadel.com/cgi-bin/spanglish.cgi?url=$path\""; return $path; }