/[nn.old]/trunk/search/nn-swish.cgi
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/search/nn-swish.cgi

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 73 by dpavlin, Thu Jun 20 07:57:05 2002 UTC revision 95 by dpavlin, Mon Sep 29 09:39:33 2003 UTC
# Line 2  Line 2 
2    
3  use strict;  use strict;
4  use CGI qw/:standard -no_xhtml/;  use CGI qw/:standard -no_xhtml/;
 use Text::Query;  
5  use CGI::Carp qw(fatalsToBrowser);  use CGI::Carp qw(fatalsToBrowser);
6  use SWISH;  use SWISH;
7    use Unicode::String qw(utf8 utf16);
8    use Lingua::Spelling::Alternative;
9  require Unicode::Map8;  require Unicode::Map8;
10    use GDBM_File;
11    
12  my $dir='/home/dpavlin/nn/swish';  my $dir='/home/dpavlin/nn';
13    my $prog='/usr/bin/swish-e';
14    my $url='http://www.nn.hr/clanci/sluzbeno/';
15    
16  my $hits=0;  my $hits=0;
17    my $max_hits=100;
18    
19  print header(-charset=>'iso-8859-2'),start_html(-title=>'NN pretrazivanje',-lang=>'hr'),start_form;  my %labels = (100=>' 100', 200=>' 200', 500=>' 500', 0=>'neograničeno');
20  print "Potra¾i zakone sa riječima: ",textfield('search');  my %index = ('title'=>'naslovu', 'text'=>'tekstu');
21    
22    my %god_lables;
23    
24    my %brzakona;
25    tie %brzakona, 'GDBM_File', "$dir/swish/brzakona.gdbm", &GDBM_READER, 0640 || die "tie: $!";
26    foreach (sort keys %brzakona) {
27            $god_lables{$_} = sprintf("%-8s (%d zakona)",$_,$brzakona{$_});
28    }
29    untie %brzakona;
30    
31    my %file2title;
32    tie %file2title, 'GDBM_File', "$dir/swish/file2title.gdbm", &GDBM_READER, 0640 || die "tie: $!";
33    
34    my $hr = new Lingua::Spelling::Alternative( DEBUG => 0 );
35    $hr->load_findaffix("$dir/prvih_50.txt");
36    
37    print header(-charset=>'iso-8859-2'),start_html(-title=>'Narodne Novine pretrazivanje',-lang=>'hr'),start_form(-name=>'search_form');
38    print "Potra¾i ",popup_menu(-name=>'max_hits',-values=>[ sort keys %labels ],-labels=>\%labels,-default=>$max_hits)," zakona sa riječima: ",textfield('search');
39    print " u ",popup_menu(-name=>'index',-values=>[ sort keys %index ],-labels=>\%index,-default=>'title')," zakona ";
40  print submit(-value=>'prika¾i');  print submit(-value=>'prika¾i');
41    print br,checkbox(-name=>'god_limit', -checked=>0, -label=>"ograniči pretra¾ivanje samo na godinu "),popup_menu(-name=>'god',-values=>[sort keys %god_lables],-labels=>\%god_lables,-onChange=>'this.form.god_limit.checked=true');
42  print end_form,hr;  print end_form,hr;
43    
44  if (param('search')) {  if (param('search')) {
45    
46          my $s=param('search');          my $s;
47            # re-write query from +/- to and/and not
48            foreach (split(/\s+/,param('search'))) {
49                    if (m/^([+-])(\S+)/) {
50                            $s.= ($s) ? "and " : "";
51                            $s.="not " if ($1 eq "-");
52                            $s.="(".join("* or ",$hr->alternatives($2)).") ";
53                    } else {
54                            $s .= "(".join("* or ",$hr->alternatives($_)).") ";
55                    }
56            }
57          $s=~tr/ššžčꊩŽČĘ/¹š¾čę©Š®ČĘ/;  # 1250 -> iso8859-2          $s=~tr/ššžčꊩŽČĘ/¹š¾čę©Š®ČĘ/;  # 1250 -> iso8859-2
58          $s=~tr/¹©šŠčČęĘ¾®/sSdDcCcCzZ/;          $s=~tr/¹©šŠčČęĘ¾®/sSdDcCcCzZ/;
59    
60          my $l2_map = Unicode::Map8->new("ISO-8859-2") || die;          my $l2_map = Unicode::Map8->new("ISO-8859-2") || die;
61          #my $utf8_map = Unicode::Map8->new("utf8") || die;          my $us = Unicode::String->new();
62    
63            my $sw_q;
64            my $sh;
65    
66            if (param('index') eq 'title') {
67    
68          my $sh = SWISH->connect('Fork',                  $sh = SWISH->connect('Fork',
69                  prog     => "$dir/swish-e",                          prog     => $prog,
70                  indexes  => "$dir/nn.index",                          indexes  => "$dir/swish/nn.index",
71                  properties  => [qw/god br nr/],                          properties  => [qw/god br nr/],
72                  results  => sub {                          results  => sub {
73                          my ($sh,$hit) = @_;                                  my ($sh,$hit) = @_;
74    
75                          my $us=$hit->swishtitle;                                  $us->utf8($hit->swishtitle);
76    
77                          print "<a href=\"",$hit->swishdocpath,"\"><tt>NN",$hit->god,"/",$hit->br,"</a> ",$hit->nr," </tt>",$hit->swishtitle,"</a> [",$hit->swishrank,"]<br>\n";                                  print "<tt><a href=\"$url",$hit->swishdocpath,"\">NN",$hit->god,"/",$hit->br,"</a> ",$hit->nr," </tt>",$l2_map->to8($us->utf16)," [",$hit->swishrank,"]<br>\n";
78    
79  #                       print $_[1]->as_string,"<br>\n";          #                       print $_[1]->as_string,"<br>\n";
80  #                       my @fields = $hit->field_names;          #                       my @fields = $hit->field_names;
81  #                       print "Field '$_' = '", $hit->$_, "'<br>\n" for sort @fields;          #                       print "Field '$_' = '", $hit->$_, "'<br>\n" for sort @fields;
82                  },                          },
83          );                          maxhits => param('max_hits') || $max_hits,
84                    );
85    
86          die $SWISH::errstr unless $sh;                  die $SWISH::errstr unless $sh;
87    
88          $hits = $sh->query("naslov_czs=($s)");                  $sw_q = "naslov_czs=($s)";
89                    if (param('god_limit')) {
90                            $sw_q .= " and god=".int(param('god')) if (param('god'));
91                    }
92    
93            } else {
94                    # search in full text
95    
96                    $sh = SWISH->connect('Fork',
97                            prog     => $prog,
98                            indexes  => "$dir/swish/sluzbeno.index",
99                            results  => sub {
100                                    my ($sh,$hit) = @_;
101    
102                                    my $path = $hit->swishdocpath;
103                                    if ($file2title{$path}) {
104                                            my ($god,$br,$nr,undef,$naslov) = split(/ /,$file2title{$path},5);
105                                            print "<tt><a href=\"$url",$path,"\">NN$god/$br</a> $nr</tt> $naslov [",$hit->swishrank,"]<br>\n";
106    
107                                    } else {
108                                            print "<!-- error! can't find $path -->\n";
109                                    }
110    
111                            },
112                            maxhits => param('max_hits') || $max_hits,
113                    );
114    
115                    die $SWISH::errstr unless $sh;
116    
117                    $sw_q = $s;
118                    if (param('god_limit')) {
119                            $sw_q .= " and swishdocpath=".int(param('god')) if (param('god'));
120                    }
121    
122            }
123    
124            print "<!-- swish query: $sw_q -->";
125    
126            $hits = $sh->query($sw_q);
127    
128          if ($hits > 0) {          if ($hits > 0) {
129                  print p,hr,"Našeno je $hits zakona...";                  print p,hr,"Prikazujem $hits zakona";
130                    print " iz godine ",param('god') if (param('god_limit'));
131                    print " (maks. ",param('max_hits') || $max_hits,")... <small>($s)</small>";
132          } else {          } else {
133                  print p,"Nije našen niti jedan zakon... (",$sh->errstr,")";                  print p,"Nije našen niti jedan zakon... <small>($s, ",$sh->errstr,")</small>";
134          }          }
135  } else {  } else {
136          print p('Kod pretra¾ivanja pretra¾ivač pronalazi sve zakone u kojima se pojavljuju <b>sve upisanje riječi</b>.',br,'Ako ispred riječi upi¹ete minus (-) neęe se prikazivati zakoni koji imaju takvu riječ. Npr. <tt>+kava +zakon -dopunama</tt>');          my $dir=$0;
137          print p("Mo¾ete pročitati i <a href=\"http://www.rot13.org/~dpavlin/nn.html\">članak</a> o tome kako je ovaj pretra¾ivač napravljen i za¹to.");          $dir=~s,(^.*?)/[^/]+$,$1,g;
138            open(HELP, "$dir/nn-help.html") || die "can't open '$dir/nn-help.html'";
139            while(<HELP>) {
140                    print;
141            }
142            close(HELP);
143  }  }
144    
145    untie %file2title;

Legend:
Removed from v.73  
changed lines
  Added in v.95

  ViewVC Help
Powered by ViewVC 1.1.26