| 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.006'; | |||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | # debugging only | ||||||||||||||||||||||||||||||||||||||||||
| 12 | 17 | my $OPEN = '['; | |||||||||||||||||||||||||||||||||||||||||
| 13 | 17 | my $CLOSE = ']'; | |||||||||||||||||||||||||||||||||||||||||
| 14 | 17 | eval { require Term::ANSIColor; }; | |||||||||||||||||||||||||||||||||||||||||
| 15 | 17 | ▶ | if ( !$@ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | sub BUILD { | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 30 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | sub _build { | ||||||||||||||||||||||||||||||||||||||||||
| 136 | 30 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build: called CC=5 SCAR=16.3
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | 30 | ▶ | my $tokens = $self->tokens or croak "tokens required"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 30 | ▶ | my $window = $self->window_size || 20; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->window_size || 20
MC/DC0%0/1
$self->window_size || 20
$self->window_size
Truth table50%1/2
A
A= $self->window_size
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | 30 | ▶ | my $as_sentences = $self->as_sentences || 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->as_sentences || 0
MC/DC100%1/1
$self->as_sentences || 0
$self->as_sentences
Truth table100%2/2
A
A= $self->as_sentences
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | 30 | ▶ | return $as_sentences | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | sub _as_sentences { | ||||||||||||||||||||||||||||||||||||||||||
| 152 | 13 | ▶ | my ( $self, $tokens, $window ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_as_sentences: called CC=41 SCAR=42.1
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | 13 | ▶ | my $debug = $self->debug || 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->debug || 0
MC/DC0%0/1
$self->debug || 0
$self->debug
Truth table50%1/2
A
A= $self->debug
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 192 | 22 | ▶ | $debug | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $heat_sentence_starts->[$i] and $heat_sentence_starts->[$i] != $start
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 221 | 1990 | my $tok = $tokens->get_token( $end++ ); | |||||||||||||||||||||||||||||||||||||||||
| 222 | 1990 | ▶ | if ( !$tok ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 223 | 0 | ▶ | $debug and warn "No token at end=$end"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||||||||||||
| 226 | 1990 | ▶ | if ( $tok->is_sentence_end ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 227 | 10 | $end--; # move back one position | |||||||||||||||||||||||||||||||||||||||||
| 228 | 10 | ▶ | if ($debug) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | 20 | ▶ | $debug | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 247 | 0 | ▶ | $debug | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 273 | 2000 | ▶ | $heat += ( exists $heatmap{$pos} ? $heatmap{$pos} : 0 ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 287 | 2000 | push @strings, $_->str; | |||||||||||||||||||||||||||||||||||||||||
| 288 | 2000 | push @cluster_pos, $pos; | |||||||||||||||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||||||||||||||||
| 290 | 20 | ▶ | next START_END unless $has_hot; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 311 | 42 | $uniq{ lc $strings[$i] } += $heatmap{$_}; | |||||||||||||||||||||||||||||||||||||||||
| 312 | 42 | ▶ | if ( $i && exists $heatmap{ $cluster_pos[ $i - 2 ] } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$i and exists $heatmap{$cluster_pos[$i - 2]}
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]}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$query_has_phrase and not $self->{"_treat_phrases_as_singles"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 325 | and !$self->{_treat_phrases_as_singles} ) | ||||||||||||||||||||||||||||||||||||||||||
| 326 | { | ||||||||||||||||||||||||||||||||||||||||||
| 327 | 7 | ▶ | if ( !$self->{_stemmer} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||
| 329 | #warn "_treat_phrases_as_singles NOT true"; | ||||||||||||||||||||||||||||||||||||||||||
| 330 | 3 | ▶ | if ( $span{str} !~ m/$qre/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 331 | 0 | ▶ | $debug | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$n_terms == $query_has_phrase and $n_terms > $span{"unique"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 343 | && $n_terms > $span{unique} ) | ||||||||||||||||||||||||||||||||||||||||||
| 344 | { | ||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||
| 346 | 0 | ▶ | $debug | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | sub _sort_spans { | ||||||||||||||||||||||||||||||||||||||||||
| 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] | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$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"}
$Search::Tools::HeatMap::b->{"unique"} <=> $Search::Tools::HeatMap::a->{"unique"} or $Search::Tools::HeatMap::b->{"proximate"} <=> $Search::Tools::HeatMap::a->{"proximate"}
MC/DC0%0/3
$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"}
$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"}
Truth table0%0/4
A or B or C
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 393 | 30 | ▶ | } @{ $_[1] } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_sort_spans: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||||
| 395 | ]; | ||||||||||||||||||||||||||||||||||||||||||
| 396 | } | ||||||||||||||||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||||||||||||||||
| 398 | sub _no_sentences { | ||||||||||||||||||||||||||||||||||||||||||
| 399 | 17 | ▶ | my ( $self, $tokens, $window ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_no_sentences: called CC=38 SCAR=38.4
| |||||||||||||||||||||||||||||||||||||||||||
| 400 | 17 | my $lhs_window = int( $window / 2 ); | |||||||||||||||||||||||||||||||||||||||||
| 401 | 17 | ▶ | my $debug = $self->debug || 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->debug || 0
MC/DC0%0/1
$self->debug || 0
$self->debug
Truth table50%1/2
A
A= $self->debug
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$i and $pos - $positions[$i - 1] > $proximity
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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] } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
scalar @$b <=> scalar @$a or $heatmap{$Search::Tools::HeatMap::b->[0]} <=> $heatmap{$Search::Tools::HeatMap::a->[0]}
MC/DC50%1/2
scalar @$b <=> scalar @$a or $heatmap{$Search::Tools::HeatMap::b->[0]} <=> $heatmap{$Search::Tools::HeatMap::a->[0]}
scalar @$b <=> scalar @$a
$heatmap{$Search::Tools::HeatMap::b->[0]} <=> $heatmap{$Search::Tools::HeatMap::a->[0]}
Truth table66%2/3
A or B
A= scalar @$b <=> scalar @$aB= $heatmap{$Search::Tools::HeatMap::b->[0]} <=> $heatmap{$Search::Tools::HeatMap::a->[0]}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 482 | 1513 | ▶ | $heat += ( exists $heatmap{$pos2} ? $heatmap{$pos2} : 0 ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
@cluster_tokens and not $cluster_tokens[0]->is_match
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
| |||||||||||||||||||||||||||||||||||||||||||
| 490 | 11 | shift @cluster_tokens; | |||||||||||||||||||||||||||||||||||||||||
| 491 | } | ||||||||||||||||||||||||||||||||||||||||||
| 492 | 50 | ▶ | while ( @cluster_tokens && !$cluster_tokens[-1]->is_match ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@cluster_tokens and not $cluster_tokens[-1]->is_match
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
| |||||||||||||||||||||||||||||||||||||||||||
| 493 | 6 | pop @cluster_tokens; | |||||||||||||||||||||||||||||||||||||||||
| 494 | } | ||||||||||||||||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||||||||||||||||
| 496 | 50 | ▶ | next CLUSTER unless @cluster_tokens; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 505 | 1496 | push @strings, $_->str; | |||||||||||||||||||||||||||||||||||||||||
| 506 | 1496 | push @cluster_pos, $pos; | |||||||||||||||||||||||||||||||||||||||||
| 507 | } | ||||||||||||||||||||||||||||||||||||||||||
| 508 | 50 | ▶ | next CLUSTER unless $has_hot; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 523 | 77 | $uniq{ lc $strings[$i] } += $heatmap{$_}; | |||||||||||||||||||||||||||||||||||||||||
| 524 | 77 | ▶ | if ( $i && exists $heatmap{ $cluster_pos[ $i - 2 ] } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$i and exists $heatmap{$cluster_pos[$i - 2]}
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]}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$query_has_phrase and not $self->{"_treat_phrases_as_singles"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 536 | and !$self->{_treat_phrases_as_singles} ) | ||||||||||||||||||||||||||||||||||||||||||
| 537 | { | ||||||||||||||||||||||||||||||||||||||||||
| 538 | 3 | ▶ | if ( !$self->{_stemmer} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||||||||||||||||
| 540 | #warn "_treat_phrases_as_singles NOT true"; | ||||||||||||||||||||||||||||||||||||||||||
| 541 | 1 | ▶ | if ( $span{str} !~ m/$qre/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 542 | 1 | ▶ | $debug | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$n_terms == $query_has_phrase and $n_terms > $span{"unique"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 552 | && $n_terms > $span{unique} ) | ||||||||||||||||||||||||||||||||||||||||||
| 553 | { | ||||||||||||||||||||||||||||||||||||||||||
| 554 | 0 | ▶ | $debug | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | sub has_spans { | ||||||||||||||||||||||||||||||||||||||||||
| 595 | 30 | ▶ | return scalar @{ $_[0]->{spans} }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
has_spans: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 596 | } | ||||||||||||||||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||||||||||||||||
| 598 | 17 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 599 | |||||||||||||||||||||||||||||||||||||||||||
| 600 | __END__ | ||||||||||||||||||||||||||||||||||||||||||