12 |
|
|
13 |
sub get_snippet { |
sub get_snippet { |
14 |
my $context_chars = 100; |
my $context_chars = 100; |
|
my $max_desc = 16384; # 16k to filter |
|
15 |
|
|
16 |
my $desc = shift || return ''; |
my $desc = shift || return ''; |
|
$desc = substr($desc,0,$max_desc) if (length($desc) > $max_desc); |
|
17 |
$desc = e($desc); |
$desc = e($desc); |
18 |
|
|
19 |
# test if $desc contains any of our query words |
# test if $desc contains any of our query words |
20 |
my @snips; |
my @snips; |
21 |
|
|
22 |
my @colors = qw{#ffff66 #a0ffff #99ff99 #ff9999 #ff66ff}; |
my @colors = qw{#ffff66 #a0ffff #99ff99 #ff9999 #ff66ff}; |
23 |
|
|
24 |
my $i = 0; |
# construct regex |
25 |
|
my $re = qq/^.*?(.{$context_chars}?)(\Q/ . join("|",@_) . |
26 |
|
qq/\E)(.{$context_chars})/; |
27 |
|
|
28 |
for my $q (@_) { |
while ($desc =~ s/$re//si) { |
29 |
if ($desc =~ m/(.*?)(\Q$q\E)(.*)/si) { |
my ($bef,$qm,$af) = ($1, $2, $3); |
|
my $bef = $1; |
|
|
my $qm = $2; |
|
|
my $af = $3; |
|
|
$bef = substr $bef, -$context_chars; |
|
|
$af = substr $af, 0, $context_chars; |
|
30 |
|
|
31 |
# no partial words... |
# no partial words... |
32 |
$af =~ s,^\S+\s+|\s+\S+$,,gs; |
$bef =~ s,^\S+\s+|\s+\S+$,,gs; |
33 |
$bef =~ s,^\S+\s+|\s+\S+$,,gs; |
$af =~ s,^\S+\s+|\s+\S+$,,gs; |
34 |
|
|
35 |
push(@snips, "$bef <span style=\"background:".$colors[$i]."; color:black;\">$qm</span> $af"); |
push @snips, "$bef $qm $af"; |
|
$i++; |
|
|
$i = 0 if ($i > $#colors); |
|
|
} |
|
36 |
} |
} |
37 |
|
|
38 |
my $ellip = ' ... '; |
my $ellip = ' ... '; |
39 |
my $snippet = $ellip. join($ellip, @snips) . $ellip if (@snips); |
my $snippet; |
40 |
|
|
41 |
|
if (@snips) { |
42 |
|
$snippet = $ellip. join($ellip, @snips) . $ellip; |
43 |
|
} else { |
44 |
|
return ''; |
45 |
|
} |
46 |
|
|
47 |
|
# color offset |
48 |
|
my $i = 0; |
49 |
|
|
50 |
|
foreach my $w (@_) { |
51 |
|
$snippet =~ s,(\Q$w\E),<span style="background: $colors[$i]; color:black;">$1</span>,gsi; |
52 |
|
$i++; |
53 |
|
$i = 0 if ($i > $#colors); |
54 |
|
} |
55 |
|
|
56 |
return $snippet; |
return $snippet; |
57 |
} |
} |
58 |
|
|
118 |
my %path_label; |
my %path_label; |
119 |
my @path_name; |
my @path_name; |
120 |
foreach (@{$config->{paths}->{path}}) { |
foreach (@{$config->{paths}->{path}}) { |
|
|
|
|
print STDERR "##: $_->{limit}",x($_->{content}),"\n"; |
|
121 |
push @path_name,x($_->{limit}); |
push @path_name,x($_->{limit}); |
122 |
$path_label{$_->{limit}} = x($_->{content}); |
$path_label{$_->{limit}} = x($_->{content}); |
123 |
} |
} |
150 |
my $s; |
my $s; |
151 |
# re-write query from +/- to and/and not |
# re-write query from +/- to and/and not |
152 |
|
|
153 |
|
my @s_elem; |
154 |
|
|
155 |
my $search = param('search'); |
my $search = param('search'); |
156 |
my $s_phrase = ""; |
|
157 |
|
# strip spaces |
158 |
|
$search =~ s/^\s+//; |
159 |
|
$search =~ s/\s+$//; |
160 |
|
# fixup search string |
161 |
|
$search=~tr/šðžèæŠÐŽÈÆ/¹ð¾èæ©Ð®ÈÆ/; # 1250 -> iso8859-2 |
162 |
|
$search=~tr/¹©ðÐèÈæƾ®/sSdDcCcCzZ/; |
163 |
|
|
164 |
|
# extract phrases and put them first |
165 |
while ($search =~ s/\s*("[^"]+")\s*/ /) { |
while ($search =~ s/\s*("[^"]+")\s*/ /) { |
166 |
$s .= "$1 "; |
$s .= "$1 "; |
167 |
|
push @s_elem, $1; |
168 |
} |
} |
|
$search =~ s/^\s+//; |
|
|
$search =~ s/\s+$//; |
|
169 |
|
|
170 |
my %words; |
my %words; |
171 |
|
|
184 |
} else { |
} else { |
185 |
$s.="$2* "; |
$s.="$2* "; |
186 |
} |
} |
187 |
|
push @s_elem, $2; |
188 |
} else { |
} else { |
189 |
if (@spellings && !param('no_affix')) { |
if (@spellings && !param('no_affix')) { |
190 |
my $w = $_; $w =~ s/[\*\s]+//g; |
my $w = $_; $w =~ s/[\*\s]+//g; |
|
#$s.="(".join("* or ",$spelling_alt->alternatives($w))."*) "; |
|
191 |
my $or=""; |
my $or=""; |
192 |
foreach my $spelling_alt (@spellings) { |
foreach my $spelling_alt (@spellings) { |
193 |
$s.="$or(".join("* or ",$spelling_alt->alternatives($w))."*) "; |
$s.="$or(".join("* or ",$spelling_alt->alternatives($w))."*) "; |
196 |
} else { |
} else { |
197 |
$s.="$_* "; |
$s.="$_* "; |
198 |
} |
} |
199 |
|
push @s_elem, $_; |
200 |
} |
} |
201 |
} |
} |
202 |
|
|
203 |
# fixup search string |
# fix multiple stars |
|
$s=~tr/šðžèæŠÐŽÈÆ/¹ð¾èæ©Ð®ÈÆ/; # 1250 -> iso8859-2 |
|
|
$s=~tr/¹©ðÐèÈæƾ®/sSdDcCcCzZ/; |
|
204 |
$s=~s/\*\*+/*/g; |
$s=~s/\*\*+/*/g; |
205 |
|
|
206 |
# limit to some path |
# limit to some path |
274 |
if ($prop =~ m/swishdescription/) { |
if ($prop =~ m/swishdescription/) { |
275 |
my $tmp = get_snippet( |
my $tmp = get_snippet( |
276 |
$result->Property($prop), |
$result->Property($prop), |
277 |
split(/\s+/,$search) |
@s_elem, |
278 |
); |
); |
279 |
|
|
280 |
push @arr, $tmp; |
push @arr, $tmp; |