| 1 | package KinoSearch1::Highlight::Highlighter; | ||||||||||||||||||||||||||||
| 2 | 18 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 18 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 18 | ▶ | use KinoSearch1::Util::ToolSet; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 18 | ▶ | use base qw( KinoSearch1::Util::Class ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 18 | ▶ | use locale; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 18 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 18 | __PACKAGE__->init_instance_vars( | |||||||||||||||||||||||||||
| 10 | # constructor params / members | ||||||||||||||||||||||||||||
| 11 | excerpt_field => undef, | ||||||||||||||||||||||||||||
| 12 | analyzer => undef, | ||||||||||||||||||||||||||||
| 13 | formatter => undef, | ||||||||||||||||||||||||||||
| 14 | encoder => undef, | ||||||||||||||||||||||||||||
| 15 | terms => undef, | ||||||||||||||||||||||||||||
| 16 | excerpt_length => 200, | ||||||||||||||||||||||||||||
| 17 | pre_tag => undef, # back compat | ||||||||||||||||||||||||||||
| 18 | post_tag => undef, # back compat | ||||||||||||||||||||||||||||
| 19 | token_re => qr/\b\w+(?:'\w+)?\b/, | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # members | ||||||||||||||||||||||||||||
| 22 | limit => undef, | ||||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||
| 24 | 18 | __PACKAGE__->ready_get_set(qw( terms )); | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 18 | ▶ | use KinoSearch1::Highlight::SimpleHTMLFormatter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 18 | ▶ | use KinoSearch1::Highlight::SimpleHTMLEncoder; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 1 | ▶ | sub init_instance { | ||||||||||||||||||||||||||
|
Subroutine
init_instance: called CC=6 SCAR=18.6
Pod
documented
| |||||||||||||||||||||||||||||
| 31 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 32 | croak("Missing required arg 'excerpt_field'") | ||||||||||||||||||||||||||||
| 33 | 1 | ▶ | unless defined $self->{excerpt_field}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 1 | $self->{terms} = []; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # assume HTML | ||||||||||||||||||||||||||||
| 37 | 1 | ▶ | if ( !defined $self->{encoder} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 1 | $self->{encoder} = KinoSearch1::Highlight::SimpleHTMLEncoder->new; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | 1 | ▶ | if ( !defined $self->{formatter} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 1 | my ( $pre_tag, $post_tag ) = @{$self}{qw( pre_tag post_tag )}; | |||||||||||||||||||||||||||
| 42 | 1 | ▶ | $pre_tag = '<strong>' unless defined $pre_tag; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 1 | ▶ | $post_tag = '</strong>' unless defined $post_tag; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 1 | $self->{formatter} = KinoSearch1::Highlight::SimpleHTMLFormatter->new( | |||||||||||||||||||||||||||
| 45 | pre_tag => $pre_tag, | ||||||||||||||||||||||||||||
| 46 | post_tag => $post_tag, | ||||||||||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # scoring window is 1.66 * excerpt_length, with the loc in the middle | ||||||||||||||||||||||||||||
| 51 | 1 | $self->{limit} = int( $self->{excerpt_length} / 3 ); | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 1 | ▶ | sub generate_excerpt { | ||||||||||||||||||||||||||
|
Subroutine
generate_excerpt: called CC=16 SCAR=49.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 55 | 1 | my ( $self, $doc ) = @_; | |||||||||||||||||||||||||||
| 56 | 1 | my $excerpt_length = $self->{excerpt_length}; | |||||||||||||||||||||||||||
| 57 | 1 | my $limit = $self->{limit}; | |||||||||||||||||||||||||||
| 58 | 1 | my $token_re = $self->{token_re}; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # retrieve the text from the chosen field | ||||||||||||||||||||||||||||
| 61 | 1 | my $field = $doc->get_field( $self->{excerpt_field} ); | |||||||||||||||||||||||||||
| 62 | 1 | my $text = $field->get_value; | |||||||||||||||||||||||||||
| 63 | 1 | my $text_length = bytes::length $text; | |||||||||||||||||||||||||||
| 64 | 1 | ▶ | return '' unless $text_length; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | # determine the rough boundaries of the excerpt | ||||||||||||||||||||||||||||
| 67 | 1 | my $posits = $self->_starts_and_ends($field); | |||||||||||||||||||||||||||
| 68 | 1 | my $best_location = $self->_calc_best_location($posits); | |||||||||||||||||||||||||||
| 69 | 1 | my $top = $best_location - $limit; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | # expand the excerpt if the best location is near the end | ||||||||||||||||||||||||||||
| 72 | 1 | ▶ | $top | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | = $text_length - $excerpt_length < $top | ||||||||||||||||||||||||||||
| 74 | ? $text_length - $excerpt_length | ||||||||||||||||||||||||||||
| 75 | : $top; | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | # if the best starting point is the very beginning, cool... | ||||||||||||||||||||||||||||
| 78 | 1 | ▶ | if ( $top <= 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | $top = 0; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | # ... otherwise ... | ||||||||||||||||||||||||||||
| 82 | else { | ||||||||||||||||||||||||||||
| 83 | # lop off $top bytes | ||||||||||||||||||||||||||||
| 84 | 1 | $text = bytes::substr( $text, $top ); | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | # try to start the excerpt at a sentence boundary | ||||||||||||||||||||||||||||
| 87 | 1 | ▶ | if ($text =~ s/ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | \A | ||||||||||||||||||||||||||||
| 89 | ( | ||||||||||||||||||||||||||||
| 90 | \C{0,$limit}? | ||||||||||||||||||||||||||||
| 91 | \.\s+ | ||||||||||||||||||||||||||||
| 92 | ) | ||||||||||||||||||||||||||||
| 93 | //xsm | ||||||||||||||||||||||||||||
| 94 | ) | ||||||||||||||||||||||||||||
| 95 | { | ||||||||||||||||||||||||||||
| 96 | 0 | $top += bytes::length($1); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | # no sentence boundary, so we'll need an ellipsis | ||||||||||||||||||||||||||||
| 99 | else { | ||||||||||||||||||||||||||||
| 100 | # skip past possible partial tokens, prepend an ellipsis | ||||||||||||||||||||||||||||
| 101 | 0 | ▶ | if ($text =~ s/ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | \A | ||||||||||||||||||||||||||||
| 103 | ( | ||||||||||||||||||||||||||||
| 104 | \C{0,$limit}? # don't go outside the window | ||||||||||||||||||||||||||||
| 105 | $token_re # match possible partial token | ||||||||||||||||||||||||||||
| 106 | .*? # ... and any junk following that token | ||||||||||||||||||||||||||||
| 107 | ) | ||||||||||||||||||||||||||||
| 108 | (?=$token_re) # just before the start of a full token... | ||||||||||||||||||||||||||||
| 109 | /... /xsm # ... insert an ellipsis | ||||||||||||||||||||||||||||
| 110 | ) | ||||||||||||||||||||||||||||
| 111 | { | ||||||||||||||||||||||||||||
| 112 | 0 | $top += bytes::length($1); | |||||||||||||||||||||||||||
| 113 | 0 | $top -= 4 # three dots and a space | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | # remove possible partial tokens from the end of the excerpt | ||||||||||||||||||||||||||||
| 119 | 0 | $text = bytes::substr( $text, 0, $excerpt_length + 1 ); | |||||||||||||||||||||||||||
| 120 | 0 | ▶ | if ( bytes::length($text) > $excerpt_length ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 0 | my $extra_char = chop $text; | |||||||||||||||||||||||||||
| 122 | # if the extra char wasn't part of a token, we aren't splitting one | ||||||||||||||||||||||||||||
| 123 | 0 | ▶ | if ( $extra_char =~ $token_re ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 0 | $text =~ s/$token_re$//; # if this is unsuccessful, that's fine | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | # if the excerpt doesn't end with a full stop, end with an an ellipsis | ||||||||||||||||||||||||||||
| 129 | 0 | ▶ | if ( $text !~ /\.\s*\Z/xsm ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 0 | $text =~ s/\W+\Z//xsm; | |||||||||||||||||||||||||||
| 131 | 0 | ▶ | while ( bytes::length($text) + 4 > $excerpt_length ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 0 | my $extra_char = chop $text; | |||||||||||||||||||||||||||
| 133 | 0 | ▶ | if ( $extra_char =~ $token_re ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 0 | $text =~ s/\W+$token_re\Z//xsm; # if unsuccessful, that's fine | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | 0 | $text =~ s/\W+\Z//xsm; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | 0 | $text .= ' ...'; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | # remap locations now that we know the starting and ending bytes | ||||||||||||||||||||||||||||
| 142 | 0 | $text_length = bytes::length($text); | |||||||||||||||||||||||||||
| 143 | 0 | my @relative_starts = map { $_->[0] - $top } @$posits; | |||||||||||||||||||||||||||
| 144 | 0 | my @relative_ends = map { $_->[1] - $top } @$posits; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | # get rid of pairs with at least one member outside the text | ||||||||||||||||||||||||||||
| 147 | 0 | ▶ | while ( @relative_starts and $relative_starts[0] < 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@relative_starts and $relative_starts[0] < 0
MC/DC0%0/2
@relative_starts and $relative_starts[0] < 0
@relative_starts
$relative_starts[0] < 0
Truth table0%0/3
A and B
A= @relative_startsB= $relative_starts[0] < 0
| |||||||||||||||||||||||||||||
| 148 | 0 | shift @relative_starts; | |||||||||||||||||||||||||||
| 149 | 0 | shift @relative_ends; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | 0 | ▶ | while ( @relative_ends and $relative_ends[-1] > $text_length ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@relative_ends and $relative_ends[-1] > $text_length
MC/DC0%0/2
@relative_ends and $relative_ends[-1] > $text_length
@relative_ends
$relative_ends[-1] > $text_length
Truth table0%0/3
A and B
A= @relative_endsB= $relative_ends[-1] > $text_length
| |||||||||||||||||||||||||||||
| 152 | 0 | pop @relative_starts; | |||||||||||||||||||||||||||
| 153 | 0 | pop @relative_ends; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | # insert highlight tags | ||||||||||||||||||||||||||||
| 157 | 0 | my $formatter = $self->{formatter}; | |||||||||||||||||||||||||||
| 158 | 0 | my $encoder = $self->{encoder}; | |||||||||||||||||||||||||||
| 159 | 0 | my $output_text = ''; | |||||||||||||||||||||||||||
| 160 | 0 | my ( $start, $end, $last_start, $last_end ) = ( undef, undef, 0, 0 ); | |||||||||||||||||||||||||||
| 161 | 0 | ▶ | while (@relative_starts) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 0 | $end = shift @relative_ends; | |||||||||||||||||||||||||||
| 163 | 0 | $start = shift @relative_starts; | |||||||||||||||||||||||||||
| 164 | 0 | $output_text .= $encoder->encode( | |||||||||||||||||||||||||||
| 165 | bytes::substr( $text, $last_end, $start - $last_end ) ); | ||||||||||||||||||||||||||||
| 166 | 0 | $output_text | |||||||||||||||||||||||||||
| 167 | .= $formatter->highlight( | ||||||||||||||||||||||||||||
| 168 | $encoder->encode( bytes::substr( $text, $start, $end - $start ) ) | ||||||||||||||||||||||||||||
| 169 | ); | ||||||||||||||||||||||||||||
| 170 | 0 | $last_end = $end; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | 0 | $output_text .= $encoder->encode( bytes::substr( $text, $last_end ) ); | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 0 | return $output_text; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =for comment | ||||||||||||||||||||||||||||
| 178 | Find all points in the text where a relevant term begins and ends. For terms | ||||||||||||||||||||||||||||
| 179 | that are part of a phrase, only include points that are part of the phrase. | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =cut | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 1 | ▶ | sub _starts_and_ends { | ||||||||||||||||||||||||||
|
Subroutine
_starts_and_ends: called CC=19 SCAR=29.9
| |||||||||||||||||||||||||||||
| 184 | 1 | my ( $self, $field ) = @_; | |||||||||||||||||||||||||||
| 185 | 1 | my @posits; | |||||||||||||||||||||||||||
| 186 | my %done; | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 1 | TERM: for my $term ( @{ $self->{terms} } ) { | |||||||||||||||||||||||||||
| 189 | 4 | ▶ | if ( a_isa_b( $term, 'KinoSearch1::Index::Term' ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 190 | 2 | my $term_text = $term->get_text; | |||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 2 | ▶ | next TERM if $done{$term_text}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | 1 | $done{$term_text} = 1; | |||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | # add all starts and ends | ||||||||||||||||||||||||||||
| 196 | 1 | my $term_vector = $field->term_vector($term_text); | |||||||||||||||||||||||||||
| 197 | 1 | ▶ | next TERM unless defined $term_vector; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | 1 | my $starts = $term_vector->get_start_offsets; | |||||||||||||||||||||||||||
| 199 | 1 | my $ends = $term_vector->get_end_offsets; | |||||||||||||||||||||||||||
| 200 | 1 | ▶ | while (@$starts) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | 1 | push @posits, [ shift @$starts, shift @$ends, 1 ]; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | # intersect positions for phrase terms | ||||||||||||||||||||||||||||
| 205 | else { | ||||||||||||||||||||||||||||
| 206 | # if not a Term, it's an array of Terms representing a phrase | ||||||||||||||||||||||||||||
| 207 | 2 | my @term_texts = map { $_->get_text } @$term; | |||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 2 | my $phrase_text = join( ' ', @term_texts ); | |||||||||||||||||||||||||||
| 210 | 2 | ▶ | next TERM if $done{$phrase_text}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 211 | 1 | $done{$phrase_text} = 1; | |||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | 1 | my $posit_vec = KinoSearch1::Util::BitVector->new; | |||||||||||||||||||||||||||
| 214 | 1 | my @term_vectors = map { $field->term_vector($_) } @term_texts; | |||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | # make sure all terms are present | ||||||||||||||||||||||||||||
| 217 | 1 | ▶ | next TERM unless scalar @term_vectors == scalar @term_texts; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 1 | my $i = 0; | |||||||||||||||||||||||||||
| 220 | 1 | for my $tv (@term_vectors) { | |||||||||||||||||||||||||||
| 221 | # one term missing, ergo no phrase | ||||||||||||||||||||||||||||
| 222 | 3 | ▶ | next TERM unless defined $tv; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 223 | 3 | ▶ | if ( $i == 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 224 | 1 | $posit_vec->set( @{ $tv->get_positions } ); | |||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | else { | ||||||||||||||||||||||||||||
| 227 | # filter positions using logical "and" | ||||||||||||||||||||||||||||
| 228 | 2 | my $other_posit_vec = KinoSearch1::Util::BitVector->new; | |||||||||||||||||||||||||||
| 229 | $other_posit_vec->set( | ||||||||||||||||||||||||||||
| 230 | 2 | grep { $_ >= 0 } | |||||||||||||||||||||||||||
| 231 | 2 | map { $_ - $i } @{ $tv->get_positions } | |||||||||||||||||||||||||||
| 232 | ); | ||||||||||||||||||||||||||||
| 233 | 2 | $posit_vec->logical_and($other_posit_vec); | |||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||
| 235 | 3 | $i++; | |||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | # add only those starts/ends that belong to a valid position | ||||||||||||||||||||||||||||
| 239 | 1 | my $tv_start_positions = $term_vectors[0]->get_positions; | |||||||||||||||||||||||||||
| 240 | 1 | my $tv_starts = $term_vectors[0]->get_start_offsets; | |||||||||||||||||||||||||||
| 241 | 1 | my $tv_end_positions = $term_vectors[-1]->get_positions; | |||||||||||||||||||||||||||
| 242 | 1 | my $tv_ends = $term_vectors[-1]->get_end_offsets; | |||||||||||||||||||||||||||
| 243 | 1 | $i = 0; | |||||||||||||||||||||||||||
| 244 | 1 | my $j = 0; | |||||||||||||||||||||||||||
| 245 | 1 | my $last_token_index = $#term_vectors; | |||||||||||||||||||||||||||
| 246 | 1 | for my $valid_position ( @{ $posit_vec->to_arrayref } ) { | |||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | 1 | ▶ | while ( $i <= $#$tv_start_positions ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 249 | 1 | ▶ | last if ( $tv_start_positions->[$i] >= $valid_position ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 250 | 0 | $i++; | |||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||
| 252 | 1 | $valid_position += $last_token_index; | |||||||||||||||||||||||||||
| 253 | 1 | ▶ | while ( $j <= $#$tv_end_positions ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 254 | 1 | ▶ | last if ( $tv_end_positions->[$j] >= $valid_position ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 255 | 0 | $j++; | |||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||
| 257 | 1 | push @posits, | |||||||||||||||||||||||||||
| 258 | [ $tv_starts->[$i], $tv_ends->[$j], scalar @$term ]; | ||||||||||||||||||||||||||||
| 259 | 1 | $i++; | |||||||||||||||||||||||||||
| 260 | 1 | $j++; | |||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | # sort, uniquify and return | ||||||||||||||||||||||||||||
| 266 | 1 | ▶ | @posits = sort { $a->[0] <=> $b->[0] || $b->[1] <=> $a->[1] } @posits; | ||||||||||||||||||||||||||
|
Condition33%1/3
$KinoSearch1::Highlight::Highlighter::a->[0] <=> $KinoSearch1::Highlight::Highlighter::b->[0] || $KinoSearch1::Highlight::Highlighter::b->[1] <=> $KinoSearch1::Highlight::Highlighter::a->[1]
MC/DC0%0/2
$KinoSearch1::Highlight::Highlighter::a->[0] <=> $KinoSearch1::Highlight::Highlighter::b->[0] || $KinoSearch1::Highlight::Highlighter::b->[1] <=> $KinoSearch1::Highlight::Highlighter::a->[1]
$KinoSearch1::Highlight::Highlighter::a->[0] <=> $KinoSearch1::Highlight::Highlighter::b->[0]
$KinoSearch1::Highlight::Highlighter::b->[1] <=> $KinoSearch1::Highlight::Highlighter::a->[1]
Truth table33%1/3
A || B
A= $KinoSearch1::Highlight::Highlighter::a->[0] <=> $KinoSearch1::Highlight::Highlighter::b->[0]B= $KinoSearch1::Highlight::Highlighter::b->[1] <=> $KinoSearch1::Highlight::Highlighter::a->[1]
| |||||||||||||||||||||||||||||
| 267 | 1 | my @unique; | |||||||||||||||||||||||||||
| 268 | 1 | my $last = ~0; | |||||||||||||||||||||||||||
| 269 | 1 | for (@posits) { | |||||||||||||||||||||||||||
| 270 | 2 | ▶ | push @unique, $_ if $_->[0] != $last; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 271 | 2 | $last = $_->[0]; | |||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||
| 273 | 1 | return \@unique; | |||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | =for comment | ||||||||||||||||||||||||||||
| 277 | Select the byte address representing the greatest keyword density. Because | ||||||||||||||||||||||||||||
| 278 | the algorithm counts bytes rather than characters, it will degrade if the | ||||||||||||||||||||||||||||
| 279 | number of bytes per character is larger than 1. | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | =cut | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | 1 | ▶ | sub _calc_best_location { | ||||||||||||||||||||||||||
|
Subroutine
_calc_best_location: called CC=7 SCAR=23.0
| |||||||||||||||||||||||||||||
| 284 | 1 | my ( $self, $posits ) = @_; | |||||||||||||||||||||||||||
| 285 | 1 | my $window = $self->{limit} * 2; | |||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | # if there aren't any keywords, take the excerpt from the top of the text | ||||||||||||||||||||||||||||
| 288 | 1 | ▶ | return 0 unless @$posits; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | 1 | my %locations = map { ( $_->[0] => 0 ) } @$posits; | |||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | # if another keyword is in close proximity, add to the loc's score | ||||||||||||||||||||||||||||
| 293 | 1 | for my $loc_index ( 0 .. $#$posits ) { | |||||||||||||||||||||||||||
| 294 | # only score positions that are in range | ||||||||||||||||||||||||||||
| 295 | 2 | my $location = $posits->[$loc_index][0]; | |||||||||||||||||||||||||||
| 296 | 2 | my $other_loc_index = $loc_index - 1; | |||||||||||||||||||||||||||
| 297 | 2 | ▶ | while ( $other_loc_index > 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 298 | 0 | my $diff = $location - $posits->[$other_loc_index][0]; | |||||||||||||||||||||||||||
| 299 | 0 | ▶ | last if $diff > $window; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 300 | 0 | my $num_tokens_at_pos = $posits->[$other_loc_index][2]; | |||||||||||||||||||||||||||
| 301 | 0 | $locations{$location} | |||||||||||||||||||||||||||
| 302 | += ( 1 / ( 1 + log($diff) ) ) * $num_tokens_at_pos; | ||||||||||||||||||||||||||||
| 303 | 0 | --$other_loc_index; | |||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||
| 305 | 2 | $other_loc_index = $loc_index + 1; | |||||||||||||||||||||||||||
| 306 | 2 | ▶ | while ( $other_loc_index <= $#$posits ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 307 | 1 | my $diff = $posits->[$other_loc_index] - $location; | |||||||||||||||||||||||||||
| 308 | 1 | ▶ | last if $diff > $window; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 309 | 0 | my $num_tokens_at_pos = $posits->[$other_loc_index][2]; | |||||||||||||||||||||||||||
| 310 | 0 | $locations{$location} | |||||||||||||||||||||||||||
| 311 | += ( 1 / ( 1 + log($diff) ) ) * $num_tokens_at_pos; | ||||||||||||||||||||||||||||
| 312 | 0 | ++$other_loc_index; | |||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | # return the highest scoring position | ||||||||||||||||||||||||||||
| 317 | 1 | return ( sort { $locations{$b} <=> $locations{$a} } keys %locations )[0]; | |||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | 18 | 1; | |||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | __END__ | ||||||||||||||||||||||||||||