Anagrams.pm

filter: Statementstmt 99.4% 352 / 354 Branchbran 84.5% 120 / 142 Conditioncond 75.0% 33 / 44 MC/DCmcdc 38.4% 10 / 26 Subroutinesub 96.8% 31 / 32 Podpod 100% 5 / 5 totaltotal 91.3% 551 / 603 SCAR 48.1
CC
119
Cov
94%
anagrams
18.3
_all_touched
14.1
new
13.2
CRAP 122.9
↑ Summary cleaner.pl »
1 package Lingua::Anagrams;
2 2 $Lingua::Anagrams::VERSION = '0.018';
3 # ABSTRACT: pure Perl anagram finder
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use List::MoreUtils qw(uniq);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 # don't cache anagrams for bigger character counts than this
12 2 our $LIMIT = 20;
13  
14 # some global variables to be localized
15 # used to limit time spent copying values
16 2 our ( $limit, $known, $trie, %cache, $cleaner, @jumps, @indices );
17  
18  
19 sub new {
20 12 ▶ my $class = shift;
Subroutine new: called CC=13 SCAR=25.8
Pod documented
21 12 my $wl = shift;
22 12 ▶ die 'first parameter expected to be an array reference'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $wl eq "ARRAY" 0 12 12
23 unless ref $wl eq 'ARRAY';
24 12 my %params = _make_opts(@_);
25 12 ▶ $class = ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0120
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
26 12 ▶ local $cleaner = $params{clean} // \&_clean;
Condition50%1/2
$params{"clean"} // \&Lingua::Anagrams::_clean
l!l
012
MC/DC0%0/1
$params{"clean"} // \&Lingua::Anagrams::_clean
$params{"clean"}
Truth table50%1/2
A
A= $params{"clean"}
Aresult
11
00
27 12 my @word_lists;
28 12 ▶ if ( ref $wl->[0] eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $wl->[0] eq 'ARRAY') { } 5 7 12
29 5 @word_lists = @$wl;
30 }
31 else {
32 7 @word_lists = ($wl);
33 }
34 12 _validate_lists( \@word_lists );
35 7 my $translator = { '' => 0 };
36 7 $translator->{$_} = scalar keys %$translator for @{ $word_lists[-1] };
37 7 my $offset; # used to reduce number of undef cells in tries and elsewhere
38 7 for my $w ( @{ $word_lists[-1] } ) {
39 31 my @ords = map ord, split //, $w;
40 31 for my $o (@ords) {
41 44 ▶ if ( defined $offset ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $offset) { } 37 7 44
42 37 ▶ $offset = $o if $o < $offset;
Branch: 1 branch
BranchTrueFalseTotal
if $o < $offset 1 36 37
43 }
44 else {
45 7 $offset = $o;
46 }
47 }
48 }
49 7 --$offset;
50 7 my @tries;
51 7 for my $words (@word_lists) {
52 8 my ( $trie, $known ) = _trieify( $words, $translator, $offset );
53 8 push @tries, [ $trie, $known ];
54 }
55 7 $translator = [ '', @{ $word_lists[-1] } ];
56 return bless {
57 limit => $params{limit} // $LIMIT,
58 sorted => $params{sorted} // 0,
59 min => $params{min},
60 7 ▶ clean => $cleaner,
Condition80%4/5
$params{"limit"} // $Lingua::Anagrams::LIMIT
l!l&&r!l&&!r
160
$params{"sorted"} // 0
l!l
16
MC/DC0%0/2
$params{"limit"} // $Lingua::Anagrams::LIMIT
$params{"limit"} $Lingua::Anagrams::LIMIT
MC/DC100%1/1
$params{"sorted"} // 0
$params{"sorted"}
Truth table66%2/3
A // B
A= $params{"limit"}B= $Lingua::Anagrams::LIMIT
ABresult
1X1
011
000
Truth table100%2/2
A
A= $params{"sorted"}
Aresult
11
00
61 tries => \@tries,
62 translator => $translator,
63 offset => $offset,
64 },
65 $class;
66 }
67  
68 # there should be no empty lists and each list should be subsumed
69 # by the next
70 sub _validate_lists {
71 12 ▶ my $lists = shift;
Subroutine _validate_lists: called CC=8 SCAR=20.8
72 12 for my $i ( 0 .. $#$lists ) {
73 my @list = uniq grep length,
74 16 ▶ map { my $v = $_ // ''; $cleaner->($v); $v } @{ $lists->[$i] };
Condition50%1/2
$_ // ""
l!l
400
MC/DC0%0/1
$_ // ""
$_
Truth table50%1/2
A
A= $_
Aresult
11
00
75 16 ▶ die 'empty list' unless @list;
Branch: 1 branch
BranchTrueFalseTotal
unless @list 2 14 16
76 14 $lists->[$i] = \@list;
77 }
78 10 for my $i ( 1 .. $#$lists ) {
79 4 my ( $prior, $list ) = @$lists[ $i - 1, $i ];
80 4 ▶ die 'lists misordered by length' if @$prior >= @$list;
Branch: 1 branch
BranchTrueFalseTotal
if @$prior >= @$list 2 2 4
81 2 my %set = map { $_ => 1 } @$list;
82 2 for my $word (@$prior) {
83 2 ▶ die 'smaller lists must be subsumed by larger' unless $set{$word};
Branch: 1 branch
BranchTrueFalseTotal
unless $set{$word} 1 1 2
84 }
85 }
86 }
87  
88 sub _trieify {
89 8 ▶ my ( $words, $translator, $offset ) = @_;
Subroutine _trieify: called CC=4 SCAR=14.0
90 8 my $base = [];
91 8 my @known;
92 8 for my $word (@$words) {
93 32 ▶ next unless length( $word // '' );
Branch: 1 branch
BranchTrueFalseTotal
unless length($word // "") 0 32 32
Condition50%1/2
$word // ""
l!l
320
MC/DC0%0/1
$word // ""
$word
Truth table50%1/2
A
A= $word
Aresult
11
00
94 32 my @chars = map { ord($_) - $offset } split //, $word;
95 32 _learn( \@known, \@chars );
96 32 _add( $base, \@chars, $word, $translator );
97 }
98 8 return $base, \@known;
99 }
100  
101 sub _learn {
102 32 ▶ my ( $known, $new ) = @_;
Subroutine _learn: called CC=3 SCAR=11.0
103 32 for my $i (@$new) {
104 47 ▶ $known->[$i] ||= 1;
Condition100%2/2
$known->[$i] ||= 1
l!l
2324
MC/DC100%1/1
$known->[$i] ||= 1
$known->[$i]
Truth table100%2/2
A
A= $known->[$i]
Aresult
11
00
105 }
106 }
107  
108 sub _add {
109 79 ▶ my ( $base, $chars, $word, $translator ) = @_;
Subroutine _add: called CC=3 SCAR=11.0
110 79 my $i = shift @$chars;
111 79 ▶ if ($i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i) { } 47 32 79
112 47 ▶ my $next = $base->[$i] //= [];
Condition100%2/2
$base->[$i] //= []
l!l
1235
MC/DC100%1/1
$base->[$i] //= []
$base->[$i]
Truth table100%2/2
A
A= $base->[$i]
Aresult
11
00
113 47 _add( $next, $chars, $word, $translator );
114 }
115 else { # store values in trie at the zero index
116 32 $base->[0] = $translator->{$word};
117 }
118 }
119  
120 # walk the trie looking for words you can make out of the current character count
121 sub _words_in {
122 71 ▶ my ( $counts, $total ) = @_;
Subroutine _words_in: called CC=8 SCAR=20.8
123 71 my @words;
124 71 my @stack = ( [ 0, $trie ] );
125 71 while (1) {
126 698 my ( $c, $level ) = @{ $stack[-1] };
127 698 ▶ if ( $c == -1 || $c >= @$level ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c == -1 or $c >= @$level) { } 160 538 698
Condition100%3/3
$c == -1 or $c >= @$level
l!l&&r!l&&!r
11545538
MC/DC100%2/2
$c == -1 or $c >= @$level
$c == -1 $c >= @$level
Truth table100%3/3
A or B
A= $c == -1B= $c >= @$level
ABresult
1X1
011
000
128 160 ▶ last if @stack == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @stack == 1 71 89 160
129 89 pop @stack;
130 89 ++$total;
131 89 $c = \( $stack[-1][0] );
132 89 ++$counts->[$$c];
133 89 $$c = $jumps[$$c];
134 }
135 else {
136 538 my $l = $level->[$c];
137 538 ▶ if ($l) { # trie holds corresponding node
Branch: 1 branch
BranchTrueFalseTotal
if ($l) { } 388 150 538
138 388 ▶ if ($c) { # character
Branch: 1 branch
BranchTrueFalseTotal
if ($c) { } 245 143 388
139 245 ▶ if ( $counts->[$c] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($counts->[$c]) { } 149 96 245
140 149 push @stack, [ 0, $l ];
141 149 --$counts->[$c];
142 149 --$total;
143 }
144 else {
145 96 $stack[-1][0] = $jumps[$c];
146 }
147 }
148 else { # terminal
149 143 push @words, [ $l, [@$counts] ];
150 143 ▶ if ($total) {
Branch: 1 branch
BranchTrueFalseTotal
if ($total) { } 83 60 143
151 83 $stack[-1][0] = $jumps[$c];
152 }
153 else {
154 60 pop @stack;
155 60 ++$total;
156 60 $c = \( $stack[-1][0] );
157 60 ++$counts->[$$c];
158 60 $$c = $jumps[$$c];
159 }
160 }
161 }
162 else { # try the next possible character
163 150 $stack[-1][0] = $jumps[$c];
164 }
165 }
166 }
167 71 \@words;
168 }
169  
170  
171 sub anagrams {
172 8 ▶ my $self = shift;
Subroutine anagrams: called CC=18 SCAR=29.1
Pod documented
173 8 my $phrase = shift;
174 8 my %opts = _make_opts(@_);
175 8 local ( $limit, $cleaner ) = @$self{qw(limit clean)};
176 8 $cleaner->($phrase);
177 8 ▶ return () unless length $phrase;
Branch: 1 branch
BranchTrueFalseTotal
unless length $phrase 0 8 8
178 8 my ( $sort, $min );
179  
180 8 ▶ if ( exists $opts{sorted} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{'sorted'}) { } 3 5 8
181 3 $sort = $opts{sorted};
182 }
183 else {
184 5 $sort = $self->{sorted};
185 }
186 8 ▶ if ( exists $opts{min} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{'min'}) { } 2 6 8
187 2 $min = $opts{min};
188 }
189 else {
190 6 $min = $self->{min};
191 }
192 8 ▶ my $i = $opts{start_list} // 0;
Condition100%2/2
$opts{"start_list"} // 0
l!l
17
MC/DC100%1/1
$opts{"start_list"} // 0
$opts{"start_list"}
Truth table100%2/2
A
A= $opts{"start_list"}
Aresult
11
00
193 8 my @pairs = @{ $self->{tries} };
194 8 ▶ if ($i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i) 1 7 8
195 1 ▶ die "impossible index for start list: $i" unless defined $pairs[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pairs[$i] 0 1 1
196 1 ▶ $i = @pairs + $i if $i < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $i < 0 1 0 1
197 1 @pairs = @pairs[ $i .. $#pairs ];
198 }
199 8 my $offset = $self->{offset};
200 8 my $counts = _counts( $phrase, $offset );
201 8 my @translator = @{ $self->{translator} };
202 8 local @jumps = _jumps($counts);
203 8 local @indices = _indices($counts);
204 8 my @anagrams;
205 8 for my $pair (@pairs) {
206 8 local ( $trie, $known ) = @$pair;
207 8 ▶ next unless _all_known($counts);
Branch: 1 branch
BranchTrueFalseTotal
unless _all_known($counts) 0 8 8
208 8 local %cache = ();
209 8 @anagrams = _anagramize($counts);
210 8 ▶ next unless @anagrams;
Branch: 1 branch
BranchTrueFalseTotal
unless @anagrams 0 8 8
211 8 ▶ next if $min and @anagrams < $min;
Branch: 1 branch
BranchTrueFalseTotal
if $min and @anagrams < $min 0 8 8
Condition66%2/3
$min and @anagrams < $min
!ll&&!rl&&r
620
MC/DC0%0/2
$min and @anagrams < $min
$min @anagrams < $min
Truth table66%2/3
A and B
A= $minB= @anagrams < $min
ABresult
0X0
100
111
212 8 last;
213 }
214 8 @anagrams = map { [ @translator[@$_] ] } @anagrams;
215 8 ▶ if ($sort) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sort) 4 4 8
216 @anagrams = sort {
217 24 ▶ my $ordered = @$a <= @$b ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
@$a <= @$b ? : 9 15 24
218 24 ▶ my ( $d, $e ) = $ordered == 1 ? ( $a, $b ) : ( $b, $a );
Branch: 1 branch
BranchTrueFalseTotal
$ordered == 1 ? : 9 15 24
219 24 for ( 0 .. $#$d ) {
220 27 my $c = $d->[$_] cmp $e->[$_];
221 27 ▶ return $ordered * $c if $c;
Branch: 1 branch
BranchTrueFalseTotal
if $c 24 3 27
222 }
223 0 -$ordered;
224 4 } map { [ sort @$_ ] } @anagrams;
225 }
226 8 return @anagrams;
227 }
228  
229 sub _make_opts {
230 27 ▶ if ( @_ == 1 ) {
Subroutine _make_opts: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 1 26 27
231 1 my $r = shift;
232 1 ▶ die 'options expected to be key value pairs or a hash ref'
Branch: 1 branch
BranchTrueFalseTotal
unless "HASH" eq ref $r 0 1 1
233 unless 'HASH' eq ref $r;
234 1 return %$r;
235 }
236 else {
237 26 return @_;
238 }
239 }
240  
241  
242 2 ▶our $null = sub { };
Subroutine __ANON__: not called CC=1 SCAR=0.0
243  
244 sub iterator {
245 7 ▶ my $self = shift;
Subroutine iterator: called CC=7 SCAR=19.7
Pod documented
246 7 my $phrase = shift;
247 7 my %opts = _make_opts(@_);
248 7 ▶ $opts{sorted} //= $self->{sorted};
Condition66%2/3
$opts{"sorted"} //= $self->{"sorted"}
l!l&&r!l&&!r
160
MC/DC0%0/2
$opts{"sorted"} //= $self->{"sorted"}
$opts{"sorted"} $self->{"sorted"}
Truth table66%2/3
A //= B
A= $opts{"sorted"}B= $self->{"sorted"}
ABresult
1X1
011
000
249 7 $self->{clean}->($phrase);
250 7 ▶ my $i = $opts{start_list} // 0;
Condition100%2/2
$opts{"start_list"} // 0
l!l
16
MC/DC100%1/1
$opts{"start_list"} // 0
$opts{"start_list"}
Truth table100%2/2
A
A= $opts{"start_list"}
Aresult
11
00
251 7 my @pairs = @{ $self->{tries} };
252 7 ▶ if ($i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i) 1 6 7
253 1 ▶ die "impossible index for start list: $i" unless defined $pairs[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pairs[$i] 0 1 1
254 1 ▶ $i = @pairs + $i if $i < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $i < 0 1 0 1
255 1 @pairs = @pairs[ $i .. $#pairs ];
256 }
257 7 ▶ return $null unless length $phrase;
Branch: 1 branch
BranchTrueFalseTotal
unless length $phrase 0 7 7
258 return _super_iterator( \@pairs, $phrase, \%opts,
259 7 @$self{qw(translator offset)} );
260 }
261  
262 # iterator that converts word indices back to words
263 sub _super_iterator {
264 7 ▶ my ( $tries, $phrase, $opts, $translator, $offset ) = @_;
Subroutine _super_iterator: called CC=1 SCAR=0.0
265 7 my $counts = _counts( $phrase, $offset );
266 7 my @j = _jumps($counts);
267 7 my @ix = _indices($counts);
268 7 my $i = _iterator( $tries, $counts, $opts );
269 7 my %c;
270 return sub {
271 28 ▶ my $rv;
Subroutine __ANON__: called CC=4 SCAR=13.9
272 28 local @jumps = @j;
273 28 local @indices = @ix;
274 {
275 28 $rv = $i->();
276 54 ▶ return unless $rv;
Branch: 1 branch
BranchTrueFalseTotal
unless $rv 7 47 54
277 47 my $key = join ',', sort { $a <=> $b } @$rv;
278 47 ▶ redo if $c{$key}++;
Branch: 1 branch
BranchTrueFalseTotal
if $c{$key}++ 26 21 47
279 }
280 21 $rv = [ @$translator[@$rv] ];
281 21 ▶ if ( $opts->{sorted} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"sorted"}) 5 16 21
282 5 $rv = [ sort @$rv ];
283 }
284 21 $rv;
285 7 };
286 }
287  
288 # iterator that manages the trie list
289 sub _iterator {
290 49 ▶ my ( $tries, $counts, $opts ) = @_;
Subroutine _iterator: called CC=2 SCAR=6.9
291 49 my $total = 0;
292 49 $total += $_ for @$counts[@indices];
293 49 my @t = @$tries;
294 49 my $i;
295 my $s = sub {
296 154 ▶ my $rv;
Subroutine __ANON__: called CC=6 SCAR=17.9
297 {
298 154 ▶ unless ($i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($i) 96 103 199
299 96 ▶ if (@t) {
Branch: 1 branch
BranchTrueFalseTotal
if (@t) { } 49 47 96
300 49 my $pair = shift @t;
301 49 local ( $trie, $known ) = @$pair;
302 49 ▶ redo unless _all_known($counts);
Branch: 1 branch
BranchTrueFalseTotal
unless _all_known($counts) 0 49 49
303 49 my $words = _words_in( $counts, $total );
304 49 ▶ redo unless _worth_pursuing( $counts, $words );
Branch: 1 branch
BranchTrueFalseTotal
unless _worth_pursuing($counts, $words) 0 49 49
305 49 $i = _sub_iterator( $tries, $words, $opts );
306 }
307 else {
308 47 return $rv;
309 }
310 }
311 152 $rv = $i->();
312 152 ▶ unless ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rv) 45 107 152
313 45 undef $i;
314 45 redo;
315 }
316 }
317 107 $rv;
318 49 };
319 49 $s;
320 }
321  
322 # iterator that actually walks tries looking for anagrams
323 sub _sub_iterator {
324 49 ▶ my ( $tries, $words, $opts ) = @_;
Subroutine _sub_iterator: called CC=1 SCAR=0.0
325 49 my @pairs = @$words;
326 return sub {
327 {
328 152 ▶ return unless @pairs;
Subroutine __ANON__: called CC=7 SCAR=19.5
Branch: 1 branch
BranchTrueFalseTotal
unless @pairs 45 192 237
329 192 ▶ if ( $opts->{random} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"random"}) 112 80 192
330 112 my $i = int rand scalar @pairs;
331 112 ▶ if ($i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i) 47 65 112
332 47 my $p = $pairs[0];
333 47 $pairs[0] = $pairs[$i];
334 47 $pairs[$i] = $p;
335 }
336 }
337 192 my ( $w, $s ) = @{ $pairs[0] };
338 192 ▶ unless ( ref $s eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $s eq "CODE") 89 103 192
339 89 ▶ if ( _any($s) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_any($s)) { } 42 47 89
340 42 $s = _iterator( $tries, $s, $opts );
341 }
342 else {
343 47 my $next = [];
344 $s = sub {
345 92 ▶ my $rv = $next;
Subroutine __ANON__: called CC=1 SCAR=0.0
346 92 undef $next;
347 92 $rv;
348 47 };
349 }
350 89 $pairs[0][1] = $s;
351 }
352 192 my $remainder = $s->();
353 192 ▶ unless ($remainder) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($remainder) 85 107 192
354 85 shift @pairs;
355 85 redo;
356 }
357 107 return [ $w, @$remainder ];
358 }
359 49 };
360 }
361  
362 # all character counts decremented
363 sub _worth_pursuing {
364 49 ▶ my ( $counts, $words ) = @_;
Subroutine _worth_pursuing: called CC=5 SCAR=16.1
365  
366 49 my $c;
367  
368 # if any letter count didn't change, there's no hope
369 49 OUTER: for my $i (@indices) {
370 137 ▶ next unless $c = $counts->[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless $c = $counts->[$i] 69 68 137
371 68 for (@$words) {
372 89 ▶ next OUTER if $_->[1][$i] < $c;
Branch: 1 branch
BranchTrueFalseTotal
if $_->[1][$i] < $c 68 21 89
373 }
374 0 return;
375 }
376 49 return 1;
377 }
378  
379 sub _indices {
380 15 ▶ my $counts = shift;
Subroutine _indices: called CC=3 SCAR=11.0
381 15 my @indices;
382 15 for my $i ( 0 .. $#$counts ) {
383 85 ▶ push @indices, $i if $counts->[$i];
Branch: 1 branch
BranchTrueFalseTotal
if $counts->[$i] 36 49 85
384 }
385 15 return @indices;
386 }
387  
388 sub _jumps {
389 15 ▶ my $counts = shift;
Subroutine _jumps: called CC=2 SCAR=6.9
390 15 my @jumps = (0) x @$counts;
391 15 my $j = 0;
392 15 ▶ while ( my $n = _next_jump( $counts, $j ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $n = _next_jump($counts, $j)) 36 15 51
393 36 $jumps[$j] = $n;
394 36 $j = $n;
395 }
396 15 $jumps[-1] = -1;
397 15 return @jumps;
398 }
399  
400 sub _next_jump {
401 51 ▶ my ( $counts, $j ) = @_;
Subroutine _next_jump: called CC=3 SCAR=11.0
402 51 for my $i ( $j + 1 .. $#$counts ) {
403 70 ▶ return $i if $counts->[$i];
Branch: 1 branch
BranchTrueFalseTotal
if $counts->[$i] 36 34 70
404 }
405 15 return;
406 }
407  
408 sub _clean {
409 61 ▶ $_[0] =~ s/\W+//g;
Subroutine _clean: called CC=1 SCAR=0.0
410 61 $_[0] = lc $_[0];
411 }
412  
413 sub _all_known {
414 57 ▶ my $counts = shift;
Subroutine _all_known: called CC=5 SCAR=16.8
415 57 ▶ return if @$counts > @$known;
Branch: 1 branch
BranchTrueFalseTotal
if @$counts > @$Lingua::Anagrams::known 0 57 57
416 57 for my $i ( 0 .. $#$counts ) {
417 265 ▶ return if $counts->[$i] && !$known->[$i];
Branch: 1 branch
BranchTrueFalseTotal
if $counts->[$i] and not $Lingua::Anagrams::known->[$i] 0 265 265
Condition66%2/3
$counts->[$i] and not $Lingua::Anagrams::known->[$i]
!ll&&!rl&&r
178870
MC/DC0%0/2
$counts->[$i] and not $Lingua::Anagrams::known->[$i]
$counts->[$i] not $Lingua::Anagrams::known->[$i]
Truth table66%2/3
A and not(B)
A= $counts->[$i]B= not $Lingua::Anagrams::known->[$i]
ABresult
0X0
100
111
418 }
419 57 return 1;
420 }
421  
422  
423 sub key {
424 6 ▶ my ( $self, $phrase ) = @_;
Subroutine key: called CC=6 SCAR=17.9
Pod documented
425 6 $self->{clean}->($phrase);
426 6 my $offset = $self->{offset};
427 6 my ( @counts, $lowest );
428 6 for my $c ( map { ord($_) - $offset } split //, $phrase ) {
429 18 ▶ if ( defined $lowest ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lowest) { } 12 6 18
430 12 ▶ $lowest = $c if $c < $lowest;
Branch: 1 branch
BranchTrueFalseTotal
if $c < $lowest 6 6 12
431 }
432 else {
433 6 $lowest = $c;
434 }
435 18 $counts[$c]++;
436 }
437 6 @counts = @counts[ $lowest .. $#counts ];
438 6 ▶ $_ //= '' for @counts;
Condition100%2/2
$_ //= ""
l!l
1831
MC/DC100%1/1
$_ //= ""
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
439 6 my $suffix = join '.', @counts;
440 6 $suffix =~ s/\.(\.+)\./'('.length($1).')'/ge;
441 6 return "$lowest:$suffix";
442 }
443  
444  
445 sub lists {
446 1 ▶ my $self = shift;
Subroutine lists: called CC=1 SCAR=0.0
Pod documented
447 1 return scalar @{ $self->{tries} };
448 }
449  
450 sub _counts {
451 15 ▶ my ( $phrase, $offset ) = @_;
Subroutine _counts: called CC=4 SCAR=13.9
452 15 my @counts;
453 15 for my $c ( map { ord($_) - $offset } split //, $phrase ) {
454 42 $counts[$c]++;
455 }
456 15 ▶ $_ //= 0 for @counts;
Condition100%2/2
$_ //= 0
l!l
3649
MC/DC100%1/1
$_ //= 0
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
457 15 \@counts;
458 }
459  
460 sub _any {
461 125 ▶ for ( @{ $_[0] } ) {
Subroutine _any: called CC=3 SCAR=11.0
462 503 ▶ return 1 if $_;
Branch: 1 branch
BranchTrueFalseTotal
if $_ 59 444 503
463 }
464 66 '';
465 }
466  
467 sub _anagramize {
468 25 ▶ my $counts = shift;
Subroutine _anagramize: called CC=10 SCAR=23.2
469 25 my $total = 0;
470 25 $total += $_ for @$counts[@indices];
471 25 my $key;
472 25 ▶ if ( $total <= $limit ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($total <= $Lingua::Anagrams::limit) 25 0 25
473 25 $key = join ',', @$counts[@indices];
474 25 my $cached = $cache{$key};
475 25 ▶ return @$cached if $cached;
Branch: 1 branch
BranchTrueFalseTotal
if $cached 3 22 25
476 }
477 22 my @anagrams;
478 22 my $words = _words_in( $counts, $total );
479 22 ▶ if ( _all_touched( $counts, $words ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_all_touched($counts, $words)) 22 0 22
480 22 for (@$words) {
481 36 my ( $word, $c ) = @$_;
482 36 ▶ if ( _any($c) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_any($c)) { } 17 19 36
483 17 push @anagrams, [ $word, @$_ ] for _anagramize($c);
484 }
485 else {
486 19 push @anagrams, [$word];
487 }
488 }
489 22 my %seen;
490 @anagrams = map {
491 22 ▶ $seen{ join ' ', sort { $a <=> $b } @$_ }++
Branch: 1 branch
BranchTrueFalseTotal
$seen{join ' ', (sort {$a <=> $b;} @$_)}++ ? : 0 39 39
492 ? ()
493 : $_
494 } @anagrams;
495 }
496 22 ▶ $cache{$key} = \@anagrams if $key;
Branch: 1 branch
BranchTrueFalseTotal
if $key 22 0 22
497 22 @anagrams;
498 }
499  
500 sub _all_touched {
501 22 ▶ my ( $counts, $words ) = @_;
Subroutine _all_touched: called CC=14 SCAR=26.5
502  
503 22 my $c;
504  
505 22 my ( @tallies, @good_indices );
506 22 for (@$words) {
507 52 my $wc = $_->[1];
508 52 for (@indices) {
509 140 ▶ next unless $c = $counts->[$_];
Branch: 1 branch
BranchTrueFalseTotal
unless $c = $counts->[$_] 26 114 140
510 114 ▶ $good_indices[$_] //= $_;
Condition66%2/3
$good_indices[$_] //= $_
l!l&&r!l&&!r
78360
MC/DC0%0/2
$good_indices[$_] //= $_
$good_indices[$_] $_
Truth table66%2/3
A //= B
A= $good_indices[$_]B= $_
ABresult
1X1
011
000
511 114 ▶ $tallies[$_]++ if $wc->[$_] < $c;
Branch: 1 branch
BranchTrueFalseTotal
if $wc->[$_] < $c 75 39 114
512 }
513 }
514  
515 # if any letter count didn't change, there's no hope
516 22 ▶ return unless @good_indices;
Branch: 1 branch
BranchTrueFalseTotal
unless @good_indices 0 22 22
517 22 for (@good_indices) {
518 109 ▶ next unless $_;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ 73 36 109
519 36 ▶ return unless $tallies[$_];
Branch: 1 branch
BranchTrueFalseTotal
unless $tallies[$_] 0 36 36
520 }
521  
522 # find the letter with the fewest possibilities
523 22 my ( $best, $min, $n );
524 22 for (@good_indices) {
525 109 ▶ next unless $_;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ 73 36 109
526 36 $n = $tallies[$_];
527 36 ▶ if ( !$best || $n < $min ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $best or $n < $min) 22 14 36
Condition66%2/3
not $best or $n < $min
l!l&&r!l&&!r
22014
MC/DC50%1/2
not $best or $n < $min
not $best $n < $min
Truth table66%2/3
not(A) or B
A= not $bestB= $n < $min
ABresult
1X1
011
000
528 22 $best = $_;
529 22 $min = $n;
530 }
531 }
532  
533 # we only need consider all the branches which affected a
534 # particular letter; we will find all possibilities in their
535 # ramifications
536 22 $c = $counts->[$best];
537 22 @$words = grep { $_->[1][$best] < $c } @$words;
538 22 return 1;
539 }
540  
541 2 1;
542  
543 __END__
↑ Summary cleaner.pl »