Acme-Yoda
view release on metacpan or search on metacpan
lib/Acme/Yoda.pm view on Meta::CPAN
=head2 yoda()
Usage : yoda('sentence')
Purpose : Translates your sentenece into yoda speak
Returns : string
Argument : string
Comments : You can sent the sentence in new() or send it here
=cut
sub yoda {
my $self = shift;
my $sentence = shift() || '';
$sentence = $sentence || $self->{sentence};
return if (!$sentence);
$self->{sentence} = $sentence;
my $ending;
if ($sentence =~ /([.?!])$/) {
chomp($sentence);
$ending = chop($sentence);
}
if (!$self->{_no_contractions}) {
my %contractions = ( "I'll" => "I will",
"I'm" => "I am",
"he'll" => "he will",
"you'll" => "you will",
"doesn't" => "does not",
"can't" => "can not",
"aren't" => "are not",
"I've" => "I have",
"we've" => "we have",
"they've" => "they have",
"he's" => "he is",
"she's" => "she is",
"isn't" => "is not",
"you're" => "you are",
"where's" => "where is",
"we'll" => "we will",
"they'll" => "they will",
"didn't" => "did not",
"I'd" => "i would",
"he'd" => "he would",
"she'd" => "she would",
"its" => "it is"
);
foreach (keys %contractions) {
if ($sentence =~ /\b\Q$_\E\b/ig) {
$sentence =~ s/\b\Q$_\E\b/$contractions{$_}/ige;
}
}
}
# Find out if I have a pivot word and grab the one with the lowest index
my $pivot = $self->_get_pivot();
return $sentence if (!$pivot);
if (index(lc($sentence),$pivot) == 0) {
$sentence = substr($sentence,,length($pivot)+1);
$ending = '?';
} else {
return $sentence unless ($sentence=~/\b$pivot\b/);
$sentence="$' $`$&";
}
# Clear leading spaces
$sentence =~ s/^\s+//;
# Sentence case
$sentence = ucfirst(lc($sentence));
$sentence =~ s/\bi\b/I/g;
$sentence .= $ending if ($ending);
return $sentence;
}
=head2 deyoda()
Usage : deyoda('sentence')
Purpose : Translates your sentenece out of yoda speak
Returns : string
Argument : string
Comments :
=cut
sub deyoda {
my $self = shift;
return $self->{sentence};
};
################################################## subroutine header end ##
sub _get_pivot {
my $self = shift();
my $parser = $self->{_link_parser};
my $s = $parser->create_sentence($self->{sentence});
my $pivot;
my @linkages = $s->linkages;
LINK:
foreach my $linkage ($linkages[0]) {
return if (!$linkage);
my @words = $linkage->get_words();
foreach (@words) {
if (/^(\w+)\.v$/) {
$pivot = $1;
last LINK;
}
}
}
return $pivot;
}
sub _get_last_pivot {
( run in 1.286 second using v1.01-cache-2.11-cpan-364913b4093 )