/[nn]/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

Contents of /search/nn-swish.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.15 - (show annotations)
Mon Sep 29 09:39:33 2003 UTC (20 years, 6 months ago) by dpavlin
Branch: MAIN
CVS Tags: HEAD
Changes since 1.14: +16 -11 lines
clean-up, javascript auto select of year selection, moved help to separate file

1 #!/usr/bin/perl -w
2
3 use strict;
4 use CGI qw/:standard -no_xhtml/;
5 use CGI::Carp qw(fatalsToBrowser);
6 use SWISH;
7 use Unicode::String qw(utf8 utf16);
8 use Lingua::Spelling::Alternative;
9 require Unicode::Map8;
10 use GDBM_File;
11
12 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;
17 my $max_hits=100;
18
19 my %labels = (100=>' 100', 200=>' 200', 500=>' 500', 0=>'neogranièeno');
20 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');
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;
43
44 if (param('search')) {
45
46 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
58 $s=~tr/¹©ðÐèÈæƾ®/sSdDcCcCzZ/;
59
60 my $l2_map = Unicode::Map8->new("ISO-8859-2") || die;
61 my $us = Unicode::String->new();
62
63 my $sw_q;
64 my $sh;
65
66 if (param('index') eq 'title') {
67
68 $sh = SWISH->connect('Fork',
69 prog => $prog,
70 indexes => "$dir/swish/nn.index",
71 properties => [qw/god br nr/],
72 results => sub {
73 my ($sh,$hit) = @_;
74
75 $us->utf8($hit->swishtitle);
76
77 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";
80 # my @fields = $hit->field_names;
81 # print "Field '$_' = '", $hit->$_, "'<br>\n" for sort @fields;
82 },
83 maxhits => param('max_hits') || $max_hits,
84 );
85
86 die $SWISH::errstr unless $sh;
87
88 $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) {
129 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 {
133 print p,"Nije naðen niti jedan zakon... <small>($s, ",$sh->errstr,")</small>";
134 }
135 } else {
136 my $dir=$0;
137 $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;

  ViewVC Help
Powered by ViewVC 1.1.26