| 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
| |||||||||||||||||||||||||||||
| 58 | 1 | croak("fetch() needs a word sent in"); | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 27 | ▶ | if (defined $context && $context !~ /^\w+$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $context and not $context =~ /^\w+$/
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+$/
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
$self->file or $response->is_success
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
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 26 | my $json; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 26 | ▶ | if ($self->file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | { | ||||||||||||||||||||||||||||
| 78 | 26 | local $/; | |||||||||||||||||||||||||||
| 79 | 26 | ▶ | open my $fh, '<', $self->file or croak( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 105 | 17 | ▶ | croak("File '$file' does not exist") if ! -e $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 15 | ▶ | croak("File '$file' is not a valid file") if ! -f $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 13 | $self->{file} = $file; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 118 | ▶ | return $self->{file} // ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"file"} // ""
MC/DC100%1/1
$self->{"file"} // ""
$self->{"file"}
Truth table100%2/2
A
A= $self->{"file"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 116 | 1006 | ▶ | croak("limit must be an integer") if $limit !~ /^\d+$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 1004 | ▶ | if ($limit < MIN_LIMIT || $limit > MAX_LIMIT) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$limit < 1 or $limit > 1000
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
| |||||||||||||||||||||||||||||
| 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
MC/DC100%1/1
$self->{"limit"} // 1000
$self->{"limit"}
Truth table100%2/2
A
A= $self->{"limit"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 135 | 1006 | ▶ | croak("max_results must be an integer") if $max !~ /^\d+$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 1004 | ▶ | if ($max < MIN_RESULTS || $max > MAX_RESULTS) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$max < 1 or $max > 1000
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
| |||||||||||||||||||||||||||||
| 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
MC/DC100%1/1
$self->{"max_results"} // 1000
$self->{"max_results"}
Truth table100%2/2
A
A= $self->{"max_results"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 154 | 1615 | ▶ | croak("min_score must be an integer") if $min !~ /^-?\d+$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 1613 | ▶ | if ($min < MIN_SCORE || $min > MAX_SCORE) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$min < 0 or $min > 1000000
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
| |||||||||||||||||||||||||||||
| 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
MC/DC100%1/1
$self->{"min_score"} // 0
$self->{"min_score"}
Truth table100%2/2
A
A= $self->{"min_score"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 173 | 110 | ▶ | croak("min_syllables must be an integer") if $min !~ /^-?\d+$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 108 | ▶ | if ($min < MIN_SYLLABLES || $min > MAX_SYLLABLES) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$min < 1 or $min > 100
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
| |||||||||||||||||||||||||||||
| 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
MC/DC100%1/1
$self->{"min_syllables"} // 1
$self->{"min_syllables"}
Truth table100%2/2
A
A= $self->{"min_syllables"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 192 | 3 | $self->{multi_word} = $bool; | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 29 | ▶ | return $self->{multi_word} // MULTI_WORD; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"multi_word"} // 0
MC/DC100%1/1
$self->{"multi_word"} // 0
$self->{"multi_word"}
Truth table100%2/2
A
A= $self->{"multi_word"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
$_ % $columns == 0 and $_ != 0
MC/DC0%0/2
$_ % $columns == 0 and $_ != 0
$_ % $columns == 0
$_ != 0
Truth table0%0/3
A and B
A= $_ % $columns == 0B= $_ != 0
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 229 | 2 | $self->{return_raw} = $ret; | |||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 28 | ▶ | return $self->{return_raw} // RETURN_RAW; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"return_raw"} // 0
MC/DC100%1/1
$self->{"return_raw"} // 0
$self->{"return_raw"}
Truth table100%2/2
A
A= $self->{"return_raw"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 238 | 7 | ▶ | if (! grep /^$sort_by$/, qw(score_desc score_asc alpha_desc alpha_asc)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 243 | 4 | ▶ | $self->{sort_by} = $sort_by =~ /desc/ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
MC/DC100%1/1
$self->{"sort_by"} // 0
$self->{"sort_by"}
Truth table100%2/2
A
A= $self->{"sort_by"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | # limit | ||||||||||||||||||||||||||||
| 266 | 39 | ▶ | $self->limit($args->{limit}) if exists $args->{limit}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | # max_results | ||||||||||||||||||||||||||||
| 269 | 36 | ▶ | $self->max_results($args->{max_results}) if exists $args->{max_results}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | # min_score | ||||||||||||||||||||||||||||
| 272 | 33 | ▶ | $self->min_score($args->{min_score}) if exists $args->{min_score}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | # min_syllables | ||||||||||||||||||||||||||||
| 275 | 30 | ▶ | $self->min_syllables($args->{min_syllables}) if exists $args->{min_syllables}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | # multi_word | ||||||||||||||||||||||||||||
| 278 | 27 | ▶ | $self->multi_word($args->{multi_word}) if exists $args->{multi_word}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | # return_raw | ||||||||||||||||||||||||||||
| 281 | 27 | ▶ | $self->return_raw($args->{return_raw}) if exists $args->{return_raw}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | # sort_by | ||||||||||||||||||||||||||||
| 284 | 27 | ▶ | $self->sort_by($args->{sort_by}) if exists $args->{sort_by}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 293 | 1 | @data = grep { $_->{score} } @$result; | |||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||
| 295 | else { | ||||||||||||||||||||||||||||
| 296 | 23 | ▶ | @data = grep { $_->{score} && $_->{word} !~ /\s+/ } @$result; | ||||||||||||||||||||||||||
|
Condition100%3/3
$_->{'score'} && !($_->{'word'} =~ /\s+/)
MC/DC100%2/2
$_->{'score'} && !($_->{'word'} =~ /\s+/)
$_->{'score'}
!($_->{'word'} =~ /\s+/)
Truth table100%3/3
A && not(B)
A= $_->{'score'}B= !($_->{'word'} =~ /\s+/)
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | 24 | for (keys %organized) { | |||||||||||||||||||||||||||
| 310 | 100 | ▶ | if ($self->sort_by == SORT_BY_ALPHA_DESC) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||