blib/lib/Word/Rhymes.pm

filter: Statementstmt 86.0% 148 / 172 Branchbran 85.7% 84 / 98 Conditioncond 85.0% 34 / 40 MC/DCmcdc 79.1% 19 / 24 Subroutinesub 90.9% 20 / 22 Podpod 100% 11 / 11 totaltotal 86.1% 316 / 367 CC 71 SCAR 23.9
CC
71
Cov
86%
print
56.0
fetch
12.2
_process
12.1
CRAP 10.9
↑ Summary
1 package Word::Rhymes;
2  
3 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 12 our $VERSION = '1.04';
7  
8 12 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 12 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
14 # Core
15 12 MIN_SCORE => 0,
16 MAX_SCORE => 1000000,
17 MIN_RESULTS => 1,
18 MAX_RESULTS => 1000,
19 MIN_SYLLABLES => 1,
20 MAX_SYLLABLES => 100,
21 MULTI_WORD => 0,
22 RETURN_RAW => 0,
23 MIN_LIMIT => 1,
24 MAX_LIMIT => 1000,
25  
26 # print() related
27 MAX_NUM_COLS => 8,
28 MIN_NUM_COLS => 7,
29 COL_DIVIDER => 15,
30 COL_PADDING => 3,
31 ROW_INDENT => ' ',
32  
33 # Sort by
34 SORT_BY_SCORE_DESC => 0x00, # Default
35 SORT_BY_SCORE_ASC => 0x01,
36 SORT_BY_ALPHA_DESC => 0x02,
37 SORT_BY_ALPHA_ASC => 0x03,
38  
39 12 };
40  
41 12 my $ua = LWP::UserAgent->new;
42  
43 # Public
44  
45 41 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
46 41 my ($class, %args) = @_;
47  
48 41 my $self = bless {}, $class;
49  
50 41 $self->_args(\%args);
51  
52 27 return $self;
53 }
54 28 ▶sub fetch {
Subroutine fetch: called CC=10 SCAR=25.0
Pod documented
55 28 my ($self, $word, $context) = @_;
56  
57 28 ▶ if (! defined $word) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $word) 1 27 28
58 1 croak("fetch() needs a word sent in");
59 }
60  
61 27 ▶ if (defined $context && $context !~ /^\w+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $context and not $context =~ /^\w+$/) 1 26 27
Condition66%2/3
defined $context and not $context =~ /^\w+$/
!ll&&!rl&&r
2601
MC/DC50%1/2
defined $context and not $context =~ /^\w+$/
defined $context not $context =~ /^\w+$/
Truth table66%2/3
A and not(B)
A= defined $contextB= not $context =~ /^\w+$/
ABresult
0X0
100
111
62 1 croak("context parameter must be an alpha word only.");
63 }
64  
65 26 my ($req, $response);
66  
67 26 ▶ if (! $self->file) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->file) 0 26 26
68 0 $req = HTTP::Request->new('GET', $self->_uri($word, $context));
69 0 $response = $ua->request($req);
70 }
71  
72 26 ▶ if ($self->file || $response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->file or $response->is_success) { } 26 0 26
Condition33%1/3
$self->file or $response->is_success
l!l&&r!l&&!r
2600
MC/DC0%0/2
$self->file or $response->is_success
$self->file $response->is_success
Truth table33%1/3
A or B
A= $self->fileB= $response->is_success
ABresult
1X1
011
000
73  
74 26 my $json;
75  
76 26 ▶ if ($self->file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->file) { } 26 0 26
77 {
78 26 local $/;
79 26 ▶ open my $fh, '<', $self->file or croak(
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $self->file or croak(sprintf("Can't open the data file '%s': $!", $self->file)) 26 0 26
80 sprintf("Can't open the data file '%s': $!", $self->file)
81 );
82 26 $json = <$fh>;
83 26 close $fh;
84 }
85 }
86 else {
87 0 $json = $response->decoded_content;
88 }
89  
90 26 my $result = decode_json $json;
91  
92 26 ▶ return $result if $self->return_raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->return_raw 2 24 26
93  
94 24 return $self->_process($result);
95 }
96 else {
97 0 print "Invalid response\n\n";
98 0 return undef;
99 }
100 }
101 122 ▶sub file {
Subroutine file: called CC=5 SCAR=16.1
Pod documented
102 122 my ($self, $file) = @_;
103  
104 122 ▶ if (defined $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file) 17 105 122
105 17 ▶ croak("File '$file' does not exist") if ! -e $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 2 15 17
106 15 ▶ croak("File '$file' is not a valid file") if ! -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 2 13 15
107 13 $self->{file} = $file;
108 }
109  
110 118 ▶ return $self->{file} // '';
Condition100%2/2
$self->{"file"} // ""
l!l
1171
MC/DC100%1/1
$self->{"file"} // ""
$self->{"file"}
Truth table100%2/2
A
A= $self->{"file"}
Aresult
11
00
111 }
112 1107 ▶sub limit {
Subroutine limit: called CC=6 SCAR=17.9
Pod documented
113 1107 my ($self, $limit) = @_;
114  
115 1107 ▶ if (defined $limit) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $limit) 1006 101 1107
116 1006 ▶ croak("limit must be an integer") if $limit !~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $limit =~ /^\d+$/ 2 1004 1006
117 1004 ▶ if ($limit < MIN_LIMIT || $limit > MAX_LIMIT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limit < 1 or $limit > 1000) 4 1000 1004
Condition100%3/3
$limit < 1 or $limit > 1000
l!l&&r!l&&!r
221000
MC/DC100%2/2
$limit < 1 or $limit > 1000
$limit < 1 $limit > 1000
Truth table100%3/3
A or B
A= $limit < 1B= $limit > 1000
ABresult
1X1
011
000
118 4 croak(
119 sprintf(
120 "limit must be between %d and %d",
121 MIN_LIMIT,
122 MAX_LIMIT
123 )
124 );
125 }
126 1000 $self->{limit} = $limit;
127 }
128  
129 1101 ▶ return $self->{limit} // MAX_LIMIT;
Condition100%2/2
$self->{"limit"} // 1000
l!l
1000101
MC/DC100%1/1
$self->{"limit"} // 1000
$self->{"limit"}
Truth table100%2/2
A
A= $self->{"limit"}
Aresult
11
00
130 }
131 1009 ▶sub max_results {
Subroutine max_results: called CC=6 SCAR=17.9
Pod documented
132 1009 my ($self, $max) = @_;
133  
134 1009 ▶ if (defined $max) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $max) 1006 3 1009
135 1006 ▶ croak("max_results must be an integer") if $max !~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $max =~ /^\d+$/ 2 1004 1006
136 1004 ▶ if ($max < MIN_RESULTS || $max > MAX_RESULTS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max < 1 or $max > 1000) 4 1000 1004
Condition100%3/3
$max < 1 or $max > 1000
l!l&&r!l&&!r
221000
MC/DC100%2/2
$max < 1 or $max > 1000
$max < 1 $max > 1000
Truth table100%3/3
A or B
A= $max < 1B= $max > 1000
ABresult
1X1
011
000
137 4 croak(
138 sprintf(
139 "max_results must be between %d and %d",
140 MIN_RESULTS,
141 MAX_RESULTS
142 )
143 );
144 }
145 1000 $self->{max_results} = $max;
146 }
147  
148 1003 ▶ return $self->{max_results} // MAX_RESULTS;
Condition100%2/2
$self->{"max_results"} // 1000
l!l
10003
MC/DC100%1/1
$self->{"max_results"} // 1000
$self->{"max_results"}
Truth table100%2/2
A
A= $self->{"max_results"}
Aresult
11
00
149 }
150 10259 ▶sub min_score {
Subroutine min_score: called CC=6 SCAR=17.9
Pod documented
151 10259 my ($self, $min) = @_;
152  
153 10259 ▶ if (defined $min) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $min) 1615 8644 10259
154 1615 ▶ croak("min_score must be an integer") if $min !~ /^-?\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $min =~ /^-?\d+$/ 2 1613 1615
155 1613 ▶ if ($min < MIN_SCORE || $min > MAX_SCORE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min < 0 or $min > 1000000) 4 1609 1613
Condition100%3/3
$min < 0 or $min > 1000000
l!l&&r!l&&!r
221609
MC/DC100%2/2
$min < 0 or $min > 1000000
$min < 0 $min > 1000000
Truth table100%3/3
A or B
A= $min < 0B= $min > 1000000
ABresult
1X1
011
000
156 4 croak(
157 sprintf(
158 "min_score must be between %d and %d",
159 MIN_SCORE,
160 MAX_SCORE
161 )
162 );
163 }
164 1609 $self->{min_score} = $min;
165 }
166  
167 10253 ▶ return $self->{min_score} // MIN_SCORE;
Condition100%2/2
$self->{"min_score"} // 0
l!l
42905963
MC/DC100%1/1
$self->{"min_score"} // 0
$self->{"min_score"}
Truth table100%2/2
A
A= $self->{"min_score"}
Aresult
11
00
168 }
169 9619 ▶sub min_syllables {
Subroutine min_syllables: called CC=6 SCAR=17.9
Pod documented
170 9619 my ($self, $min) = @_;
171  
172 9619 ▶ if (defined $min) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $min) 110 9509 9619
173 110 ▶ croak("min_syllables must be an integer") if $min !~ /^-?\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $min =~ /^-?\d+$/ 2 108 110
174 108 ▶ if ($min < MIN_SYLLABLES || $min > MAX_SYLLABLES) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min < 1 or $min > 100) 4 104 108
Condition100%3/3
$min < 1 or $min > 100
l!l&&r!l&&!r
22104
MC/DC100%2/2
$min < 1 or $min > 100
$min < 1 $min > 100
Truth table100%3/3
A or B
A= $min < 1B= $min > 100
ABresult
1X1
011
000
175 4 croak(
176 sprintf(
177 "min_syllables must be between %d and %d",
178 MIN_SYLLABLES,
179 MAX_SYLLABLES
180 )
181 );
182 }
183 104 $self->{min_syllables} = $min;
184 }
185  
186 9613 ▶ return $self->{min_syllables} // MIN_SYLLABLES;
Condition100%2/2
$self->{"min_syllables"} // 1
l!l
16367977
MC/DC100%1/1
$self->{"min_syllables"} // 1
$self->{"min_syllables"}
Truth table100%2/2
A
A= $self->{"min_syllables"}
Aresult
11
00
187 }
188 29 ▶sub multi_word {
Subroutine multi_word: called CC=3 SCAR=11.0
Pod documented
189 29 my ($self, $bool) = @_;
190  
191 29 ▶ if (defined $bool) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bool) 3 26 29
192 3 $self->{multi_word} = $bool;
193 }
194  
195 29 ▶ return $self->{multi_word} // MULTI_WORD;
Condition100%2/2
$self->{"multi_word"} // 0
l!l
524
MC/DC100%1/1
$self->{"multi_word"} // 0
$self->{"multi_word"}
Truth table100%2/2
A
A= $self->{"multi_word"}
Aresult
11
00
196 }
197 0 ▶sub print {
Subroutine print: not called CC=7 SCAR=40.3
Pod documented
198 0 my ($self, $word, $context) = @_;
199  
200 0 my $rhyming_words = $self->fetch($word, $context);
201  
202 0 ▶ print defined $context
Branch: 1 branch
BranchTrueFalseTotal
defined $context ? : 0 0 0
203 ? "\nRhymes with '$word' related to '$context'\n"
204 : "\nRhymes with '$word'\n";
205  
206 0 for my $num_syl (reverse sort keys %$rhyming_words) {
207 my $max_word_len = length(
208 0 (sort {length $b->{word} <=> length $a->{word}} @{ $rhyming_words->{$num_syl} })[0]->{word}
209 0 );
210  
211 0 my $column_width = $max_word_len + COL_PADDING;
212 0 ▶ my $columns = $column_width > COL_DIVIDER ? MIN_NUM_COLS : MAX_NUM_COLS;
Branch: 1 branch
BranchTrueFalseTotal
$column_width > 15 ? : 0 0 0
213  
214 0 printf "\nSyllables: $num_syl\n\n%s", ROW_INDENT;
215  
216 0 for (0 .. $#{ $rhyming_words->{$num_syl} }) {
217 0 ▶ printf "\n%s", ROW_INDENT if $_ % $columns == 0 && $_ != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $_ % $columns == 0 and $_ != 0 0 0 0
Condition0%0/3
$_ % $columns == 0 and $_ != 0
!ll&&!rl&&r
000
MC/DC0%0/2
$_ % $columns == 0 and $_ != 0
$_ % $columns == 0 $_ != 0
Truth table0%0/3
A and B
A= $_ % $columns == 0B= $_ != 0
ABresult
0X0
100
111
218 0 printf("%-*s", $column_width, $rhyming_words->{$num_syl}[$_]->{word});
219 }
220 0 print "\n";
221 }
222  
223 0 return 0;
224 }
225 28 ▶sub return_raw {
Subroutine return_raw: called CC=3 SCAR=11.0
Pod documented
226 28 my ($self, $ret) = @_;
227  
228 28 ▶ if (defined $ret) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ret) 2 26 28
229 2 $self->{return_raw} = $ret;
230 }
231  
232 28 ▶ return $self->{return_raw} // RETURN_RAW;
Condition100%2/2
$self->{"return_raw"} // 0
l!l
424
MC/DC100%1/1
$self->{"return_raw"} // 0
$self->{"return_raw"}
Truth table100%2/2
A
A= $self->{"return_raw"}
Aresult
11
00
233 }
234 294 ▶sub sort_by {
Subroutine sort_by: called CC=8 SCAR=20.9
Pod documented
235 294 my ($self, $sort_by) = @_;
236  
237 294 ▶ if (defined $sort_by) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sort_by) 7 287 294
238 7 ▶ if (! grep /^$sort_by$/, qw(score_desc score_asc alpha_desc alpha_asc)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep /^$sort_by$/, ("score_desc", "score_asc", "alpha_desc", "alpha_asc")) 1 6 7
239 1 croak("sort() needs 'score_desc', 'score_asc', 'alpha_desc' or 'alpha_asc' as param");
240 }
241  
242 6 ▶ if ($sort_by =~ /^alpha/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($sort_by =~ /^alpha/) { } 4 2 6
elsif ($sort_by =~ /^score/) { } 2 0 2
243 4 ▶ $self->{sort_by} = $sort_by =~ /desc/
Branch: 1 branch
BranchTrueFalseTotal
$sort_by =~ /desc/ ? : 2 2 4
244 ? SORT_BY_ALPHA_DESC
245 : SORT_BY_ALPHA_ASC;
246 }
247 elsif ($sort_by =~ /^score/) {
248 2 ▶ $self->{sort_by} = $sort_by =~ /desc/
Branch: 1 branch
BranchTrueFalseTotal
$sort_by =~ /desc/ ? : 0 2 2
249 ? SORT_BY_SCORE_DESC
250 : SORT_BY_SCORE_ASC;
251 }
252 }
253  
254 293 ▶ return $self->{sort_by} // SORT_BY_SCORE_DESC;
Condition100%2/2
$self->{"sort_by"} // 0
l!l
82211
MC/DC100%1/1
$self->{"sort_by"} // 0
$self->{"sort_by"}
Truth table100%2/2
A
A= $self->{"sort_by"}
Aresult
11
00
255 }
256  
257 # Private
258  
259 41 ▶sub _args {
Subroutine _args: called CC=9 SCAR=22.0
260 41 my ($self, $args) = @_;
261  
262 # file
263 41 ▶ $self->file($args->{file}) if exists $args->{file};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"file"} 15 26 41
264  
265 # limit
266 39 ▶ $self->limit($args->{limit}) if exists $args->{limit};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"limit"} 3 36 39
267  
268 # max_results
269 36 ▶ $self->max_results($args->{max_results}) if exists $args->{max_results};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"max_results"} 3 33 36
270  
271 # min_score
272 33 ▶ $self->min_score($args->{min_score}) if exists $args->{min_score};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"min_score"} 3 30 33
273  
274 # min_syllables
275 30 ▶ $self->min_syllables($args->{min_syllables}) if exists $args->{min_syllables};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"min_syllables"} 3 27 30
276  
277 # multi_word
278 27 ▶ $self->multi_word($args->{multi_word}) if exists $args->{multi_word};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"multi_word"} 2 25 27
279  
280 # return_raw
281 27 ▶ $self->return_raw($args->{return_raw}) if exists $args->{return_raw};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"return_raw"} 1 26 27
282  
283 # sort_by
284 27 ▶ $self->sort_by($args->{sort_by}) if exists $args->{sort_by};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"sort_by"} 3 24 27
285 }
286 24 ▶sub _process {
Subroutine _process: called CC=12 SCAR=24.9
287 24 my ($self, $result) = @_;
288  
289 24 my @data;
290  
291 # Dump rhyming words that don't have a score or are multi-word
292 24 ▶ if ($self->multi_word) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->multi_word) { } 1 23 24
293 1 @data = grep { $_->{score} } @$result;
294 }
295 else {
296 23 ▶ @data = grep { $_->{score} && $_->{word} !~ /\s+/ } @$result;
Condition100%3/3
$_->{'score'} && !($_->{'word'} =~ /\s+/)
!ll&&!rl&&r
239272688809
MC/DC100%2/2
$_->{'score'} && !($_->{'word'} =~ /\s+/)
$_->{'score'} !($_->{'word'} =~ /\s+/)
Truth table100%3/3
A && not(B)
A= $_->{'score'}B= !($_->{'word'} =~ /\s+/)
ABresult
0X0
100
111
297 }
298  
299 # Dump rhyming words that are outside of min_syllables threshold
300 24 @data = grep { $_->{numSyllables} >= $self->min_syllables } @data;
301  
302 24 my @sorted = sort {$b->{numSyllables} <=> $a->{numSyllables}} @data;
303 24 my %organized;
304  
305 24 for (@sorted) {
306 8643 ▶ push @{ $organized{$_->{numSyllables}} }, $_ if $_->{score} >= $self->min_score;
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"score"} >= $self->min_score 6497 2146 8643
307 }
308  
309 24 for (keys %organized) {
310 100 ▶ if ($self->sort_by == SORT_BY_ALPHA_DESC) {
Branch: 4 branches
BranchTrueFalseTotal
if ($self->sort_by == 2) { } 10 90 100
elsif ($self->sort_by == 3) { } 10 80 90
elsif ($self->sort_by == 0) { } 70 10 80
elsif ($self->sort_by == 1) { } 10 0 10
311 10 @{ $organized{$_} } = sort {$b->{word} cmp $a->{word}} @{ $organized{$_} };
312 }
313 elsif ($self->sort_by == SORT_BY_ALPHA_ASC) {
314 10 @{ $organized{$_} } = sort {$a->{word} cmp $b->{word}} @{ $organized{$_} };
315 }
316 elsif ($self->sort_by == SORT_BY_SCORE_DESC) {
317 70 @{ $organized{$_} } = sort {$b->{score} <=> $a->{score}} @{ $organized{$_} };
318 }
319 elsif ($self->sort_by == SORT_BY_SCORE_ASC) {
320 10 @{ $organized{$_} } = sort {$a->{score} <=> $b->{score}} @{ $organized{$_} };
321 }
322 }
323  
324 # Limit the result count in each syllable href
325  
326 24 for (keys %organized) {
327 100 ▶ next if scalar @{ $organized{$_} } <= $self->limit;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$organized{$_};} <= $self->limit 100 0 100
328 0 @{ $organized{$_} } = @{ $organized{$_} }[0..$self->limit -1];
329 }
330  
331 24 return \%organized;
332  
333 }
334 2 ▶sub _uri {
Subroutine _uri: called CC=2 SCAR=6.9
335 2 my ($self, $word, $context) = @_;
336  
337 2 my $uri;
338  
339 2 ▶ if (defined $context) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $context) { } 1 1 2
340 1 $uri = sprintf(
341 "http://api.datamuse.com/words?max=%d&ml=%s&rel_rhy=%s",
342 $self->max_results,
343 $context,
344 $word
345 );
346 }
347 else {
348 1 $uri = sprintf(
349 "http://api.datamuse.com/words?max=%d&rel_rhy=%s",
350 $self->max_results,
351 $word
352 );
353 }
354  
355 2 return $uri;
356 }
357 0 ▶sub __placeholder {}
Subroutine __placeholder: not called CC=1 SCAR=0.0
358  
359 12 1;
360 __END__
↑ Summary