--- trunk/search/nn-swish.cgi 2005/02/22 14:46:28 101 +++ trunk/search/nn-swish.cgi 2008/08/04 15:28:56 106 @@ -3,19 +3,17 @@ use strict; use CGI qw/:standard -no_xhtml/; use CGI::Carp qw(fatalsToBrowser); -use SWISH; -use Unicode::String qw(utf8 utf16); +use SWISH::API; use Lingua::Spelling::Alternative; -require Unicode::Map8; +use Encode qw/from_to/; use GDBM_File; use lib '/data/swish/html'; use FormatResult; my $dir='/home/dpavlin/nn'; -my $prog='/usr/bin/swish-e'; +#my $prog='/usr/bin/swish-e'; my $url='http://www.nn.hr/clanci/sluzbeno/'; -my $hits=0; my $max_hits=100; my %labels = (100=>' 100', 200=>' 200', 500=>' 500', 0=>'neogranièeno'); @@ -66,87 +64,79 @@ $s=~tr/šðžèæŠÐŽÈÆ/¹ð¾èæ©Ð®ÈÆ/; # 1250 -> iso8859-2 $s=~tr/¹©ðÐèÈæƾ®/sSdDcCcCzZ/; - my $l2_map = Unicode::Map8->new("ISO-8859-2") || die; - my $us = Unicode::String->new(); - - my $sw_q; - my $sh; + my $swish_query; if (defined(param('max_hits'))) { $max_hits = param('max_hits'); } - if (param('index') eq 'title') { - - $sh = SWISH->connect('Fork', - prog => $prog, - indexes => "$dir/swish/nn.index", - properties => [qw/god br nr/], - results => sub { - my ($sh,$hit) = @_; - - $us->utf8($hit->swishtitle); + my ( $index_path, $result_coderef ); - my $naslov = $l2_map->to8($us->utf16); - $naslov = FormatResult::highlite_words(\$naslov, \@hl_words); - print "swishdocpath,"\">NN",$hit->god,"/",$hit->br," ",$hit->nr," $naslov [",$hit->swishrank,"]
\n"; - # print $_[1]->as_string,"
\n"; - # my @fields = $hit->field_names; - # print "Field '$_' = '", $hit->$_, "'
\n" for sort @fields; - }, - maxhits => $max_hits, - ); + if (param('index') eq 'title') { - die $SWISH::errstr unless $sh; + $result_coderef = sub { + my $hit = shift || die; + my $naslov = $hit->property('swishtitle'); + from_to($naslov, 'utf-8', 'iso-8859-2'); + $naslov = FormatResult::highlite_words(\$naslov, \@hl_words); + print "property('swishdocpath'),"\">NN",$hit->property('god'),"/",$hit->property('br')," ",$hit->property('nr')," $naslov [",$hit->property('swishrank'),"]
\n"; + }; - $sw_q = "naslov_czs=($s)"; + $index_path = "$dir/swish/nn.index"; + $swish_query = "naslov_czs=($s)"; if (param('god_limit')) { - $sw_q .= " and god=".int(param('god')) if (param('god')); + $swish_query .= " and god=".int(param('god')) if (param('god')); } } else { # search in full text - $sh = SWISH->connect('Fork', - prog => $prog, - indexes => "$dir/swish/sluzbeno.index", - results => sub { - my ($sh,$hit) = @_; - - my $path = $hit->swishdocpath; - if ($file2title{$path}) { - my ($god,$br,$nr,undef,$naslov) = split(/ /,$file2title{$path},5); - $naslov = FormatResult::highlite_words(\$naslov, \@hl_words); - print "NN$god/$br $nr $naslov [",$hit->swishrank,"]
\n"; - - } else { - print "\n"; - } - - }, - maxhits => $max_hits, - ); + $result_coderef = sub { + my $hit = shift || die; - die $SWISH::errstr unless $sh; + my $path = $hit->property('swishdocpath'); + if ($file2title{$path}) { + my ($god,$br,$nr,undef,$naslov) = split(/ /,$file2title{$path},5); + $naslov = FormatResult::highlite_words(\$naslov, \@hl_words); + from_to($naslov, 'utf-8', 'iso-8859-2'); + print "NN$god/$br $nr $naslov [",$hit->property('swishrank'),"]
\n"; - $sw_q = $s; + } else { + print "\n"; + } + + }; + + $index_path = "$dir/swish/sluzbeno.index"; + $swish_query = $s; if (param('god_limit')) { - $sw_q .= " and swishdocpath=".int(param('god')) if (param('god')); + $swish_query .= " and swishdocpath=".int(param('god')) if (param('god')); } } - print ""; + print ""; + + my $swish = SWISH::API->new( $index_path ); + $swish->abort_last_error if $swish->Error; - $hits = $sh->query($sw_q); + my $results = $swish->query($swish_query); + + my $hits = $results->hits; if ($hits > 0) { - print p,hr,"Prikazujem $hits zakona"; + my $i = 1; + while ( my $hit = $results->next_result ) { + $result_coderef->( $hit ); + last if $i++ == $max_hits; + } + + print p,hr,"Pronaðeno $hits zakona"; print " iz godine ",param('god') if (param('god_limit')); - print " (maks. $max_hits)... " if ($max_hits); + print " (prikazano prvih $max_hits)... " if ($max_hits); print " [$s]"; } else { - print p,"Nije naðen niti jedan zakon... [$s, ",$sh->errstr,"]"; + print p,"Nije naðen niti jedan zakon... [$s, ",$swish->error_string,"]"; } } else { my $dir=$0;