blib/lib/Search/Tools/HeatMap.pm

filter: Statementstmt 88.3% 205 / 232 Branchbran 63.8% 69 / 108 Conditioncond 75.4% 40 / 53 MC/DCmcdc 41.9% 13 / 31 Subroutinesub 100% 10 / 10 Podpod 100% 2 / 2 totaltotal 77.7% 339 / 436 CC 85 SCAR 38.9
CC
85
Cov
80%
_as_sentences
67.3
_no_sentences
47.3
_build
5.1
CRAP 49.0
« blib/lib/Search/Tools/Snipper.pm ↑ Summary blib/lib/Search/Tools/QueryParser.pm »
1 package Search::Tools::HeatMap;
2 17 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 17 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use Data::Dump qw( dump );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 extends 'Search::Tools::Object';
6  
7 17 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 17 our $VERSION = '1.007';
10  
11 # debugging only
12 17 my $OPEN = '[';
13 17 my $CLOSE = ']';
14 17 eval { require Term::ANSIColor; };
15 17 ▶if ( !$@ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 17 0 17
16 17 $OPEN .= Term::ANSIColor::color('bold red');
17 17 $CLOSE = Term::ANSIColor::color('reset') . $CLOSE;
18 }
19  
20 17 my @attrs = qw( window_size
21 tokens
22 spans
23 as_sentences
24 _treat_phrases_as_singles
25 _qre
26 _query
27 _stemmer
28 );
29  
30 17 for my $attr (@attrs) {
31 136 has $attr => ( is => 'rw' );
32 }
33  
34 =head1 NAME
35  
36 Search::Tools::HeatMap - locate the best matches in a snippet extract
37  
38 =head1 SYNOPSIS
39  
40 use Search::Tools::Tokenizer;
41 use Search::Tools::HeatMap;
42
43 my $tokens = $self->tokenizer->tokenize( $my_string, qr/^(interesting)$/ );
44 my $heatmap = Search::Tools::HeatMap->new(
45 tokens => $tokens,
46 window_size => 20, # default
47 as_sentences => 0, # default
48 );
49  
50 if ( $heatmap->has_spans ) {
51
52 my $tokens_arr = $tokens->as_array;
53  
54 # stringify positions
55 my @snips;
56 for my $span ( @{ $heatmap->spans } ) {
57 push( @snips, $span->{str} );
58 }
59 my $occur_index = $self->occur - 1;
60 if ( $#snips > $occur_index ) {
61 @snips = @snips[ 0 .. $occur_index ];
62 }
63 printf("%s\n", join( ' ... ', @snips ));
64
65 }
66  
67 =head1 DESCRIPTION
68  
69 Search::Tools::HeatMap implements a simple algorithm for locating
70 the densest clusters of unique, hot terms in a TokenList.
71  
72 HeatMap is used internally by Snipper but documented here in case
73 someone wants to abuse and/or improve it.
74  
75 =head1 METHODS
76  
77 =head2 new( tokens => I<TokenList> )
78  
79 Create a new HeatMap. The I<TokenList> object may be either a
80 Search::Tools::TokenList or Search::Tools::TokenListPP object.
81  
82 =head2 BUILD
83  
84 Builds the HeatMap object. Called internally by new().
85  
86 =cut
87  
88 30 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod documented
89 30 my $self = shift;
90 30 $self->_build;
91 30 return $self;
92 }
93  
94 =head2 window_size
95  
96 The max width of a span. Defaults to 20 tokens, including the
97 matches.
98  
99 Set this in new(). Access it later if you need to, but the spans
100 will have already been created by new().
101  
102 =head2 as_sentences
103  
104 Try to match clusters at sentence boundaries. Default is false.
105  
106 Set this in new().
107  
108 =head2 spans
109  
110 Returns an array ref of matching clusters. Each span in the array
111 is a hash ref with the following keys:
112  
113 =over
114  
115 =item cluster
116  
117 =item pos
118  
119 =item heat
120  
121 =item str
122  
123 =item str_w_pos
124  
125 This item is available only if debug() is true.
126  
127 =item unique
128  
129 =back
130  
131 =cut
132  
133 # TODO this is mostly integer math and might be much
134 # faster if rewritten in XS once the algorithm is "final".
135 30 ▶sub _build {
Subroutine _build: called CC=5 SCAR=16.3
136 30 my $self = shift;
137 30 ▶ my $tokens = $self->tokens or croak "tokens required";
Branch: 1 branch
BranchTrueFalseTotal
my $tokens = $self->tokens or croak("tokens required") 30 0 30
138 30 ▶ my $window = $self->window_size || 20;
Condition50%1/2
$self->window_size || 20
l!l
300
MC/DC0%0/1
$self->window_size || 20
$self->window_size
Truth table50%1/2
A
A= $self->window_size
Aresult
11
00
139 30 ▶ my $as_sentences = $self->as_sentences || 0;
Condition100%2/2
$self->as_sentences || 0
l!l
1317
MC/DC100%1/1
$self->as_sentences || 0
$self->as_sentences
Truth table100%2/2
A
A= $self->as_sentences
Aresult
11
00
140 30 ▶ return $as_sentences
Branch: 1 branch
BranchTrueFalseTotal
$as_sentences ? : 13 17 30
141 ? $self->_as_sentences( $tokens, $window )
142 : $self->_no_sentences( $tokens, $window );
143 }
144  
145 # currently _as_sentences() is mostly identical to _no_sentences()
146 # with slightly fewer gymnastics.
147 # Since we already know via sentence_starts where our boundaries are,
148 # we do not have to call $tokens->get_window().
149 # Who knows how we might improve the sentence algorithm in future,
150 # so already having it in its own method seems like a win.
151 13 ▶sub _as_sentences {
Subroutine _as_sentences: called CC=41 SCAR=42.1
152 13 my ( $self, $tokens, $window ) = @_;
153 13 ▶ my $debug = $self->debug || 0;
Condition50%1/2
$self->debug || 0
l!l
013
MC/DC0%0/1
$self->debug || 0
$self->debug
Truth table50%1/2
A
A= $self->debug
Aresult
11
00
154 13 my $sentence_length = $window * 2;
155  
156 # build heatmap with sentence starts
157 13 my $num_tokens = $tokens->len;
158 13 my $tokens_arr = $tokens->as_array;
159 13 my %heatmap = ();
160 13 my $token_list_heat = $tokens->get_heat;
161 13 my $heat_sentence_starts = $tokens->get_sentence_starts;
162  
163 # this regex is a sanity check for phrases. we replace the \ with a
164 # more promiscuous check because the single space is too naive
165 # for real text (e.g. st. john's)
166 13 my $qre = $self->{_qre};
167 13 my @phrases = @{ $self->{_query}->phrases };
168 13 my $n_terms = $self->{_query}->num_terms;
169 13 my $query_has_phrase = $qre =~ s/(\\ )+/.+/g;
170  
171 13 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 13 13
172 0 warn "heat_sentence_starts: " . dump($heat_sentence_starts);
173 0 warn "token_list_heat: " . dump($token_list_heat);
174 0 warn "n_terms: $n_terms";
175 0 warn "phrases: " . dump( \@phrases );
176 0 warn "query_has_phrase: $query_has_phrase";
177 }
178  
179 # find the "sentence" that each hot token appears in.
180 13 my @starts_ends;
181 13 my $i = 0;
182 13 my %heat_sentence_ends = (); # cache
183 13 for (@$token_list_heat) {
184 42 my $token = $tokens->get_token($_);
185 42 my $token_pos = $token->pos;
186 42 my $start = $heat_sentence_starts->[ $i++ ];
187 42 $heatmap{$token_pos} = $token->is_hot;
188  
189 # a little optimization for when we've got
190 # multiple hot tokens in the same sentence
191 42 ▶ if ( exists $heat_sentence_ends{$start} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $heat_sentence_ends{$start}) 22 20 42
192 22 ▶ $debug
Branch: 1 branch
BranchTrueFalseTotal
$debug and warn "found cached end $heat_sentence_ends{$start} " . "for start $start token $token_pos\n" 0 22 22
193 and warn "found cached end $heat_sentence_ends{$start} "
194 . "for start $start token $token_pos\n";
195  
196 push( @starts_ends,
197 22 [ $start, $token_pos, $heat_sentence_ends{$start} ] );
198 22 next;
199 }
200  
201 # find the outermost limit of where this sentence might end
202 20 my $max_end;
203  
204 # is there a "next" start?
205 20 ▶ if ( defined $heat_sentence_starts->[$i]
Branch: 1 branch
BranchTrueFalseTotal
if (defined $heat_sentence_starts->[$i] and $heat_sentence_starts->[$i] != $start) { } 3 17 20
Condition100%3/3
defined $heat_sentence_starts->[$i] and $heat_sentence_starts->[$i] != $start
!ll&&!rl&&r
4133
MC/DC100%2/2
defined $heat_sentence_starts->[$i] and $heat_sentence_starts->[$i] != $start
defined $heat_sentence_starts->[$i] $heat_sentence_starts->[$i] != $start
Truth table100%3/3
A and B
A= defined $heat_sentence_starts->[$i]B= $heat_sentence_starts->[$i] != $start
ABresult
0X0
100
111
206 and $heat_sentence_starts->[$i] != $start )
207 {
208  
209 # this token is unique in this non-final sentence
210 3 $max_end = $heat_sentence_starts->[$i] - 1;
211 }
212 else {
213  
214 # this is the final sentence
215 17 $max_end = $num_tokens - 1;
216 }
217 20 my $end = $start;
218  
219 # find the nearest sentence end to the start
220 20 ▶ while ( $end < $max_end ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end < $max_end) 1990 10 2000
221 1990 my $tok = $tokens->get_token( $end++ );
222 1990 ▶ if ( !$tok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tok) 0 1990 1990
223 0 ▶ $debug and warn "No token at end=$end";
Branch: 1 branch
BranchTrueFalseTotal
$debug and warn "No token at end=$end" 0 0 0
224 0 last;
225 }
226 1990 ▶ if ( $tok->is_sentence_end ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tok->is_sentence_end) 10 1980 1990
227 10 $end--; # move back one position
228 10 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 10 10
229 0 warn "tok $_ is_sentence_end end=$end";
230 0 $tok->dump;
231 }
232 10 last;
233 }
234 }
235  
236 # back up if we've exceeded the 0-based tokens array.
237 20 ▶ $end = $num_tokens if $end > $num_tokens;
Branch: 1 branch
BranchTrueFalseTotal
if $end > $num_tokens 0 20 20
238  
239 20 ▶ $debug
Branch: 1 branch
BranchTrueFalseTotal
$debug and warn "start=$start max_end=$max_end " . "sentence_length=$sentence_length end=$end " . "token_pos=$token_pos\n" 0 20 20
240 and warn "start=$start max_end=$max_end "
241 . "sentence_length=$sentence_length end=$end "
242 . "token_pos=$token_pos\n";
243  
244 # if we didn't yet set the actual hot token,
245 # include everything up to it.
246 20 ▶ if ( $end < $token_pos ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end < $token_pos) 0 20 20
247 0 ▶ $debug
Branch: 1 branch
BranchTrueFalseTotal
$debug and warn "resetting end=$token_pos\n" 0 0 0
248 and warn "resetting end=$token_pos\n";
249  
250 0 $end = $token_pos;
251 }
252 20 push( @starts_ends, [ $start, $token_pos, $end ] );
253  
254 # cache
255 20 $heat_sentence_ends{$start} = $end;
256 }
257  
258 13 ▶ $debug and warn "starts_ends: " . dump( \@starts_ends );
Branch: 1 branch
BranchTrueFalseTotal
$debug and warn 'starts_ends: ' . Search::Tools::HeatMap::dump(\@starts_ends) 0 13 13
259  
260 13 my @spans;
261 my %seen_pos;
262 START_END:
263 13 for my $start_end (@starts_ends) {
264  
265 # get full window, ignoring positions we've already seen.
266 42 my $heat = 0;
267 42 my %span;
268 my @cluster_tokens;
269  
270 42 my ( $start, $hot_pos, $end ) = @$start_end;
271 42 POS: for my $pos ( $start .. $end ) {
272 5075 ▶ next POS if $seen_pos{$pos}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen_pos{$pos}++ 3075 2000 5075
273 2000 ▶ $heat += ( exists $heatmap{$pos} ? $heatmap{$pos} : 0 );
Branch: 1 branch
BranchTrueFalseTotal
exists $heatmap{$pos} ? : 42 1958 2000
274 2000 push( @cluster_tokens, $tokens->get_token($pos) );
275 }
276  
277 # if we had already seen_pos all positions.
278 42 ▶ next START_END unless @cluster_tokens;
Branch: 1 branch
BranchTrueFalseTotal
unless @cluster_tokens 22 20 42
279  
280 # sanity: make sure we still have something hot
281 20 my $has_hot = 0;
282 20 my @cluster_pos;
283 my @strings;
284 20 TOK: for (@cluster_tokens) {
285 2000 my $pos = $_->pos;
286 2000 ▶ $has_hot++ if exists $heatmap{$pos};
Branch: 1 branch
BranchTrueFalseTotal
if exists $heatmap{$pos} 42 1958 2000
287 2000 push @strings, $_->str;
288 2000 push @cluster_pos, $pos;
289 }
290 20 ▶ next START_END unless $has_hot;
Branch: 1 branch
BranchTrueFalseTotal
unless $has_hot 0 20 20
291  
292 # the final string is a sentence end,
293 # but we only want the first char in it,
294 # and not any whitespace, stray punctuation or other
295 # non-word noise.
296 20 $strings[$#strings] =~ s/^([\.\?\!]).*/$1/;
297  
298 20 $span{start_end} = $start_end;
299 20 $span{heat} = $heat;
300 20 $span{pos} = \@cluster_pos;
301 20 $span{tokens} = \@cluster_tokens;
302 20 $span{str} = join( '', @strings );
303  
304 # spans with more *unique* hot tokens in a single span rank higher
305 # spans with more *proximate* hot tokens in a single span rank higher
306 20 my %uniq = ();
307 20 my $i = 0;
308 20 my $num_proximate = 1; # one for the single hot token
309 20 for (@cluster_pos) {
310 2000 ▶ if ( exists $heatmap{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $heatmap{$_}) 42 1958 2000
311 42 $uniq{ lc $strings[$i] } += $heatmap{$_};
312 42 ▶ if ( $i && exists $heatmap{ $cluster_pos[ $i - 2 ] } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i and exists $heatmap{$cluster_pos[$i - 2]}) 10 32 42
Condition100%3/3
$i and exists $heatmap{$cluster_pos[$i - 2]}
!ll&&!rl&&r
23010
MC/DC100%2/2
$i and exists $heatmap{$cluster_pos[$i - 2]}
$i exists $heatmap{$cluster_pos[$i - 2]}
Truth table100%3/3
A and B
A= $iB= exists $heatmap{$cluster_pos[$i - 2]}
ABresult
0X0
100
111
313 10 $num_proximate++;
314 }
315 }
316 2000 $i++;
317 }
318 20 $span{unique} = scalar keys %uniq;
319 20 $span{proximate} = $num_proximate;
320  
321 # no false phrase matches if !_treat_phrases_as_singles
322 # stemmer check because regex will likely fail
323 # when stemmer is on
324 20 ▶ if ( $query_has_phrase
Branch: 1 branch
BranchTrueFalseTotal
if ($query_has_phrase and not $self->{"_treat_phrases_as_singles"}) 7 13 20
Condition66%2/3
$query_has_phrase and not $self->{"_treat_phrases_as_singles"}
!ll&&!rl&&r
1307
MC/DC50%1/2
$query_has_phrase and not $self->{"_treat_phrases_as_singles"}
$query_has_phrase not $self->{"_treat_phrases_as_singles"}
Truth table66%2/3
A and not(B)
A= $query_has_phraseB= not $self->{"_treat_phrases_as_singles"}
ABresult
0X0
100
111
325 and !$self->{_treat_phrases_as_singles} )
326 {
327 7 ▶ if ( !$self->{_stemmer} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'_stemmer'}) { } 3 4 7
328  
329 #warn "_treat_phrases_as_singles NOT true";
330 3 ▶ if ( $span{str} !~ m/$qre/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($span{"str"} =~ /$qre/) 0 3 3
331 0 ▶ $debug
Branch: 1 branch
BranchTrueFalseTotal
$debug and warn qq[treat_phrases_as_singles=FALSE and '$span{"str"}' failed to match $qre\n] 0 0 0
332 and warn
333 "treat_phrases_as_singles=FALSE and '$span{str}' failed to match $qre\n";
334 0 next START_END;
335 }
336 }
337 else {
338  
339 # if stemmer was on, we cannot rely on the regex,
340 # but we assume that number of uniq terms must match query
341  
342 4 ▶ if ( $n_terms == $query_has_phrase
Branch: 1 branch
BranchTrueFalseTotal
if ($n_terms == $query_has_phrase and $n_terms > $span{"unique"}) 0 4 4
Condition66%2/3
$n_terms == $query_has_phrase and $n_terms > $span{"unique"}
!ll&&!rl&&r
220
MC/DC0%0/2
$n_terms == $query_has_phrase and $n_terms > $span{"unique"}
$n_terms == $query_has_phrase $n_terms > $span{"unique"}
Truth table66%2/3
A and B
A= $n_terms == $query_has_phraseB= $n_terms > $span{"unique"}
ABresult
0X0
100
111
343 && $n_terms > $span{unique} )
344 {
345  
346 0 ▶ $debug
Branch: 1 branch
BranchTrueFalseTotal
$debug and warn qq[treat_phrases_as_singles=FALSE and '$span{"str"}' ] . qq[expected $n_terms unique terms, got $span{"unique"}\n] 0 0 0
347 and warn
348 "treat_phrases_as_singles=FALSE and '$span{str}' "
349 . "expected $n_terms unique terms, got $span{unique}\n";
350 0 next START_END;
351 }
352  
353 }
354 }
355  
356 # just for debug
357 20 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 20 20
358 0 my $i = 0;
359 $span{str_w_pos} = join(
360 '',
361 map {
362 0 $strings[ $i++ ]
363 . ( exists $heatmap{$_} ? $OPEN : '[' )
364 . $_
365 0 ▶ . ( exists $heatmap{$_} ? $CLOSE : ']' )
Branch: 2 branches
BranchTrueFalseTotal
exists $heatmap{$_} ? : 0 0 0
exists $heatmap{$_} ? : 0 0 0
366 } @cluster_pos
367 );
368 }
369  
370 20 push @spans, \%span;
371  
372 }
373  
374 13 $self->{spans} = $self->_sort_spans( \@spans );
375 13 $self->{heatmap} = \%heatmap;
376  
377 13 return $self;
378 }
379  
380 30 ▶sub _sort_spans {
Subroutine _sort_spans: called CC=4 SCAR=14.0
381 return [
382  
383 # sort by unique,
384 # then by proximity
385 # then by heat
386 # then by pos
387  
388 sort {
389 $b->{unique} <=> $a->{unique}
390 || $b->{proximate} <=> $a->{proximate}
391 || $b->{heat} <=> $a->{heat}
392 51 ▶ || $a->{pos}->[0] <=> $b->{pos}->[0]
Condition77%7/9
$Search::Tools::HeatMap::b->{'unique'} <=> $Search::Tools::HeatMap::a->{'unique'} || $Search::Tools::HeatMap::b->{'proximate'} <=> $Search::Tools::HeatMap::a->{'proximate'} || $Search::Tools::HeatMap::b->{'heat'} <=> $Search::Tools::HeatMap::a->{'heat'} || $Search::Tools::HeatMap::a->{'pos'}[0] <=> $Search::Tools::HeatMap::b->{'pos'}[0]
l!l&&r!l&&!r
21300
$Search::Tools::HeatMap::b->{"unique"} <=> $Search::Tools::HeatMap::a->{"unique"} or $Search::Tools::HeatMap::b->{"proximate"} <=> $Search::Tools::HeatMap::a->{"proximate"} or $Search::Tools::HeatMap::b->{"heat"} <=> $Search::Tools::HeatMap::a->{"heat"}
l!l&&r!l&&!r
16530
$Search::Tools::HeatMap::b->{"unique"} <=> $Search::Tools::HeatMap::a->{"unique"} or $Search::Tools::HeatMap::b->{"proximate"} <=> $Search::Tools::HeatMap::a->{"proximate"}
l!l&&r!l&&!r
16035
MC/DC0%0/4
$Search::Tools::HeatMap::b->{'unique'} <=> $Search::Tools::HeatMap::a->{'unique'} || $Search::Tools::HeatMap::b->{'proximate'} <=> $Search::Tools::HeatMap::a->{'proximate'} || $Search::Tools::HeatMap::b->{'heat'} <=> $Search::Tools::HeatMap::a->{'heat'} || $Search::Tools::HeatMap::a->{'pos'}[0] <=> $Search::Tools::HeatMap::b->{'pos'}[0]
$Search::Tools::HeatMap::b->{"unique"} <=> $Search::Tools::HeatMap::a->{"unique"} $Search::Tools::HeatMap::b->{"proximate"} <=> $Search::Tools::HeatMap::a->{"proximate"} $Search::Tools::HeatMap::b->{"heat"} <=> $Search::Tools::HeatMap::a->{"heat"} $Search::Tools::HeatMap::a->{'pos'}[0] <=> $Search::Tools::HeatMap::b->{'pos'}[0]
Truth table60%3/5
A or B or C || D
A= $Search::Tools::HeatMap::b->{"unique"} <=> $Search::Tools::HeatMap::a->{"unique"}B= $Search::Tools::HeatMap::b->{"proximate"} <=> $Search::Tools::HeatMap::a->{"proximate"}C= $Search::Tools::HeatMap::b->{"heat"} <=> $Search::Tools::HeatMap::a->{"heat"}D= $Search::Tools::HeatMap::a->{'pos'}[0] <=> $Search::Tools::HeatMap::b->{'pos'}[0]
ABCDresult
1XXX1
01XX1
001X1
00011
00000
393 30 } @{ $_[1] }
394  
395 ];
396 }
397  
398 17 ▶sub _no_sentences {
Subroutine _no_sentences: called CC=38 SCAR=38.6
399 17 my ( $self, $tokens, $window ) = @_;
400 17 my $lhs_window = int( $window / 2 );
401 17 ▶ my $debug = $self->debug || 0;
Condition50%1/2
$self->debug || 0
l!l
017
MC/DC0%0/1
$self->debug || 0
$self->debug
Truth table50%1/2
A
A= $self->debug
Aresult
11
00
402  
403 17 my $num_tokens = $tokens->len;
404 17 my $tokens_arr = $tokens->as_array;
405 17 my %heatmap = ();
406 17 my $token_list_heat = $tokens->get_heat;
407  
408 # this regex is a sanity check for phrases. we replace the \ with a
409 # more promiscuous check because the single space is too naive
410 # for real text (e.g. st. john's)
411 17 my $qre = $self->{_qre};
412 17 my @phrases = @{ $self->{_query}->phrases };
413 17 my $n_terms = $self->{_query}->num_terms;
414 17 my $query_has_phrase = $qre =~ s/(\\ )+/.+/g;
415  
416 17 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 17 17
417 0 warn "token_list_heat: " . dump($token_list_heat);
418 0 warn "n_terms: $n_terms";
419 0 warn "phrases: " . dump( \@phrases );
420 0 warn "query_has_phrase: $query_has_phrase";
421 }
422  
423 # build heatmap
424 17 for (@$token_list_heat) {
425 77 my $token = $tokens->get_token($_);
426 77 $heatmap{ $token->pos } = $token->is_hot;
427 }
428  
429 # make clusters
430  
431 # $proximity == (1/4 of $window)+1 is somewhat arbitrary,
432 # but since we want to err in having too much context,
433 # we aim high. Worst case scenario is where there are
434 # multiple hot spots in a cluster and each is a full
435 # $proximity length apart, which will grow the
436 # eventual span far beyond $window size. We rely
437 # on max_chars in Snipper to catch that worst case.
438 17 my $proximity = int( $lhs_window / 2 ) + 1;
439 17 my @positions = sort { $a <=> $b } keys %heatmap;
440 17 my @clusters = ( [] );
441 17 my $i = 0;
442 17 for my $pos (@positions) {
443  
444 # if we have advanced past the first position
445 # and the previous position is not "close" to this one,
446 # start a new cluster
447 77 ▶ if ( $i && ( $pos - $positions[ $i - 1 ] ) > $proximity ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i and $pos - $positions[$i - 1] > $proximity) { } 33 44 77
Condition100%3/3
$i and $pos - $positions[$i - 1] > $proximity
!ll&&!rl&&r
172733
MC/DC100%2/2
$i and $pos - $positions[$i - 1] > $proximity
$i $pos - $positions[$i - 1] > $proximity
Truth table100%3/3
A and B
A= $iB= $pos - $positions[$i - 1] > $proximity
ABresult
0X0
100
111
448 33 push( @clusters, [$pos] );
449 }
450 else {
451 44 push( @{ $clusters[-1] }, $pos );
452 }
453 77 $i++;
454 }
455  
456 $debug
457 17 ▶ and warn "proximity: $proximity clusters: " . dump \@clusters;
Branch: 1 branch
BranchTrueFalseTotal
$debug and warn "proximity: $proximity clusters: " . Search::Tools::HeatMap::dump(\@clusters) 0 17 17
458  
459 # create spans from each cluster, each with a weight.
460 # we do the initial sort so that clusters that overlap
461 # other clusters via get_window() are weeded out via %seen_pos.
462 17 my @spans;
463 my %seen_pos;
464 CLUSTER:
465 17 for my $cluster (
466 sort {
467 scalar(@$b) <=> scalar(@$a)
468 61 ▶ || $heatmap{ $b->[0] } <=> $heatmap{ $a->[0] }
Condition66%4/6
scalar @$b <=> scalar @$a || $heatmap{$Search::Tools::HeatMap::b->[0]} <=> $heatmap{$Search::Tools::HeatMap::a->[0]} || $Search::Tools::HeatMap::a->[0] <=> $Search::Tools::HeatMap::b->[0]
l!l&&r!l&&!r
19420
scalar @$b <=> scalar @$a or $heatmap{$Search::Tools::HeatMap::b->[0]} <=> $heatmap{$Search::Tools::HeatMap::a->[0]}
l!l&&r!l&&!r
19042
MC/DC0%0/3
scalar @$b <=> scalar @$a || $heatmap{$Search::Tools::HeatMap::b->[0]} <=> $heatmap{$Search::Tools::HeatMap::a->[0]} || $Search::Tools::HeatMap::a->[0] <=> $Search::Tools::HeatMap::b->[0]
scalar @$b <=> scalar @$a $heatmap{$Search::Tools::HeatMap::b->[0]} <=> $heatmap{$Search::Tools::HeatMap::a->[0]} $Search::Tools::HeatMap::a->[0] <=> $Search::Tools::HeatMap::b->[0]
Truth table50%2/4
A or B || C
A= scalar @$b <=> scalar @$aB= $heatmap{$Search::Tools::HeatMap::b->[0]} <=> $heatmap{$Search::Tools::HeatMap::a->[0]}C= $Search::Tools::HeatMap::a->[0] <=> $Search::Tools::HeatMap::b->[0]
ABCresult
1XX1
01X1
0011
0000
469 || $a->[0] <=> $b->[0]
470 } @clusters
471 )
472 {
473  
474 # get full window, ignoring positions we've already seen.
475 50 my $heat = 0;
476 50 my %span;
477 my @cluster_tokens;
478 50 POS: for my $pos (@$cluster) {
479 77 my ( $start, $end ) = $tokens->get_window( $pos, $window );
480 77 POS_TWO: for my $pos2 ( $start .. $end ) {
481 3357 ▶ next if $seen_pos{$pos2}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen_pos{$pos2}++ 1844 1513 3357
482 1513 ▶ $heat += ( exists $heatmap{$pos2} ? $heatmap{$pos2} : 0 );
Branch: 1 branch
BranchTrueFalseTotal
exists $heatmap{$pos2} ? : 77 1436 1513
483 1513 push( @cluster_tokens, $tokens->get_token($pos2) );
484 }
485 }
486  
487 # we may have skipped a $seen_pos from the $slice above
488 # so make sure we still start/end on a match
489 50 ▶ while ( @cluster_tokens && !$cluster_tokens[0]->is_match ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@cluster_tokens and not $cluster_tokens[0]->is_match) 11 50 61
Condition66%2/3
@cluster_tokens and not $cluster_tokens[0]->is_match
!ll&&!rl&&r
05011
MC/DC50%1/2
@cluster_tokens and not $cluster_tokens[0]->is_match
@cluster_tokens not $cluster_tokens[0]->is_match
Truth table66%2/3
A and not(B)
A= @cluster_tokensB= not $cluster_tokens[0]->is_match
ABresult
0X0
100
111
490 11 shift @cluster_tokens;
491 }
492 50 ▶ while ( @cluster_tokens && !$cluster_tokens[-1]->is_match ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@cluster_tokens and not $cluster_tokens[-1]->is_match) 6 50 56
Condition66%2/3
@cluster_tokens and not $cluster_tokens[-1]->is_match
!ll&&!rl&&r
0506
MC/DC50%1/2
@cluster_tokens and not $cluster_tokens[-1]->is_match
@cluster_tokens not $cluster_tokens[-1]->is_match
Truth table66%2/3
A and not(B)
A= @cluster_tokensB= not $cluster_tokens[-1]->is_match
ABresult
0X0
100
111
493 6 pop @cluster_tokens;
494 }
495  
496 50 ▶ next CLUSTER unless @cluster_tokens;
Branch: 1 branch
BranchTrueFalseTotal
unless @cluster_tokens 0 50 50
497  
498 # sanity: make sure we still have something hot
499 50 my $has_hot = 0;
500 50 my @cluster_pos;
501 my @strings;
502 50 for (@cluster_tokens) {
503 1496 my $pos = $_->pos;
504 1496 ▶ $has_hot++ if exists $heatmap{$pos};
Branch: 1 branch
BranchTrueFalseTotal
if exists $heatmap{$pos} 77 1419 1496
505 1496 push @strings, $_->str;
506 1496 push @cluster_pos, $pos;
507 }
508 50 ▶ next CLUSTER unless $has_hot;
Branch: 1 branch
BranchTrueFalseTotal
unless $has_hot 11 39 50
509  
510 39 $span{cluster} = $cluster;
511 39 $span{heat} = $heat;
512 39 $span{pos} = \@cluster_pos;
513 39 $span{tokens} = \@cluster_tokens;
514 39 $span{str} = join( '', @strings );
515  
516 # spans with more *unique* hot tokens in a single span rank higher
517 # spans with more *proximate* hot tokens in a single span rank higher
518 39 my %uniq = ();
519 39 my $i = 0;
520 39 my $num_proximate = 1; # one for the single hot token
521 39 for (@cluster_pos) {
522 1341 ▶ if ( exists $heatmap{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $heatmap{$_}) 77 1264 1341
523 77 $uniq{ lc $strings[$i] } += $heatmap{$_};
524 77 ▶ if ( $i && exists $heatmap{ $cluster_pos[ $i - 2 ] } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i and exists $heatmap{$cluster_pos[$i - 2]}) 23 54 77
Condition100%3/3
$i and exists $heatmap{$cluster_pos[$i - 2]}
!ll&&!rl&&r
54923
MC/DC100%2/2
$i and exists $heatmap{$cluster_pos[$i - 2]}
$i exists $heatmap{$cluster_pos[$i - 2]}
Truth table100%3/3
A and B
A= $iB= exists $heatmap{$cluster_pos[$i - 2]}
ABresult
0X0
100
111
525 23 $num_proximate++;
526 }
527 }
528 1341 $i++;
529 }
530 39 $span{unique} = scalar keys %uniq;
531 39 $span{proximate} = $num_proximate;
532  
533 # no false phrase matches if !_treat_phrases_as_singles
534 # stemmer check because regex will likely fail when stemmer is on
535 39 ▶ if ( $query_has_phrase
Branch: 1 branch
BranchTrueFalseTotal
if ($query_has_phrase and not $self->{"_treat_phrases_as_singles"}) 3 36 39
Condition66%2/3
$query_has_phrase and not $self->{"_treat_phrases_as_singles"}
!ll&&!rl&&r
3603
MC/DC50%1/2
$query_has_phrase and not $self->{"_treat_phrases_as_singles"}
$query_has_phrase not $self->{"_treat_phrases_as_singles"}
Truth table66%2/3
A and not(B)
A= $query_has_phraseB= not $self->{"_treat_phrases_as_singles"}
ABresult
0X0
100
111
536 and !$self->{_treat_phrases_as_singles} )
537 {
538 3 ▶ if ( !$self->{_stemmer} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'_stemmer'}) { } 1 2 3
539  
540 #warn "_treat_phrases_as_singles NOT true";
541 1 ▶ if ( $span{str} !~ m/$qre/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($span{"str"} =~ /$qre/) 1 0 1
542 1 ▶ $debug
Branch: 1 branch
BranchTrueFalseTotal
$debug and warn qq[treat_phrases_as_singles=FALSE and '$span{"str"}' failed to match $qre\n] 0 1 1
543 and warn
544 "treat_phrases_as_singles=FALSE and '$span{str}' failed to match $qre\n";
545 1 next CLUSTER;
546 }
547 }
548 else {
549  
550 # stemmer used, so check unique term count against n_terms
551 2 ▶ if ( $n_terms == $query_has_phrase
Branch: 1 branch
BranchTrueFalseTotal
if ($n_terms == $query_has_phrase and $n_terms > $span{"unique"}) 0 2 2
Condition66%2/3
$n_terms == $query_has_phrase and $n_terms > $span{"unique"}
!ll&&!rl&&r
110
MC/DC0%0/2
$n_terms == $query_has_phrase and $n_terms > $span{"unique"}
$n_terms == $query_has_phrase $n_terms > $span{"unique"}
Truth table66%2/3
A and B
A= $n_terms == $query_has_phraseB= $n_terms > $span{"unique"}
ABresult
0X0
100
111
552 && $n_terms > $span{unique} )
553 {
554 0 ▶ $debug
Branch: 1 branch
BranchTrueFalseTotal
$debug and warn qq[treat_phrases_as_singles=FALSE and '$span{"str"}' ] . qq[expected $n_terms but got $span{"unique"}\n] 0 0 0
555 and warn
556 "treat_phrases_as_singles=FALSE and '$span{str}' "
557 . "expected $n_terms but got $span{unique}\n";
558 0 next CLUSTER;
559 }
560  
561 }
562 }
563  
564 # just for debug
565 38 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 38 38
566 0 my $i = 0;
567 $span{str_w_pos} = join(
568 '',
569 map {
570 0 $strings[ $i++ ]
571 . ( exists $heatmap{$_} ? $OPEN : '[' )
572 . $_
573 0 ▶ . ( exists $heatmap{$_} ? $CLOSE : ']' )
Branch: 2 branches
BranchTrueFalseTotal
exists $heatmap{$_} ? : 0 0 0
exists $heatmap{$_} ? : 0 0 0
574 } @cluster_pos
575 );
576 }
577  
578 38 push @spans, \%span;
579  
580 }
581  
582 17 $self->{spans} = $self->_sort_spans( \@spans );
583 17 $self->{heatmap} = \%heatmap;
584  
585 17 return $self;
586 }
587  
588 =head2 has_spans
589  
590 Returns the number of spans found.
591  
592 =cut
593  
594 30 ▶sub has_spans {
Subroutine has_spans: called CC=1 SCAR=0.0
Pod documented
595 30 return scalar @{ $_[0]->{spans} };
596 }
597  
598 17 1;
599  
600 __END__
« blib/lib/Search/Tools/Snipper.pm ↑ Summary blib/lib/Search/Tools/QueryParser.pm »