blib/lib/Search/Tools/Snipper.pm

filter: Statementstmt 69.4% 255 / 367 Branchbran 47.0% 81 / 172 Conditioncond 53.0% 26 / 49 MC/DCmcdc 32.2% 10 / 31 Subroutinesub 96.1% 25 / 26 Podpod 100% 2 / 2 totaltotal 61.6% 399 / 647 CC 110 SCAR 47.9
CC
110
Cov
62%
_loop
462.0
_re_match
170.5
_re
21.5
CRAP 120.0
↑ Summary blib/lib/Search/Tools/HeatMap.pm »
1 package Search::Tools::Snipper;
2 17 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 17 extends 'Search::Tools::Object';
4 17 with 'Search::Tools::ArgNormalizer';
5 17 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 17 ▶use Data::Dump qw( dump );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 17 ▶use Search::Tools::XML;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 17 ▶use Search::Tools::UTF8;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 17 ▶use Search::Tools::Tokenizer;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 17 ▶use Search::Tools::HeatMap;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 17 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 17 our $VERSION = '1.007';
15  
16 # extra space here so pmvers works against $VERSION
17 17 our $ellip = ' ... ';
18 17 our $DefaultSnipper = 'offset';
19  
20 #
21 # TODO allow for returning an array ref of
22 # extracts instead of joining them all with $ellip
23 #
24  
25 17 my @attrs = qw(
26 as_sentences
27 collapse_whitespace
28 context
29 count
30 escape
31 force
32 ignore_length
33 max_chars
34 occur
35 query
36 show
37 snipper
38 strip_markup
39 treat_phrases_as_singles
40 type
41 type_used
42 use_pp
43 word_len
44 );
45  
46 17 my %Defaults = (
47 type => $DefaultSnipper,
48 occur => 5,
49 max_chars => 300,
50 context => 8,
51 word_len => 4, # TODO still used?
52 show => 1,
53 collapse_whitespace => 1,
54 escape => 0,
55 force => 0,
56 as_sentences => 0,
57 ignore_length => 0,
58 treat_phrases_as_singles => 1,
59 strip_markup => 0,
60 );
61  
62 17 for my $attr (@attrs) {
63 306 ▶ my $def = $Defaults{$attr} || undef;
Condition100%2/2
$Defaults{$attr} || undef
l!l
136170
MC/DC100%1/1
$Defaults{$attr} || undef
$Defaults{$attr}
Truth table100%2/2
A
A= $Defaults{$attr}
Aresult
11
00
64 306 ▶ if ( defined $def ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $def) { } 136 170 306
65 163 ▶ has( $attr => ( is => 'rw', default => sub {$def} ) );
Subroutine __ANON__: called CC=1 SCAR=0.0
66 }
67 else {
68 170 has( $attr => ( is => 'rw' ) );
69 }
70 }
71  
72 30 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod documented
73 30 my $self = shift;
74  
75 #dump $self;
76  
77 30 $self->{_tokenizer} = Search::Tools::Tokenizer->new(
78 re => $self->query->qp->term_re,
79 debug => $self->debug,
80 );
81  
82 30 my $wc = $self->query->qp->word_characters;
83  
84 # regexp for splitting into terms in _re()
85 30 $self->{_wc_regexp} = qr/[^$wc]+/io;
86  
87 $self->{_qre}
88 30 = $self->query->terms_as_regex( $self->treat_phrases_as_singles );
89  
90 30 $self->count(0);
91  
92 30 return $self;
93 }
94  
95 # I tried Text::Context but that was too slow.
96 # Here are several different models.
97 # I have found that _loop() is faster for single-word queries,
98 # while _re() seems to be the best compromise between speed and accuracy.
99 # New in version 0.24 is _token() which is mostly XS and should be best.
100  
101 31 ▶sub _pick_snipper {
Subroutine _pick_snipper: called CC=3 SCAR=11.0
102 31 my ( $self, $text ) = @_;
103 31 ▶ my $snipper_name = $self->type || $DefaultSnipper;
Condition66%2/3
$self->type || $Search::Tools::Snipper::DefaultSnipper
l!l&&r!l&&!r
2740
MC/DC0%0/2
$self->type || $Search::Tools::Snipper::DefaultSnipper
$self->type $Search::Tools::Snipper::DefaultSnipper
Truth table66%2/3
A || B
A= $self->typeB= $Search::Tools::Snipper::DefaultSnipper
ABresult
1X1
011
000
104 31 ▶ if ( $self->query->qp->stemmer ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->query->qp->stemmer) 5 26 31
105 5 $snipper_name = 'token';
106 }
107 31 my $method_name = '_' . $snipper_name;
108 31 $self->type_used($snipper_name);
109 31 ▶ my $func = sub { shift->$method_name(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
110 31 return $func;
111 }
112  
113 # 2 passes, excluding ' ' in the first one,
114 # is 60% faster than a single pass including ' '.
115 # likely because there are far fewer matches
116 # in either of the 2 than the 1.
117 62 ▶sub _normalize_whitespace {
Subroutine _normalize_whitespace: called CC=1 SCAR=0.0
118 62 $_[0] =~ s,[\n\r\t\xa0]+,\ ,go;
119 62 $_[0] =~ s,\ +, ,go; # \ \ + was 16x slower on bigfile!!
120 }
121  
122 33 ▶sub snip {
Subroutine snip: called CC=17 SCAR=30.0
Pod documented
123 33 my $self = shift;
124 33 my $text = shift;
125 33 ▶ if ( !defined $text ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $text) 0 33 33
126 0 croak "text required to snip";
127 }
128  
129 # normalize encoding, esp for regular expressions.
130 33 $text = to_utf8($text);
131  
132 # don't snip if we're less than the threshold
133 33 ▶ if ( length($text) < $self->max_chars && !$self->ignore_length ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $text < $self->max_chars and not $self->ignore_length) 2 31 33
Condition100%3/3
length $text < $self->max_chars and not $self->ignore_length
!ll&&!rl&&r
2652
MC/DC100%2/2
length $text < $self->max_chars and not $self->ignore_length
length $text < $self->max_chars not $self->ignore_length
Truth table100%3/3
A and not(B)
A= length $text < $self->max_charsB= not $self->ignore_length
ABresult
0X0
100
111
134 2 ▶ if ( $self->show ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->show) 2 0 2
135 2 ▶ if ( $self->strip_markup ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->strip_markup) 0 2 2
136 0 return Search::Tools::XML->no_html($text);
137 }
138 2 return $text;
139 }
140 0 return '';
141 }
142  
143 31 ▶ if ( $self->strip_markup ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->strip_markup) 1 30 31
144 1 $text = Search::Tools::XML->no_html($text);
145 }
146  
147 31 ▶ if ( $self->collapse_whitespace ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->collapse_whitespace) 31 0 31
148 31 _normalize_whitespace($text);
149 }
150  
151 # we calculate the snipper each time since caller
152 # may set type() or snipper() between calls to snip().
153 31 ▶ my $func = $self->snipper || $self->_pick_snipper($text);
Condition33%1/3
$self->snipper || $self->_pick_snipper($text)
l!l&&r!l&&!r
0310
MC/DC0%0/2
$self->snipper || $self->_pick_snipper($text)
$self->snipper $self->_pick_snipper($text)
Truth table33%1/3
A || B
A= $self->snipperB= $self->_pick_snipper($text)
ABresult
1X1
011
000
154  
155 31 my $s = $func->( $self, $text );
156  
157 31 ▶ $self->debug and warn "snipped: '$s'\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and warn "snipped: '${s}'\n" 0 31 31
158  
159 # sanity check
160 31 ▶ if ( length($s) > ( $self->max_chars * 4 ) && !$self->ignore_length ) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $s > $self->max_chars * 4 and not $self->ignore_length) { } 1 30 31
elsif (not length $s || $self->ignore_length) { } 1 29 30
Condition100%3/3
length $s > $self->max_chars * 4 and not $self->ignore_length
!ll&&!rl&&r
2911
MC/DC100%2/2
length $s > $self->max_chars * 4 and not $self->ignore_length
length $s > $self->max_chars * 4 not $self->ignore_length
Truth table100%3/3
A and not(B)
A= length $s > $self->max_chars * 4B= not $self->ignore_length
ABresult
0X0
100
111
161 1 $s = $self->_dumb($s);
162 1 ▶ $self->debug and warn "too long. dumb snip: '$s'\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and warn "too long. dumb snip: '${s}'\n" 0 1 1
163 }
164 ▶ elsif ( !length($s) && !$self->ignore_length ) {
Condition66%2/3
length $s || $self->ignore_length
l!l&&r!l&&!r
2901
MC/DC50%1/2
length $s || $self->ignore_length
length $s $self->ignore_length
Truth table66%2/3
A || B
A= length $sB= $self->ignore_length
ABresult
1X1
011
000
165 1 $s = $self->_dumb($text);
166 1 ▶ $self->debug and warn "too short. dumb snip: '$s'\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and warn "too short. dumb snip: '${s}'\n" 0 1 1
167 }
168  
169 # escape entities before collapsing whitespace.
170 31 $s = $self->_escape($s);
171  
172 31 ▶ if ( $self->collapse_whitespace ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->collapse_whitespace) 31 0 31
173 31 _normalize_whitespace($s);
174 }
175  
176 31 return $s;
177  
178 }
179  
180 30 ▶sub _token {
Subroutine _token: called CC=15 SCAR=27.1
181 30 my $self = shift;
182 30 my $qre = $self->{_qre};
183 30 ▶ $self->debug and warn "\$qre: $qre";
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and warn "\$qre: $qre" 0 30 30
184  
185 30 ▶ my $method = ( $self->{use_pp} ) ? 'tokenize_pp' : 'tokenize';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'use_pp'} ? : 1 29 30
186  
187 # must split phrases into OR'd regex or else no heat is generated.
188 30 my $qre_ORd = $qre;
189 30 $qre_ORd =~ s/(\\ )+/\|/g;
190 30 my $heat_seeker = qr/^$qre_ORd$/;
191  
192 # if stemmer is on, we must stem each token to look for a match
193 30 ▶ if ( $self->query->qp->stemmer ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->query->qp->stemmer) 5 25 30
194 5 my $stemmer = $self->query->qp->stemmer;
195 5 my $qp = $self->query->qp;
196 5 my $re = $heat_seeker;
197 486 ▶ $heat_seeker = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
198 486 my ($token) = @_;
199 486 my $st = $stemmer->( $qp, $token->str );
200 486 return $st =~ m/$re/;
201 5 };
202 }
203 30 my $tokens = $self->{_tokenizer}->$method( $_[0], $heat_seeker );
204  
205 #$self->debug and $tokens->dump;
206  
207 30 ▶ return $self->_dumb( $_[0] ) unless scalar @{ $tokens->get_heat };
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$tokens->get_heat;} 0 30 30
208  
209 my $heatmap = Search::Tools::HeatMap->new(
210 tokens => $tokens,
211 window_size => $self->{context},
212 as_sentences => $self->{as_sentences},
213 debug => $self->debug,
214 _query => $self->query,
215 _qre => $qre,
216 _treat_phrases_as_singles => $self->{treat_phrases_as_singles},
217 30 _stemmer => $self->query->qp->stemmer,
218 );
219  
220 # reduce noise in debug
221 30 delete $heatmap->{_query};
222  
223 30 ▶ $self->debug and warn "heatmap: " . dump $heatmap;
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and warn 'heatmap: ' . Search::Tools::Snipper::dump($heatmap) 0 30 30
224  
225 30 my $tokens_arr = $tokens->as_array;
226  
227 #warn "snips: " . dump $heatmap->spans;
228 30 ▶ if ( $heatmap->has_spans ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($heatmap->has_spans) { } 29 1 30
229  
230 # stringify positions
231 29 my @snips;
232 29 for my $span ( @{ $heatmap->spans } ) {
233  
234 58 ▶ $self->debug and warn '>>>' . $span->{str_w_pos} . '<<<';
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and warn '>>>' . $span->{"str_w_pos"} . '<<<' 0 58 58
235 58 push( @snips, $span->{str} );
236 }
237 29 my $occur_index = $self->occur - 1;
238 29 ▶ if ( $#snips > $occur_index ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#snips > $occur_index) 5 24 29
239 5 @snips = @snips[ 0 .. $occur_index ];
240 }
241 29 my $snip = join( $ellip, @snips );
242 29 my $snips_start_with_query = $_[0] =~ m/^\Q$snip\E/;
243 29 my $snips_end_with_query = $_[0] =~ m/\Q$snip\E$/;
244 29 ▶ if ( $self->{as_sentences} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"as_sentences"}) 13 16 29
245 13 $snips_start_with_query = 1;
246 13 $snips_end_with_query = $snip =~ m/[\.\?\!]\s*$/;
247 }
248  
249 # if we are pulling out something less than the entire
250 # text, insert ellipses...
251 29 ▶ if ( $_[0] ne $snip ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] ne $snip) { } 25 4 29
252 25 ▶ $self->debug and warn "extract is smaller than snip";
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and warn "extract is smaller than snip" 0 25 25
253 25 ▶ my $extract = join( '',
Branch: 2 branches
BranchTrueFalseTotal
$snips_start_with_query ? : 10 15 25
$snips_end_with_query ? : 7 18 25
254 ( $snips_start_with_query ? '' : $ellip ),
255 $snip, ( $snips_end_with_query ? '' : $ellip ) );
256 25 return $extract;
257 }
258 else {
259 4 return $snip;
260 }
261 }
262 else {
263  
264 #warn "no spans. using dumb snip";
265 1 return $self->_dumb( $_[0] );
266 }
267  
268 }
269  
270 25 ▶sub _get_offsets {
Subroutine _get_offsets: called CC=1 SCAR=0.0
271 25 my $self = shift;
272 25 return $self->{_tokenizer}->get_offsets( @_, $self->{_qre} );
273 }
274  
275 25 ▶sub _offset {
Subroutine _offset: called CC=1 SCAR=0.0
276 25 my $self = shift;
277 25 my $txt = shift;
278 25 my $offsets = $self->_get_offsets($txt);
279 25 my $snips = $self->_get_offset_snips( $txt, $offsets );
280 25 return $self->_token( join( '', @$snips ) );
281 }
282  
283 25 ▶sub _get_offset_snips {
Subroutine _get_offset_snips: called CC=11 SCAR=24.0
284 25 my $self = shift;
285 25 my $txt = shift;
286 25 my $offsets = shift;
287  
288 # grab $size chars on either side of each offset
289 # and tokenize each.
290 # $size should be nice and wide to minimize the substr() calls.
291 25 my $size = $self->max_chars * 10;
292  
293 #warn "window size $size";
294  
295 25 my @buf;
296 25 my $len = length($txt);
297 25 ▶ if ( $size > $len ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size > $len) 20 5 25
298  
299 #warn "window bigger than document";
300 20 return [$txt];
301 }
302  
303 5 my ( $seen_start, $seen_end );
304 5 my $last_ending = 0;
305 5 for my $pos (@$offsets) {
306  
307 34 my $tmp;
308  
309 34 my $start = $pos - int( $size / 2 );
310 34 my $end = $pos + int( $size / 2 );
311  
312 # avoid overlaps
313 34 ▶ if ( $last_ending && $start < $last_ending ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_ending and $start < $last_ending) 26 8 34
Condition100%3/3
$last_ending and $start < $last_ending
!ll&&!rl&&r
5326
MC/DC100%2/2
$last_ending and $start < $last_ending
$last_ending $start < $last_ending
Truth table100%3/3
A and B
A= $last_endingB= $start < $last_ending
ABresult
0X0
100
111
314 26 $start = $last_ending + 1;
315 26 $end = $start + $size;
316 }
317  
318 #warn "$start .. $pos .. $end";
319  
320 34 ▶ if ( $pos > $end or $pos < $start ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos > $end or $pos < $start) 23 11 34
Condition66%2/3
$pos > $end or $pos < $start
l!l&&r!l&&!r
02311
MC/DC50%1/2
$pos > $end or $pos < $start
$pos > $end $pos < $start
Truth table66%2/3
A or B
A= $pos > $endB= $pos < $start
ABresult
1X1
011
000
321 23 next;
322 }
323  
324 11 $last_ending = $end;
325  
326 #warn "$start .. $end";
327  
328 # if $pos is close to the front of $txt
329 11 ▶ if ( $start <= 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($start <= 0) { } 1 10 11
elsif ($end > $len) { } 2 8 10
330 1 ▶ next if $seen_start++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen_start++ 0 1 1
331  
332 #warn "start";
333 1 $tmp = substr( $txt, 0, $size );
334 }
335  
336 # if $pos is somewhere near the end
337 elsif ( $end > $len ) {
338 2 ▶ next if $seen_end++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen_end++ 0 2 2
339  
340 #warn "end";
341 2 $tmp = substr( $txt, ( $len - $size ) );
342 }
343  
344 # default is somewhere in the ripe middle.
345 else {
346  
347 #warn "middle";
348 8 $tmp = substr( $txt, $start, $size );
349 }
350  
351 11 push @buf, $tmp;
352 }
353  
354 5 return \@buf;
355 }
356  
357 0 ▶sub _loop {
Subroutine _loop: not called CC=21 SCAR=61.4
358 0 my $self = shift;
359 0 my $txt = shift;
360 0 my $regexp = $self->{_qre};
361  
362 #carp "loop snip: $txt";
363  
364 0 ▶ $self->debug and carp "loop snip regexp: $regexp";
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and carp("loop snip regexp: $regexp") 0 0 0
365  
366 0 ▶ my $debug = $self->debug || 0;
Condition0%0/2
$self->debug || 0
l!l
00
MC/DC0%0/1
$self->debug || 0
$self->debug
Truth table0%0/2
A
A= $self->debug
Aresult
11
00
367  
368 # no matches
369 0 ▶ return $self->_dumb($txt) unless $txt =~ m/$regexp/;
Branch: 1 branch
BranchTrueFalseTotal
unless $txt =~ /$regexp/ 0 0 0
370  
371 #carp "loop snip: $txt";
372  
373 0 my $context = $self->context - 1;
374 0 ▶ my $occur = $self->occur || 1;
Condition0%0/2
$self->occur || 1
l!l
00
MC/DC0%0/1
$self->occur || 1
$self->occur
Truth table0%0/2
A
A= $self->occur
Aresult
11
00
375 0 my @snips;
376  
377 0 my $notwc = $self->{_wc_regexp};
378  
379 0 my @words = split( /($notwc)/, $txt );
380 0 my $count = -1;
381 0 my $start_again = $count;
382 0 my $total = 0;
383 0 my $first_match = 0;
384  
385 0 WORD: for my $w (@words) {
386  
387 0 ▶ if ( $debug > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug > 1) 0 0 0
388 0 ▶ warn ">>\n" if $count % 2;
Branch: 1 branch
BranchTrueFalseTotal
if $count % 2 0 0 0
389 0 warn "word: '$w'\n";
390 }
391  
392 0 $count++;
393 0 ▶ next WORD if $count < $start_again;
Branch: 1 branch
BranchTrueFalseTotal
if $count < $start_again 0 0 0
394  
395 # the next WORD lets us skip past the last frag we excerpted
396  
397 0 my $last = $count - 1;
398 0 my $next = $count + 1;
399  
400 #warn '-' x 30 . "\n";
401 0 ▶ if ( $w =~ m/^$regexp$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($w =~ /^$regexp$/) 0 0 0
402  
403 0 ▶ if ( $debug > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug > 1) 0 0 0
404 0 warn "w: '$w' match: '$1'\n";
405 }
406  
407 0 $first_match = $count;
408  
409 0 my $before = $last - $context;
410 0 ▶ $before = 0 if $before < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $before < 0 0 0 0
411 0 my $after = $next + $context;
412 0 ▶ $after = $#words if $after > $#words;
Branch: 1 branch
BranchTrueFalseTotal
if $after > $#words 0 0 0
413  
414 0 ▶ if ( $debug > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug > 1) 0 0 0
415 0 warn "$before .. $last, $count, $next .. $after\n";
416 }
417  
418 0 my @before = @words[ $before .. $last ];
419 0 my @after = @words[ $next .. $after ];
420  
421 0 my $this_snip_matches = grep {m/^$regexp$/i} ( @before, @after );
422 0 ▶ if ($this_snip_matches) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this_snip_matches) 0 0 0
423 0 $after += $this_snip_matches;
424 0 @after = @words[ $next .. $after ];
425 }
426 0 $total += $this_snip_matches;
427 0 $total++; # for current $w
428  
429 0 my $t = join( '', @before, $w, @after );
430  
431 0 ▶ $t .= $ellip unless $count == $#words;
Branch: 1 branch
BranchTrueFalseTotal
unless $count == $#words 0 0 0
432  
433 0 ▶ if ( $debug > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug > 1) 0 0 0
434 0 warn "t: $t\n";
435 0 warn "this_snip_matches: $this_snip_matches\n";
436 0 warn "total: $total\n";
437 }
438  
439 0 push( @snips, [ $t, $this_snip_matches + 1 ] ); # +1 for $w
440 0 $start_again = $after;
441 }
442  
443 }
444  
445 # sort by match density.
446 # consistent with HeatMap and lets us find
447 # the *best* match, including phrases.
448 0 @snips = map { $_->[0] } sort { $b->[1] <=> $a->[1] } @snips;
449  
450 0 ▶ if ( $debug > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug > 1) 0 0 0
451 0 carp "snips: " . scalar @snips;
452 0 carp "words: $count\n";
453 0 carp "grandtotal: $total\n";
454 0 carp "occur: $occur\n";
455 0 carp '-' x 50 . "\n";
456  
457 }
458  
459 0 $self->count( scalar(@snips) + $self->count );
460 0 my $last_snip = $occur - 1;
461 0 ▶ if ( $last_snip > $#snips ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_snip > $#snips) 0 0 0
462 0 $last_snip = $#snips;
463 }
464  
465 #warn dump \@snips;
466 0 my $snippet = join( '', @snips[ 0 .. $last_snip ] );
467 0 ▶ $self->debug and warn "before no_start_partial: '$snippet'\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and warn "before no_start_partial: '${snippet}'\n" 0 0 0
468  
469 #_no_start_partial($snippet);
470 0 ▶ $snippet = $ellip . $snippet if $first_match;
Branch: 1 branch
BranchTrueFalseTotal
if $first_match 0 0 0
471  
472 0 return $snippet;
473 }
474  
475 1 ▶sub _re {
Subroutine _re: called CC=15 SCAR=30.7
476  
477 # get first N matches for each q, then take one of each till we have $occur
478  
479 1 my $self = shift;
480 1 my $text = shift;
481 1 my @q = @{ $self->query->terms };
482 1 my $occur = $self->occur;
483 1 my $Nchar = $self->context * $self->word_len;
484 1 my $total = 0;
485 1 my $notwc = $self->{_wc_regexp};
486  
487 # get minimum number of snips necessary to meet $occur
488 1 my $snip_per_q = int( $occur / scalar(@q) );
489 1 ▶ $snip_per_q ||= 1;
Condition50%1/2
$snip_per_q ||= 1
l!l
10
MC/DC0%0/1
$snip_per_q ||= 1
$snip_per_q
Truth table50%1/2
A
A= $snip_per_q
Aresult
11
00
490  
491 1 my ( %snips, @snips, %ranges, $snip_starts_with_query );
492 1 $snip_starts_with_query = 0;
493  
494 1 Q: for my $q (@q) {
495 1 $snips{$q} = { t => [], offset => [] };
496  
497 1 ▶ $self->debug and warn "$q : $snip_starts_with_query";
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and warn "$q : $snip_starts_with_query" 0 1 1
498  
499 # try simple regexp first, then more complex if we don't match
500 next Q
501 if $self->_re_match( \$text, $self->query->regex_for($q)->plain,
502 1 ▶ \$total, $snips{$q}, \%ranges, $Nchar, $snip_per_q,
Branch: 1 branch
BranchTrueFalseTotal
if $self->_re_match(\$text, $self->query->regex_for($q)->plain, \$total, $snips{$q}, \%ranges, $Nchar, $snip_per_q, \$snip_starts_with_query) 1 0 1
503 \$snip_starts_with_query );
504  
505 0 ▶ $self->debug and warn "failed match on plain regexp";
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and warn "failed match on plain regexp" 0 0 0
506  
507 0 pos $text = 0; # do we really need to reset this?
508  
509 0 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_re_match(\$text, $self->query->regex_for($q)->html, \$total, $snips{$q}, \%ranges, $Nchar, $snip_per_q, \$snip_starts_with_query)) 0 0 0
510 $self->_re_match(
511 \$text, $self->query->regex_for($q)->html,
512 \$total, $snips{$q},
513 \%ranges, $Nchar,
514 $snip_per_q, \$snip_starts_with_query
515 )
516 )
517 {
518 0 ▶ $self->debug and warn "failed match on html regexp";
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and warn "failed match on html regexp" 0 0 0
519 }
520  
521 }
522  
523 1 ▶ return $self->_dumb($text) unless $total;
Branch: 1 branch
BranchTrueFalseTotal
unless $total 0 1 1
524  
525 # get all snips into one array in order they appeared in $text
526 # should be a max of $snip_per_q in any one $q snip array
527 # so we should have at least $occur in total,
528 # which we'll splice() if need be.
529  
530 1 my %offsets;
531 1 for my $q ( keys %snips ) {
532 1 my @s = @{ $snips{$q}->{t} };
533 1 my @o = @{ $snips{$q}->{offset} };
534  
535 1 my $i = 0;
536 1 for (@s) {
537 1 $offsets{$_} = $o[$i];
538 }
539 }
540 0 @snips = sort { $offsets{$a} <=> $offsets{$b} } keys %offsets;
541  
542 # max = $occur
543 1 @snips = splice @snips, 0, $occur;
544  
545 1 ▶ $self->debug and warn dump( \@snips );
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and warn Search::Tools::Snipper::dump(\@snips) 0 1 1
546  
547 1 my $snip = join( $ellip, @snips );
548 1 ▶ _no_start_partial($snip) unless $snip_starts_with_query;
Branch: 1 branch
BranchTrueFalseTotal
unless $snip_starts_with_query 1 0 1
549 1 ▶ $snip = $ellip . $snip unless $text =~ m/^\Q$snips[0]/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /^\Q$snips[0]\E/i 1 0 1
550 1 ▶ $snip .= $ellip unless $text =~ m/\Q$snips[-1]$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /\Q$snips[-1]$\E/i 1 0 1
551  
552 1 $self->count( scalar(@snips) + $self->count );
553  
554 1 return $snip;
555  
556 }
557  
558 1 ▶sub _re_match {
Subroutine _re_match: called CC=32 SCAR=51.4
559  
560 # the .{0,$Nchar} regexp slows things WAY down. so just match,
561 # then use pos() to get chars before and after.
562  
563 # if escape = 0 and if prefix or suffix contains a < or >,
564 # try to include entire tagset.
565  
566 1 my ( $self, $text, $re, $total, $snips, $ranges, $Nchar, $max_snips,
567 $snip_starts_with_query )
568 = @_;
569  
570 1 my $t_len = length $$text;
571  
572 1 my $cnt = 0;
573  
574 1 ▶ if ( $self->debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) 0 1 1
575 0 warn "re_match regexp: >$re<\n";
576 0 warn "max_snips: $max_snips\n";
577 }
578  
579 1 ▶RE: while ( $$text =~ m/$re/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$text =~ /$re/g) 1 0 1
580  
581 1 my $pos = pos $$text;
582 1 my $before_match = $1;
583 1 my $match = $2;
584 1 my $after_match = $3;
585 1 $cnt++;
586 1 my $len = length $match;
587 1 my $blen = length $before_match;
588 1 ▶ if ( $self->debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) 0 1 1
589 0 warn "re: '$re'\n";
590 0 warn "\$1 = '$before_match' = ", ord($before_match), "\n";
591 0 warn "\$2 = '$match'\n";
592 0 warn "\$3 = '$after_match' = ", ord($after_match), "\n";
593 0 warn "pos = $pos\n";
594 0 warn "len = $len\n";
595 0 warn "blen= $blen\n";
596 }
597  
598 1 ▶ if ( $self->debug && exists $ranges->{$pos} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug and exists $ranges->{$pos}) 0 1 1
Condition33%1/3
$self->debug and exists $ranges->{$pos}
!ll&&!rl&&r
100
MC/DC0%0/2
$self->debug and exists $ranges->{$pos}
$self->debug exists $ranges->{$pos}
Truth table33%1/3
A and B
A= $self->debugB= exists $ranges->{$pos}
ABresult
0X0
100
111
599 0 warn "already found $pos\n";
600 }
601  
602 1 ▶ next RE if exists $ranges->{$pos};
Branch: 1 branch
BranchTrueFalseTotal
if exists $ranges->{$pos} 0 1 1
603  
604 1 ▶ my $start_match = $pos - $len - ( $blen || 1 );
Condition50%1/2
$blen || 1
l!l
10
MC/DC0%0/1
$blen || 1
$blen
Truth table50%1/2
A
A= $blen
Aresult
11
00
605 1 ▶ $start_match = 0 if $start_match < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $start_match < 0 0 1 1
606  
607 1 ▶ $$snip_starts_with_query = 1 if $start_match == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $start_match == 0 0 1 1
608  
609 # sanity
610 1 ▶ $self->debug
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and warn "match should be [$start_match $len]: '", substr($$text, $start_match, $len), "'\n" 0 1 1
611 and warn "match should be [$start_match $len]: '",
612 substr( $$text, $start_match, $len ), "'\n";
613  
614 1 ▶ my $prefix_start
Branch: 1 branch
BranchTrueFalseTotal
$start_match < $Nchar ? : 0 1 1
615 = $start_match < $Nchar
616 ? 0
617 : $start_match - $Nchar;
618  
619 1 my $prefix_len = $start_match - $prefix_start;
620  
621 #$prefix_len++; $prefix_len++;
622  
623 1 my $suffix_start = $pos - length($after_match);
624 1 my $suffix_len = $Nchar;
625 1 my $end = $suffix_start + $suffix_len;
626  
627 # if $end extends beyond, that's ok, substr compensates
628  
629 1 $ranges->{$_}++ for ( $prefix_start .. $end );
630 1 my $prefix = substr( $$text, $prefix_start, $prefix_len );
631 1 my $suffix = substr( $$text, $suffix_start, $suffix_len );
632  
633 1 ▶ if ( $self->debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) 0 1 1
634 0 warn "prefix_start = $prefix_start\n";
635 0 warn "prefix_len = $prefix_len\n";
636 0 warn "start_match = $start_match\n";
637 0 warn "len = $len\n";
638 0 warn "pos = $pos\n";
639 0 warn "char = $Nchar\n";
640 0 warn "suffix_start = $suffix_start\n";
641 0 warn "suffix_len = $suffix_len\n";
642 0 warn "end = $end\n";
643 0 warn "prefix: '$prefix'\n";
644 0 warn "match: '$match'\n";
645 0 warn "suffix: '$suffix'\n";
646 }
647  
648 # try and get whole words if we split one up
649 # _no_*_partial does this more rudely
650  
651 # might be faster to do m/(\S)*$prefix/i
652 # but we couldn't guarantee position accuracy
653 # e.g. if $prefix matched more than once in $$text,
654 # we might pull the wrong \S*
655  
656 1 ▶ unless ( $prefix =~ m/^\s/
Branch: 1 branch
BranchTrueFalseTotal
unless ($prefix =~ /^\s/ or substr($$text, $prefix_start - 1, 1) =~ /(\s)/) 0 1 1
Condition33%1/3
$prefix =~ /^\s/ or substr($$text, $prefix_start - 1, 1) =~ /(\s)/
l!l&&r!l&&!r
100
MC/DC0%0/2
$prefix =~ /^\s/ or substr($$text, $prefix_start - 1, 1) =~ /(\s)/
$prefix =~ /^\s/ substr($$text, $prefix_start - 1, 1) =~ /(\s)/
Truth table33%1/3
A or B
A= $prefix =~ /^\s/B= substr($$text, $prefix_start - 1, 1) =~ /(\s)/
ABresult
1X1
011
000
657 or substr( $$text, $prefix_start - 1, 1 ) =~ m/(\s)/ )
658 {
659 0 ▶ while ( --$prefix_start >= 0
Branch: 1 branch
BranchTrueFalseTotal
if (--$prefix_start >= 0 and substr($$text, $prefix_start, 1) =~ /(\S)/) 0 0 0
Condition0%0/3
--$prefix_start >= 0 and substr($$text, $prefix_start, 1) =~ /(\S)/
!ll&&!rl&&r
000
MC/DC0%0/2
--$prefix_start >= 0 and substr($$text, $prefix_start, 1) =~ /(\S)/
--$prefix_start >= 0 substr($$text, $prefix_start, 1) =~ /(\S)/
Truth table0%0/3
A and B
A= --$prefix_start >= 0B= substr($$text, $prefix_start, 1) =~ /(\S)/
ABresult
0X0
100
111
660 and substr( $$text, $prefix_start, 1 ) =~ m/(\S)/ )
661 {
662 0 my $onemorechar = $1;
663  
664 #warn "adding $onemorechar to prefix\n";
665 0 $prefix = $onemorechar . $prefix;
666  
667 #last if $prefix_start <= 0 or $onemorechar !~ /\S/;
668 }
669 }
670  
671 # do same for suffix
672  
673 # We get error here under -w
674 # about substr outside of string -- is $end undefined sometimes??
675  
676 1 ▶ unless ( $suffix =~ m/\s$/ or substr( $$text, $end, 1 ) =~ m/(\s)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($suffix =~ /\s$/ or substr($$text, $end, 1) =~ /(\s)/) 1 0 1
Condition33%1/3
$suffix =~ /\s$/ or substr($$text, $end, 1) =~ /(\s)/
l!l&&r!l&&!r
001
MC/DC0%0/2
$suffix =~ /\s$/ or substr($$text, $end, 1) =~ /(\s)/
$suffix =~ /\s$/ substr($$text, $end, 1) =~ /(\s)/
Truth table33%1/3
A or B
A= $suffix =~ /\s$/B= substr($$text, $end, 1) =~ /(\s)/
ABresult
1X1
011
000
677 1 ▶ while ( $end <= $t_len
Branch: 1 branch
BranchTrueFalseTotal
if ($end <= $t_len and substr($$text, $end++, 1) =~ /(\S)/) 3 1 4
Condition66%2/3
$end <= $t_len and substr($$text, $end++, 1) =~ /(\S)/
!ll&&!rl&&r
013
MC/DC50%1/2
$end <= $t_len and substr($$text, $end++, 1) =~ /(\S)/
$end <= $t_len substr($$text, $end++, 1) =~ /(\S)/
Truth table66%2/3
A and B
A= $end <= $t_lenB= substr($$text, $end++, 1) =~ /(\S)/
ABresult
0X0
100
111
678 and substr( $$text, $end++, 1 ) =~ m/(\S)/ )
679 {
680  
681 3 my $onemore = $1;
682  
683 #warn "adding $onemore to suffix\n";
684 #warn "before '$suffix'\n";
685 3 $suffix .= $onemore;
686  
687 #warn "after '$suffix'\n";
688 }
689 }
690  
691 # will likely fail to include one half of tagset if other is complete
692 1 ▶ unless ( $self->escape ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->escape) 1 0 1
693 1 my $sanity = 0;
694 1 my @l = ( $prefix =~ /(<)/g );
695 1 my @r = ( $prefix =~ /(>)/g );
696 1 ▶ while ( scalar @l != scalar @r ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @l != scalar @r) 0 1 1
697  
698 0 @l = ( $prefix =~ /(<)/g );
699 0 @r = ( $prefix =~ /(>)/g );
700 last
701 if scalar @l
702 0 ▶ == scalar @r; # don't take any more than we need to
Branch: 1 branch
BranchTrueFalseTotal
if scalar @l == scalar @r 0 0 0
703  
704 0 my $onemorechar = substr( $$text, $prefix_start--, 1 );
705  
706 #warn "tagfix: adding $onemorechar to prefix\n";
707 0 $prefix = $onemorechar . $prefix;
708 0 ▶ last if $prefix_start <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $prefix_start <= 0 0 0 0
709 0 ▶ last if $sanity++ > 100;
Branch: 1 branch
BranchTrueFalseTotal
if $sanity++ > 100 0 0 0
710  
711 }
712  
713 1 $sanity = 0;
714 1 ▶ while ( $suffix =~ /<(\w+)/ && $suffix !~ /<\/$1>/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($suffix =~ /<(\w+)/ and not $suffix =~ m[</$1>]) 0 1 1
Condition33%1/3
$suffix =~ /<(\w+)/ and not $suffix =~ m[</$1>]
!ll&&!rl&&r
100
MC/DC0%0/2
$suffix =~ /<(\w+)/ and not $suffix =~ m[</$1>]
$suffix =~ /<(\w+)/ not $suffix =~ m[</$1>]
Truth table33%1/3
A and not(B)
A= $suffix =~ /<(\w+)/B= not $suffix =~ m[</$1>]
ABresult
0X0
100
111
715  
716 0 my $onemorechar = substr( $$text, $end, 1 );
717  
718 #warn "tagfix: adding $onemorechar to suffix\n";
719 0 $suffix .= $onemorechar;
720 0 ▶ last if ++$end > $t_len;
Branch: 1 branch
BranchTrueFalseTotal
if ++$end > $t_len 0 0 0
721 0 ▶ last if $sanity++ > 100;
Branch: 1 branch
BranchTrueFalseTotal
if $sanity++ > 100 0 0 0
722  
723 }
724 }
725  
726 # warn "prefix: '$prefix'\n";
727 # warn "match: '$match'\n";
728 # warn "suffix: '$suffix'\n";
729  
730 1 my $context = join( '', $prefix, $match, $suffix );
731  
732 #warn "context is '$context'\n";
733  
734 1 push( @{ $snips->{t} }, $context );
735 1 push( @{ $snips->{offset} }, $prefix_start );
736  
737 1 $$total++;
738  
739 # warn '-' x 40, "\n";
740  
741 1 ▶ last if $cnt >= $max_snips;
Branch: 1 branch
BranchTrueFalseTotal
if $cnt >= $max_snips 1 0 1
742 }
743  
744 1 return $cnt;
745 }
746  
747 3 ▶sub _dumb {
Subroutine _dumb: called CC=2 SCAR=6.9
748  
749 # just grap the first X chars and return
750  
751 3 my $self = shift;
752 3 ▶ return '' unless $self->show;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->show 2 1 3
753  
754 1 my $txt = shift;
755 1 my $max = $self->max_chars;
756 1 $self->type_used('dumb');
757  
758 1 my $show = substr( $txt, 0, $max );
759 1 _no_end_partial($show);
760 1 $show .= $ellip;
761  
762 1 $self->count( 1 + $self->count );
763  
764 1 return $show;
765  
766 }
767  
768 1 ▶sub _no_start_partial {
Subroutine _no_start_partial: called CC=1 SCAR=0.0
769 1 $_[0] =~ s/^\S+\s+//gs;
770 }
771  
772 1 ▶sub _no_end_partial {
Subroutine _no_end_partial: called CC=1 SCAR=0.0
773 1 $_[0] =~ s/\s+\S+$//gs;
774 }
775  
776 31 ▶sub _escape {
Subroutine _escape: called CC=2 SCAR=8.1
777 31 ▶ if ( $_[0]->escape ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]->escape) { } 0 31 31
778 0 return Search::Tools::XML->escape( $_[1] );
779 }
780 else {
781 31 return $_[1];
782 }
783 }
784  
785 17 1;
786 __END__
↑ Summary blib/lib/Search/Tools/HeatMap.pm »