8 |
use Lingua::Spelling::Alternative; |
use Lingua::Spelling::Alternative; |
9 |
use Text::Iconv; |
use Text::Iconv; |
10 |
use Data::Pageset; |
use Data::Pageset; |
11 |
|
use FormatResult; |
|
sub get_snippet { |
|
|
|
|
|
# maximum length of context in characters |
|
|
my $cc = 50; |
|
|
|
|
|
my $desc = shift || return ''; |
|
|
$desc = e($desc); |
|
|
|
|
|
# sort words from longer to shorter (for hilighting later) |
|
|
my @words = sort { length($b) <=> length($a) } @_; |
|
|
|
|
|
# colors to highlite |
|
|
my @colors = qw{#ffff66 #a0ffff #99ff99 #ff9999 #ff66ff}; |
|
|
|
|
|
# construct regex |
|
|
my $re = qq/^(.*?\\b)(/ . join('|', @words) . qq/)/; |
|
|
|
|
|
my $ellip = ' ... '; |
|
|
my $snippet = ''; |
|
|
|
|
|
#print "<ul>"; |
|
|
|
|
|
while ($desc =~ s/$re//si) { |
|
|
my ($foo, $match) = ($1,$2); |
|
|
|
|
|
#print "<br>desc: <small>$desc</small>\n"; |
|
|
#print "<br>foo: <small>$foo<b>$match</b></small>\n"; |
|
|
|
|
|
if (length($foo) < $cc * 2) { |
|
|
$snippet .= $foo . $match; |
|
|
} else { |
|
|
|
|
|
if ($foo =~ m/^(.{0,$cc})(\s.*?\s|\s|)?(.{0,$cc})$/) { |
|
|
|
|
|
# print "<li><small>$snippet</small><br> |
|
|
# ",length($1),": <i>$1</i><br> |
|
|
# ",length($2),": <span style=\"color:grey\">$2</span><br> |
|
|
# ",length($3),": <i>$3</i><br> |
|
|
# <b>$match</b>\n"; |
|
|
|
|
|
if ($snippet) { |
|
|
$snippet .= $1 . $ellip . $3 . $match; |
|
|
} else { |
|
|
$snippet = $ellip . $3 . $match ; |
|
|
} |
|
|
|
|
|
} else { |
|
|
# print "<li> <big>SKIP</big> $foo\n"; |
|
|
print STDERR "this shouldn't happen!\n"; |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
#print "</ul>"; |
|
|
|
|
|
# color offset |
|
|
my $i = 0; |
|
|
|
|
|
foreach my $w (@words) { |
|
|
$snippet =~ s,(\b\Q$w\E),<span style="background: $colors[$i]; color:black;">$1</span>,gsi; |
|
|
$i++; |
|
|
$i = 0 if ($i > $#colors); |
|
|
} |
|
|
|
|
|
$snippet .= $ellip if ($snippet); |
|
|
|
|
|
return $snippet; |
|
|
} |
|
12 |
|
|
13 |
# for pager |
# for pager |
14 |
my $pages_per_set = 20; |
my $pages_per_set = 20; |
104 |
my $s; |
my $s; |
105 |
# re-write query from +/- to and/and not |
# re-write query from +/- to and/and not |
106 |
|
|
107 |
my @s_elem; |
my @s_highlite; |
108 |
|
|
109 |
my $search = param('search'); |
my $search = param('search'); |
110 |
|
|
118 |
# extract phrases and put them first |
# extract phrases and put them first |
119 |
while ($search =~ s/\s*("[^"]+")\s*/ /) { |
while ($search =~ s/\s*("[^"]+")\s*/ /) { |
120 |
$s .= "$1 "; |
$s .= "$1 "; |
121 |
push @s_elem, $1; |
push @s_highlite, $1; |
122 |
} |
} |
123 |
|
|
124 |
my %words; |
my %words; |
138 |
} else { |
} else { |
139 |
$s.="$2* "; |
$s.="$2* "; |
140 |
} |
} |
141 |
push @s_elem, $2; |
push @s_highlite, $2 if ($1 ne "-"); |
142 |
} else { |
} else { |
143 |
if (@spellings && !param('no_affix')) { |
if (@spellings && !param('no_affix')) { |
144 |
my $w = $_; $w =~ s/[\*\s]+//g; |
my $w = $_; $w =~ s/[\*\s]+//g; |
150 |
} else { |
} else { |
151 |
$s.="$_* "; |
$s.="$_* "; |
152 |
} |
} |
153 |
push @s_elem, $_; |
push @s_highlite, $_; |
154 |
} |
} |
155 |
} |
} |
156 |
|
|
226 |
|
|
227 |
foreach my $prop (@properties) { |
foreach my $prop (@properties) { |
228 |
if ($prop =~ m/swishdescription/) { |
if ($prop =~ m/swishdescription/) { |
229 |
my $tmp = get_snippet( |
my $tmp = FormatResult::get_snippet( |
230 |
$result->Property($prop), |
e($result->Property($prop)), |
231 |
@s_elem, |
@s_highlite, |
232 |
); |
); |
233 |
|
|
234 |
push @arr, $tmp; |
push @arr, $tmp; |