Created
August 28, 2011 00:44
-
-
Save delonnewman/1176072 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
# Description: Converts given English text into Pig Latin | |
# Delon Newman <[email protected]> Copyright (C) 2007 | |
$/=""; | |
my $text = <>; | |
translate(\$text, create_dictionary($text)); | |
sub translate { | |
my $text = shift; | |
my $dict = shift; | |
print "Beginning to translate...\n"; | |
foreach (keys %$dict) { $$text =~ s/\b$_\b/$dict->{$_}/g } | |
print "Translation complete.\n"; | |
print $$text; | |
} | |
sub create_dictionary { | |
my $text = shift; | |
$text =~ s/[\,\.\:\;\-\?\!\%\*\#\^\(\)\@\$\+="\[\]{}\\\|\'\/]/ /g; | |
my @words = split /[\s\n]+/, $text; | |
my %dict; | |
foreach (@words) { | |
next if /\d/; | |
next unless $_; | |
next if $dict{$_}; | |
if (starts_with_vowl($_)) { | |
$dict{$_} = $_; | |
} else { | |
# FIXME: This should exclude y in | |
# cases where it is a vowl | |
m/([b-df-hj-np-tv-z]+)/i; | |
$dict{$_} = $_ . lc($1); | |
$dict{$_} =~ s/$1//i; | |
} | |
if (/[A-Z]{2,}/) { $dict{$_} .= 'AY' } | |
else { $dict{$_} .= 'ay' } | |
$dict{$_} = ucfirst($dict{$_}) if (/^[A-Z]/); | |
} | |
print "Dictionary complete.\n"; | |
print int(keys %dict) . " words.\n"; | |
return \%dict; | |
} | |
sub _starts_with { | |
my $word = shift; | |
my @letters = @_; | |
map { return 1 if ($word =~ /^$_/i) } @letters; | |
return 0; | |
} | |
sub starts_with_vowl { | |
my $word = shift; | |
my @vowls = qw{a e i o u}; | |
return _starts_with($word, @vowls); | |
} | |
sub starts_with_consonant { | |
my $word = shift; | |
my @consonants = qw{b c d f g h j k l m n p q r s t v w x y z}; | |
return _starts_with($word, @consonants); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment