Category: | Fun Stuff |
Author/Contact Info | Brent Garber Curtis Hawthorne |
Description: | A Fun script that was coded when we were taking Spanish Classes. What this script does is translate a selection of text, or an URL into Spanglish, which is a hybrid of English and Spanish. A Working example can be found here |
#!/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<<EOT; <html> <head> <title>Spanglish Translator</title> </head> <body bgcolor="black" text="white"> <center> <h1>Spanglish Translator</h1> </center> <p>Please type the English text to be translated into Spanglish below. +<br> Note: The Spanglish Translator works best with larger ammounts of text +. <form action="spanglish.cgi" method="post"> <p><textarea cols="40" rows="10" name="text" wrap="virtual"></textarea +><br> <input type="submit" value="Translate"> </form> <p>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 th +e planets, and the color of the t-shirt you wore last Thursday...)<br +> <form action="spanglish.cgi" method="post"> <input type="text" name="url" value="http://"><br> <input type="submit" value="Translate"> </form> <p><center>Original Copyright: <a href="http://www.mrperson.org/">Mr_P +erson</a><br />Modified by: <a href="http://www.remnetworks.org">OvrL +rd-Q</a></center></p> </body> </html> 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<<EOT; <html> <head> <title>Spanglish Translator</title> </head> <body bgcolor="black" text="white"> <center> <h1>Spanglish Translator</h1> </center> EOT print $newtext; print<<EOT; <p><center>Original Copyright: <a href="http://www.mrperson.org/">Mr_P +erson</a><br />Modified by: <a href="http://www.remnetworks.org">OvrL +rd-Q</a></center></p> </body> </html> 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 r +elative 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 p +rint 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 en +gine 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 i +t 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 appe +nded 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?ur +l=$path\""; return $path; } |
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Spanglish Translator
by Anonymous Monk on Jan 29, 2009 at 10:47 UTC | |
by OverlordQ (Hermit) on Mar 24, 2009 at 01:28 UTC |