File Coverage

blib/lib/Acme/Yoda.pm
Criterion Covered Total %
statement 4 6 66.6
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 6 8 75.0


line stmt bran cond sub pod time code
1              
2             package Acme::Yoda;
3 2     2   25520 use strict;
  2         5  
  2         59  
4 2     2   1913 use Lingua::LinkParser;
  0            
  0            
5              
6             BEGIN {
7             use vars qw ($VERSION);
8             $VERSION = 0.02;
9             }
10              
11              
12             ########################################### main pod documentation begin ##
13             # Below is the stub of documentation for your module. You better edit it!
14              
15              
16             =head1 NAME
17              
18             Acme::Yoda -
19              
20             =head1 SYNOPSIS
21              
22             use Acme::Yoda;
23              
24              
25             =head1 DESCRIPTION
26              
27             Acme::Yoda translates back and forth from yoda speak.
28              
29              
30             =head1 USAGE
31            
32             use Acme::Yoda;
33              
34             my $y = Acme::Yoda->new();
35             my $translated = $y->yoda('I am your father');
36             my $back_again = $y->deyoda($translated)
37              
38            
39             =head1 BUGS
40             Right now Acme::Yoda does not handle contractions nor does it have a
41             comprehensive list of verbs.
42              
43             You can only deyoda sentences you have yoda'ed, since I have no
44             reliable way to discern the subject of the sentence.
45              
46             Both issues need to be fixed.
47              
48              
49             =head1 SUPPORT
50              
51             email me if you need help.
52              
53              
54              
55             =head1 AUTHOR
56              
57             Christian Brink
58             GREP
59             cbrink@flylines.org
60             http://www.yoda-speak.org
61              
62             =head1 COPYRIGHT
63              
64             This program is free software; you can redistribute
65             it and/or modify it under the same terms as Perl itself.
66              
67             The full text of the license can be found in the
68             LICENSE file included with this module.
69              
70              
71             =head1 SEE ALSO
72              
73             perl(1).
74              
75             =cut
76              
77             ############################################# main pod documentation end ##
78              
79              
80             ################################################ subroutine header begin ##
81              
82             =head2 new()
83              
84             Usage : new( sentence => 'I am a sentence') or just new();
85             Purpose : constructor
86             Returns : Acme::Yoda object
87             Argument : can take a sentence
88              
89             =cut
90              
91             my $_link = Lingua::LinkParser->new(verbosity => 0,
92             display_walls => 0);
93              
94             sub new {
95             my $class = shift;
96             my %args = @_;
97             $args{_link_parser} = $_link;
98            
99             my $self = bless \%args, ref($class) || $class ;
100              
101             return $self;
102             }
103              
104              
105              
106             =head2 yoda()
107              
108             Usage : yoda('sentence')
109             Purpose : Translates your sentenece into yoda speak
110             Returns : string
111             Argument : string
112             Comments : You can sent the sentence in new() or send it here
113              
114             =cut
115             sub yoda {
116             my $self = shift;
117             my $sentence = shift() || '';
118             $sentence = $sentence || $self->{sentence};
119              
120             return if (!$sentence);
121             $self->{sentence} = $sentence;
122              
123             my $ending;
124             if ($sentence =~ /([.?!])$/) {
125             chomp($sentence);
126             $ending = chop($sentence);
127             }
128              
129             if (!$self->{_no_contractions}) {
130             my %contractions = ( "I'll" => "I will",
131             "I'm" => "I am",
132             "he'll" => "he will",
133             "you'll" => "you will",
134             "doesn't" => "does not",
135             "can't" => "can not",
136             "aren't" => "are not",
137             "I've" => "I have",
138             "we've" => "we have",
139             "they've" => "they have",
140             "he's" => "he is",
141             "she's" => "she is",
142             "isn't" => "is not",
143             "you're" => "you are",
144             "where's" => "where is",
145             "we'll" => "we will",
146             "they'll" => "they will",
147             "didn't" => "did not",
148             "I'd" => "i would",
149             "he'd" => "he would",
150             "she'd" => "she would",
151             "its" => "it is"
152             );
153            
154             foreach (keys %contractions) {
155             if ($sentence =~ /\b\Q$_\E\b/ig) {
156             $sentence =~ s/\b\Q$_\E\b/$contractions{$_}/ige;
157             }
158             }
159             }
160              
161             # Find out if I have a pivot word and grab the one with the lowest index
162             my $pivot = $self->_get_pivot();
163              
164             return $sentence if (!$pivot);
165             if (index(lc($sentence),$pivot) == 0) {
166             $sentence = substr($sentence,,length($pivot)+1);
167             $ending = '?';
168             } else {
169             return $sentence unless ($sentence=~/\b$pivot\b/);
170             $sentence="$' $`$&";
171             }
172             # Clear leading spaces
173             $sentence =~ s/^\s+//;
174              
175             # Sentence case
176             $sentence = ucfirst(lc($sentence));
177             $sentence =~ s/\bi\b/I/g;
178             $sentence .= $ending if ($ending);
179             return $sentence;
180             }
181              
182             =head2 deyoda()
183              
184             Usage : deyoda('sentence')
185             Purpose : Translates your sentenece out of yoda speak
186             Returns : string
187             Argument : string
188             Comments :
189              
190             =cut
191             sub deyoda {
192             my $self = shift;
193             return $self->{sentence};
194              
195              
196             };
197              
198              
199              
200              
201             ################################################## subroutine header end ##
202              
203              
204             sub _get_pivot {
205             my $self = shift();
206            
207             my $parser = $self->{_link_parser};
208             my $s = $parser->create_sentence($self->{sentence});
209             my $pivot;
210              
211             my @linkages = $s->linkages;
212             LINK:
213             foreach my $linkage ($linkages[0]) {
214             return if (!$linkage);
215             my @words = $linkage->get_words();
216             foreach (@words) {
217             if (/^(\w+)\.v$/) {
218             $pivot = $1;
219             last LINK;
220             }
221             }
222             }
223             return $pivot;
224             }
225              
226             sub _get_last_pivot {
227             my $self = shift();
228            
229             my $parser = $self->{_link_parser};
230             my $s = $parser->create_sentence($self->{sentence});
231             my $pivot;
232              
233             my @linkages = $s->linkages;
234             LINK:
235             foreach my $linkage ($linkages[0]) {
236             my @words = $linkage->get_words();
237             foreach (@words) {
238             if (/^(\w+)\.v$/) {
239             $pivot = $1;
240             }
241             }
242             }
243              
244             return $pivot;
245              
246              
247              
248              
249             }
250              
251             1; #this line is important and will help the module return a true value
252             __END__