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