DDG/Block/Words.pm

filter: Statementstmt 96.5% 139 / 144 Branchbran 94.8% 55 / 58 Conditioncond 73.3% 11 / 15 MC/DCmcdc 60.0% 6 / 10 Subroutinesub 84.6% 11 / 13 Podpod 75.0% 3 / 4 totaltotal 92.2% 225 / 244 CC 52 SCAR 31.2
CC
52
Cov
94%
request
35.2
BUILD
13.0
_set_word_plugin
6.0
CRAP 22.7
« DDG/Rewrite.pm ↑ Summary DDG/App/Attribution.pm »
1 package DDG::Block::Words;
2 9 our $AUTHORITY = 'cpan:DDG';
3 # ABSTRACT: Block implementation to handle words based plugins
4 9 $DDG::Block::Words::VERSION = '1017';
5 9 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 with qw( DDG::Block );
8  
9 sub BUILD {
10 13 ▶ my ( $self ) = @_;
Subroutine BUILD: called CC=13 SCAR=25.7
Pod undocumented
11 13 for (@{$self->plugin_objs}) {
12 28 my $triggers = $_->[0];
13 28 my $plugin = $_->[1];
14 28 for (@{$triggers}) {
15 28 my $trigger = $_;
16 28 ▶ croak "trigger must be a hash on ".(ref $plugin) unless ref $trigger eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $trigger eq "HASH" 0 28 28
17 28 ▶ if (defined $trigger->{start}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $trigger->{"start"}) 7 21 28
18 7 $self->_set_start_word_plugin($_,$plugin) for (@{$trigger->{start}});
19 }
20 28 ▶ if (defined $trigger->{end}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $trigger->{"end"}) 5 23 28
21 5 $self->_set_end_word_plugin($_,$plugin) for (@{$trigger->{end}});
22 }
23 28 ▶ if (defined $trigger->{startend}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $trigger->{"startend"}) 3 25 28
24 3 $self->_set_start_word_plugin($_,$plugin) for (@{$trigger->{startend}});
25 3 $self->_set_end_word_plugin($_,$plugin) for (@{$trigger->{startend}});
26 }
27 28 ▶ if (defined $trigger->{any}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $trigger->{"any"}) 23 5 28
28 23 $self->_set_any_word_plugin($_,$plugin) for (@{$trigger->{any}});
29 }
30 }
31 }
32 }
33  
34  
35 11 ▶sub _set_start_word_plugin { shift->_set_beginword_word_plugin('start',@_) }
Subroutine _set_start_word_plugin: called CC=1 SCAR=0.0
36 30 ▶sub _set_any_word_plugin { shift->_set_beginword_word_plugin('any',@_) }
Subroutine _set_any_word_plugin: called CC=1 SCAR=0.0
37 9 ▶sub _set_end_word_plugin { shift->_set_endword_word_plugin('end',@_) }
Subroutine _set_end_word_plugin: called CC=1 SCAR=0.0
38  
39 # Grab trigger word (or FIRST word from trigger phrase)
40 # to use as hash key for `start` and `any` trigger hashes
41 sub _set_endword_word_plugin {
42 9 ▶ my ( $self, $type, $word, $plugin ) = @_;
Subroutine _set_endword_word_plugin: called CC=1 SCAR=0.0
43 9 my @words = split(/\s+/,$word);
44 9 $word = join(' ',@words);
45 9 $self->_set_word_plugin($type,pop @words,$word,$plugin);
46 }
47  
48 # Grab trigger word (or LAST word from trigger phrase)
49 # to use as hash key for `end` trigger hash
50 sub _set_beginword_word_plugin {
51 41 ▶ my ( $self, $type, $word, $plugin ) = @_;
Subroutine _set_beginword_word_plugin: called CC=1 SCAR=0.0
52 41 my @words = split(/\s+/,$word);
53 41 $word = join(' ',@words);
54 41 $self->_set_word_plugin($type,shift @words,$word,$plugin);
55 }
56  
57 sub _set_word_plugin {
58 50 ▶ my ( $self, $type, $key, $word, $plugin ) = @_;
Subroutine _set_word_plugin: called CC=6 SCAR=17.9
59 50 my @split_word = split(/\s+/,$word);
60 50 my $word_count = scalar @split_word;
61 50 ▶ $self->_words_plugins->{$type}->{$key} = {} unless defined $self->_words_plugins->{$type}->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->_words_plugins->{$type}{$key} 46 4 50
62 50 ▶ if ($word_count eq 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($word_count eq 1) { } 32 18 50
63 32 ▶ $self->_words_plugins->{$type}->{$key}->{1} = [] unless defined $self->_words_plugins->{$type}->{$key}->{$word_count};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->_words_plugins->{$type}{$key}{$word_count} 30 2 32
64 32 push @{$self->_words_plugins->{$type}->{$key}->{1}}, $plugin;
65 } else {
66 18 ▶ $self->_words_plugins->{$type}->{$key}->{$word_count} = {} unless defined $self->_words_plugins->{$type}->{$key}->{$word_count};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->_words_plugins->{$type}{$key}{$word_count} 16 2 18
67 18 ▶ $self->_words_plugins->{$type}->{$key}->{$word_count}->{$word} = [] unless defined $self->_words_plugins->{$type}->{$key}->{$word_count}->{$word};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->_words_plugins->{$type}{$key}{$word_count}{$word} 18 0 18
68 18 push @{$self->_words_plugins->{$type}->{$key}->{$word_count}->{$word}}, $plugin;
69 }
70 }
71  
72  
73 9 has _words_plugins => (
74 # like HashRef[HashRef[DDG::Block::Plugin]]',
75 is => 'ro',
76 lazy => 1,
77 builder => 1,
78 );
79 0 ▶sub words_plugins { shift->_words_plugins }
Subroutine words_plugins: not called CC=1 SCAR=6.9
Pod documented
80  
81 sub _build__words_plugins {{
82 10 ▶ start => {},
Subroutine _build__words_plugins: called CC=1 SCAR=0.0
83 end => {},
84 any => {},
85 }}
86  
87  
88 sub request {
89 43 ▶ my ( $self, $request ) = @_;
Subroutine request: called CC=35 SCAR=35.6
Pod documented
90 43 my @results;
91 #
92 # Mapping positions of keywords in the request
93 # to a flat array which access stepwise.
94 #
95 # @poses is an array of the positions inside
96 # the triggers hash.
97 #
98 ################################################
99 43 my %triggers = %{$request->triggers};
100 43 my $max = scalar keys %triggers;
101 43 my @poses = sort { $a <=> $b } keys %triggers;
102 43 $self->trace( "Trigger word positions: ", @poses );
103 43 for my $cnt (0..$max-1) {
104 #
105 # Split into a flat array so we can easily
106 # determine if the query is starting, ending
107 # or still in the beginning, this is very essential
108 # for the following steps.
109 #
110 104 ▶ my $start = $cnt == 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$cnt == 0 ? : 43 61 104
111 104 ▶ my $end = $cnt == $max-1 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$cnt == $max - 1 ? : 25 79 104
112  
113 # Iterate over each word in the query, checking if any of
114 # the IA's have this specific word as (part of) a `start`,
115 # `end` or `any` trigger.
116 104 for my $word (@{$request->triggers->{$poses[$cnt]}}) {
117 132 $self->trace( "Testing word:", "'".$word."'" );
118  
119 132 my @hits = ();
120 {
121  
122 # Flag that tells us if there could be more words
123 # after the matched word.
124 #
125 # Used for `any` and `start` triggers. Not for `end`
126 # triggers because they look in-front of the word.
127 132 my $begin = 0;
128  
129 # A hash containing all triggers related to the current word
130 #
131 # The keys inside the hitstruct define the word count for the
132 # trigger word/phrase. This is used to determine if the query
133 # is long enough to have a match.
134 # ie. a 2 word query can't match a 3 word trigger phrase
135 132 my $hitstruct = undef;
136  
137 132 ▶ my $is_start = $start && defined $self->_words_plugins->{start}->{$word};
Condition100%3/3
$start && defined $self->_words_plugins->{'start'}{$word}
!ll&&!rl&&r
734712
MC/DC100%2/2
$start && defined $self->_words_plugins->{'start'}{$word}
$start defined $self->_words_plugins->{'start'}{$word}
Truth table100%3/3
A && B
A= $startB= defined $self->_words_plugins->{'start'}{$word}
ABresult
0X0
100
111
138 132 ▶ my $is_end = $end && defined $self->_words_plugins->{end}->{$word};
Condition100%3/3
$end && defined $self->_words_plugins->{'end'}{$word}
!ll&&!rl&&r
97296
MC/DC100%2/2
$end && defined $self->_words_plugins->{'end'}{$word}
$end defined $self->_words_plugins->{'end'}{$word}
Truth table100%3/3
A && B
A= $endB= defined $self->_words_plugins->{'end'}{$word}
ABresult
0X0
100
111
139 132 my $is_any = defined $self->_words_plugins->{any}->{$word};
140  
141 132 ▶ if ($is_start) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_start) 12 120 132
142 12 $begin = 1;
143 12 $hitstruct = $self->_words_plugins->{start}->{$word};
144 12 push(@hits,[$begin,$hitstruct]);
145 }
146  
147 132 ▶ if ($is_end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_end) 6 126 132
148 6 $begin = 0;
149 6 $hitstruct = $self->_words_plugins->{end}->{$word};
150 6 push(@hits,[$begin,$hitstruct]);
151 }
152  
153 132 ▶ if ($is_any) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_any) 28 104 132
154 28 $begin = 1;
155 28 $hitstruct = $self->_words_plugins->{any}->{$word};
156 28 push(@hits,[$begin,$hitstruct]);
157 }
158 }
159  
160 132 ▶ unless ( @hits ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@hits) 90 42 132
161 90 $self->trace("No hit with","'".$word."'");
162 }
163  
164 # iterate over each type of trigger match for the current word
165 # this allows us to consider combinations of `start`, `end` and `any`
166 # triggers for the current word
167 132 ▶ while (my $hitref = shift @hits) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $hitref = shift @hits) 44 110 154
168  
169 44 my ($begin,$hitstruct) = @{$hitref};
170 ######################################################
171 44 ▶ $self->trace("Got a hit with","'".$word."'","!", $begin ? "And it's just the beginning..." : "");
Branch: 1 branch
BranchTrueFalseTotal
$begin ? : 38 6 44
172  
173 # $cnt is the specific position inside our flat array of
174 # positions inside the query.
175 44 my $pos = $poses[$cnt];
176  
177 # This `for` loop is only executed if we have a partial
178 # match on a trigger phrase i.e. the first word in a `start`
179 # or `any` trigger phrase, or the last word in an `end`
180 # trigger phrase. In this case it iterates through all
181 # those different combination and tries to match it
182 # with the request of the query.
183 #
184 44 for my $word_count (sort { $b <=> $a } grep { $_ > 1 } keys %{$hitstruct}) {
185 ############################################################
186 23 $self->trace( "Checking additional multiword triggers with length of", $word_count);
187 23 my @sofar_words = @{$triggers{$pos}};
188 23 for my $sofar_word (@sofar_words) {
189 44 ▶ if (defined $hitstruct->{$word_count}->{$sofar_word}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hitstruct->{$word_count}{$sofar_word}) 2 42 44
190 2 for (@{$hitstruct->{$word_count}->{$sofar_word}}) {
191 2 push @results, $self->handle_request_matches($_,$request,$pos);
192 2 ▶ if ($self->return_one && @results) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->return_one and @results) 0 2 2
Condition33%1/3
$self->return_one and @results
!ll&&!rl&&r
200
MC/DC0%0/2
$self->return_one and @results
$self->return_one @results
Truth table33%1/3
A and B
A= $self->return_oneB= @results
ABresult
0X0
100
111
193 0 $self->trace("Got return_one and ".(scalar @results)." results, finishing here");
194 0 return @results;
195 }
196 }
197 }
198 }
199 # Here we take the index of the partially matched trigger phrase and
200 # calculate where the trigger should start or end (based on whether
201 # it's a `start` or `end` trigger) to verify if the partial match is a
202 # full match against the whole trigger
203 #
204 # Then we check if the next/previous word in the string matches
205 # the next/previous word in the trigger phrase (again, based on whether it's a start or end trigger)
206 23 ▶ my @next_poses_key = grep { $_ >= 0 } $begin ? ($cnt+1)..($cnt+$word_count-1) : ($cnt-$word_count+1)..($cnt-1);
Branch: 1 branch
BranchTrueFalseTotal
$begin ? : 19 4 23
207 23 ▶ my @next_poses = grep { defined $_ && defined $triggers{$_} } @poses[@next_poses_key];
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and defined $triggers{$_} 29 1 30
208 23 ▶ @next_poses = reverse @next_poses unless $begin;
Branch: 1 branch
BranchTrueFalseTotal
unless $begin 4 19 23
209 23 for my $next_pos (@next_poses) {
210 29 my @next_triggers = @{$triggers{$next_pos}};
211 29 my @new_next_words;
212 29 for my $next_trigger (@next_triggers) {
213 30 for my $current_sofar_word (@sofar_words) {
214 98 ▶ my $new_next_word = $begin
Branch: 1 branch
BranchTrueFalseTotal
$begin ? : 87 11 98
215 ? join(" ",$current_sofar_word,$next_trigger)
216 : join(" ",$next_trigger,$current_sofar_word);
217 98 ▶ if (defined $hitstruct->{$word_count}->{$new_next_word}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hitstruct->{$word_count}{$new_next_word}) 18 80 98
218 18 for (@{$hitstruct->{$word_count}->{$new_next_word}}) {
219 18 ▶ push @results, $self->handle_request_matches($_,$request,( $pos < $next_pos ) ? ( $pos,$next_pos ) : ( $next_pos,$pos ));
Branch: 1 branch
BranchTrueFalseTotal
$pos < $next_pos ? : 16 2 18
220 18 ▶ if ($self->return_one && @results) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->return_one and @results) 13 5 18
Condition66%2/3
$self->return_one and @results
!ll&&!rl&&r
5013
MC/DC50%1/2
$self->return_one and @results
$self->return_one @results
Truth table66%2/3
A and B
A= $self->return_oneB= @results
ABresult
0X0
100
111
221 13 $self->trace("Got return_one and ".(scalar @results)." results, finishing here");
222 13 return @results;
223 }
224 }
225 }
226 85 push @new_next_words, $new_next_word;
227 }
228 }
229 16 push @sofar_words, @new_next_words;
230 }
231 }
232  
233 # Check if we have match on a single trigger word
234 31 ▶ if (defined $hitstruct->{1}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hitstruct->{1}) 21 10 31
235 21 for (@{$hitstruct->{1}}) {
236 22 push @results, $self->handle_request_matches($_,$request,$poses[$cnt]);
237 22 ▶ if ($self->return_one && @results) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->return_one and @results) 9 13 22
Condition66%2/3
$self->return_one and @results
!ll&&!rl&&r
1309
MC/DC50%1/2
$self->return_one and @results
$self->return_one @results
Truth table66%2/3
A and B
A= $self->return_oneB= @results
ABresult
0X0
100
111
238 9 $self->trace("Got return_one and ".(scalar @results)." results, finishing here");
239 9 return @results;
240 }
241 }
242 }
243 }
244 }
245 }
246 21 return @results;
247 }
248  
249  
250 0 ▶sub empty_trigger { croak "empty triggers are not supported by ".__PACKAGE__ }
Subroutine empty_trigger: not called CC=1 SCAR=6.9
Pod documented
251  
252 9 1;
253  
254 __END__
« DDG/Rewrite.pm ↑ Summary DDG/App/Attribution.pm »