/[webpac]/trunk2/all2all.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /trunk2/all2all.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 562 - (hide annotations)
Sat Oct 30 23:56:57 2004 UTC (15 years ago) by dpavlin
File MIME type: text/plain
File size: 9780 byte(s)
fix patch to icon image

1 dpavlin 1 #!/usr/bin/perl -w
2    
3 dpavlin 348 =head1 NAME
4    
5 dpavlin 354 all2all.pl - basic script for all WebPAC needs
6 dpavlin 348
7     =cut
8    
9 dpavlin 1 use strict;
10 dpavlin 490 use locale;
11 dpavlin 454 use YAML;
12 dpavlin 352 use Carp;
13 dpavlin 437 use Getopt::Long;
14 dpavlin 546 use Text::Unaccent 1.02;
15 dpavlin 1
16 dpavlin 352 use lib './lib';
17 dpavlin 354 use WebPAC;
18 dpavlin 390 use WebPAC::jsFind;
19 dpavlin 410 use WebPAC::Index;
20 dpavlin 441 use WebPAC::Tree;
21 dpavlin 348
22 dpavlin 437 # options which can be changed via command line
23     #
24     my $code_page = 'ISO-8859-2';
25     my ($limit_mfn, $start_mfn, $debug, $low_mem);
26     my $index_path = './out/index';
27    
28     my $result = GetOptions(
29     "code_page=s" => \$code_page,
30     "limit_mfn=i" => \$limit_mfn,
31     "start_mfn=i" => \$start_mfn,
32     "debug!" => \$debug,
33     "low_mem!" => \$low_mem,
34     );
35    
36 dpavlin 500 my $filter = {
37     'CROVOC' => sub {
38     my $tmp = shift || return;
39 dpavlin 560 return undef unless ($tmp =~ s/\s*CROVOC.*$/ #C#/);
40     # remove repeating stars
41     # FIXME this should be fixed at right place, not here!
42     $tmp =~ s/(\s*#C#)+/ #C#/g;
43 dpavlin 500 return $tmp;
44     },
45 dpavlin 501 'CROVOC_tree' => sub {
46     my $tmp = shift || return;
47 dpavlin 561 $tmp =~ s/\s*CROVOC.*$/ <img src="img\/crovoc.png" border="0">/;
48 dpavlin 503 $tmp =~ s/\s*EUROVOC.*//;
49 dpavlin 501 return $tmp;
50     },
51 dpavlin 560 # TT filter
52     'CROVOC_img' => sub {
53     my $tmp = shift;
54     $tmp =~ s/\s*#C#\s*/ <img src="..\/img\/crovoc.png" border="0">/gis;
55 dpavlin 562 $tmp =~ s/"img\/crovoc.png"/"..\/img\/crovoc.png"/gis;
56 dpavlin 560 return $tmp;
57     }
58 dpavlin 500 };
59    
60 dpavlin 546 ## remove accented characters
61     #
62     sub unac {
63     my $string = shift || return;
64     $string = unac_string($code_page,$string);
65     $string =~ tr/ðÐ/dD/;
66 dpavlin 550 $string = unac_entities($string);
67 dpavlin 546 return $string;
68     }
69     sub unac_2 {
70     my $string = shift || return;
71     if (length($string) > 2) {
72     my $pr = substr($string,0,2);
73     $string = unac_string($code_page,substr($string,2));
74     $string =~ tr/ðÐ/dD/;
75     $string = $pr . $string;
76     }
77 dpavlin 550 $string = unac_entities($string);
78 dpavlin 546 return lc($string);
79     }
80    
81 dpavlin 550 sub unac_entities {
82     my $ent = shift || return;
83    
84     $ent =~ s/&(\w)(acute|cedil|circ|grave|ring|slash|tilde|uml);/$1/gi;
85     $ent =~ s/&eth;/d/g;
86     $ent =~ s/&E[tT][hH];/D/g;
87     $ent =~ s/&(\w\w)lig;/$1/gi;
88    
89     return $ent;
90     }
91    
92 dpavlin 437 # create WebPAC object
93     #
94 dpavlin 354 my $webpac = new WebPAC(
95 dpavlin 437 code_page => $code_page,
96     limit_mfn => $limit_mfn,
97     start_mfn => $start_mfn,
98     debug => $debug,
99     low_mem => $low_mem,
100 dpavlin 500 filter => $filter,
101 dpavlin 352 ) || die;
102    
103 dpavlin 421 my $log = $webpac->_get_logger() || die "can't get logger";
104 dpavlin 373
105 dpavlin 421 $log->debug("creating WebPAC::jsFind object");
106    
107 dpavlin 390 my $index = new WebPAC::jsFind(
108 dpavlin 437 index_path => $index_path,
109 dpavlin 560 keys => 62,
110 dpavlin 390 ) || die;
111    
112 dpavlin 410 my $thes;
113    
114 dpavlin 10 $|=1;
115 dpavlin 9
116 dpavlin 352 my $maxmfn = $webpac->open_isis(
117 dpavlin 357 filename => shift @ARGV || '/data/hidra/THS/THS',
118 dpavlin 352 lookup => [
119 dpavlin 501 { 'key' => 'd:v900', 'val' => 'filter{CROVOC_tree}v250^a v800' },
120 dpavlin 352 # { 'eval' => '"v901^a" eq "Podruèje"', 'key' => 'pa:v561^4:v562^4:v461^1', 'val' => 'v900' },
121     # { 'eval '=> '"v901^a" eq "Mikrotezaurus"', 'key' => 'a:v561^4:v562^4:v461^1', 'val' => 'v900' },
122     # { 'eval' => '"v901^a" eq "Deskriptor"', 'key' => 'a:v561^4:v562^4:v461^1', 'val' => 'v900' },
123     { 'key' => 'a:v561^4:v562^4:v461^1', 'val' => 'v900' },
124 dpavlin 374 { 'key' => '900_mfn:v900', 'val' => 'v000' },
125 dpavlin 419 # tree structure
126 dpavlin 471 { 'eval' => 'length("v251") == 2 && "v800" =~ m/EUROVOC/ || "v800" =~ m/CROVOC/ && "v251" =~ m/^(H|HD|L|Z|P)$/', 'key' => 'root:v251', 'val' => 'v900' },
127 dpavlin 419 { 'eval' => '"v251"', 'key' => 'code:v900', 'val' => 'v561^4:v251' },
128 dpavlin 460 { 'eval' => '"v561^4" && "v562^4"', 'key' => 'code:v900', 'val' => 'v561^4:v562^4' },
129 dpavlin 352 ],
130 dpavlin 54 );
131 dpavlin 3
132 dpavlin 421 $log->debug("isis file ",$webpac->{'isis_filename'}," opened");
133    
134 dpavlin 372 $log->info("rows: $maxmfn");
135 dpavlin 170
136 dpavlin 363 $webpac->open_import_xml(type => 'isis_hidra_ths');
137    
138 dpavlin 425 if(1) { # XXX
139 dpavlin 424
140 dpavlin 362 while (my $rec = $webpac->fetch_rec) {
141 dpavlin 358
142 dpavlin 370 my @ds = $webpac->data_structure($rec);
143 dpavlin 366
144 dpavlin 411 if (0 && $log->is_debug) {
145 dpavlin 454 $log->debug("rec = ",Dump($rec));
146     $log->debug("ds = ",Dump(\@ds));
147 dpavlin 372 }
148    
149 dpavlin 374 next if (! @ds);
150 dpavlin 366
151 dpavlin 421 my $filename = $webpac->{'current_filename'} || $log->logdie("no current_filename in webpac object");
152 dpavlin 374
153     if ($filename) {
154 dpavlin 411 $webpac->output_file(
155     file => $filename,
156 dpavlin 374 template => 'html.tt',
157     data => \@ds,
158 dpavlin 398 headline => $webpac->{'headline'},
159 dpavlin 374 );
160     } else {
161     print $webpac->output(
162     template => 'text.tt',
163     data => \@ds,
164 dpavlin 398 headline => $webpac->{'headline'},
165 dpavlin 374 );
166     }
167    
168 dpavlin 398 my $headline = $webpac->{'headline'};
169 dpavlin 390
170 dpavlin 392 my $f = $filename;
171     $f =~ s!out/!!;
172    
173 dpavlin 390 # save into index
174     foreach my $ds (@ds) {
175     next if (! $ds->{'swish'});
176    
177 dpavlin 466 # strip all non word characters from beginning or end
178     # of word
179     my $words = join(" ",@{$ds->{'swish'}});
180     $words =~ s/^\W+//;
181     $words =~ s/\W*\s+\W*/ /g;
182     $words =~ s/\W+$//;
183    
184 dpavlin 504 # first try to generate headline for this entry from index
185 dpavlin 520 my $h = $ds->{'index'}->[0];
186 dpavlin 504 # then, from display
187 dpavlin 520 $h ||= $ds->{'display'}->[0];
188 dpavlin 504 # and as last resport, fallback to headline
189     $h ||= $headline;
190    
191 dpavlin 390 $index->insert(
192     index_name => $ds->{'tag'},
193 dpavlin 466 #path => $f,
194     path => $webpac->mfn,
195 dpavlin 504 headline => $h,
196 dpavlin 546 words => unac($words),
197 dpavlin 390 );
198     }
199    
200 dpavlin 410 # save into sorted index (thesaurus)
201     foreach my $ds (@ds) {
202     next if (! $ds->{'index'});
203    
204 dpavlin 511 $thes->{$ds->{'tag'}} ||= new WebPAC::Index( name => $ds->{'tag'} );
205 dpavlin 410
206 dpavlin 415 foreach my $h (@{$ds->{'index'}}) {
207     $thes->{$ds->{'tag'}}->insert(
208 dpavlin 546 sort_by => unac_2($h),
209     mfn => $webpac->mfn,
210 dpavlin 415 headline => $h,
211     );
212     }
213 dpavlin 410 }
214    
215 dpavlin 454 # print Dump(\@ds);
216 dpavlin 390
217 dpavlin 3 }
218    
219 dpavlin 411 foreach my $t (keys %{$thes}) {
220    
221     my @e = $thes->{$t}->elements;
222     if (! @e) {
223     $log->logwarn("no elements in sorted index $t?");
224     next;
225     }
226    
227 dpavlin 415 my $file = "./out/bfilter/$t.txt";
228     $log->info("saving sorted index $t to '$file' [".scalar(@e)." elements]");
229 dpavlin 411
230     $webpac->output_file(
231 dpavlin 415 file => $file,
232 dpavlin 411 template => 'index.tt',
233     data => \@e,
234     index_name => $t,
235     );
236     }
237    
238     if (0 && $log->is_debug) {
239 dpavlin 454 $log->debug("lookup hash: ",Dump($webpac->{'lookup'}));
240     $log->debug("data hash: ",Dump($webpac->{'data'}));
241 dpavlin 410 foreach my $t (keys %{$thes}) {
242 dpavlin 454 $log->debug("thesaurus $t hash: ",Dump($thes->{$t}));
243 dpavlin 410 }
244 dpavlin 372 }
245 dpavlin 390
246 dpavlin 424 } # XXX if(0)
247    
248 dpavlin 530 #$log->debug("lookup hash: ",Dump($webpac->{'lookup'}));
249 dpavlin 420
250 dpavlin 441 $log->info("creating tree");
251    
252 dpavlin 419 #
253 dpavlin 441 # define tree structure
254 dpavlin 419 #
255    
256 dpavlin 424 my $l = $webpac->{'lookup'} || $log->logconfess("can't find lookup");
257 dpavlin 419
258 dpavlin 424 my @tree = ({
259     # level 0
260     code_arr => sub { sort keys %{$l} },
261     filter_code => sub {
262     my $t = shift;
263     return $t if ($t =~ s/root://);
264     },
265     lookup_v900 => sub { shift @{$l->{"root:".$_[0]}} },
266     lookup_term => sub { shift @{$l->{"d:".$_[1]}} },
267     lookup_mfn => sub { shift @{$l->{"900_mfn:".$_[1]}} },
268 dpavlin 454 have_children => sub { return $l->{"a:".$_[0]."::"} },
269     have_children_at_level => sub {
270 dpavlin 471 return unless (defined($l->{"code:".$_[1]}));
271     my $code = shift @{$l->{"code:".$_[1]}};
272     print STDERR "## $_[1] -> $code\n";
273     return undef unless($code);
274     return(9, $l->{"a:$code:"} ) if (defined($l->{"a:$code:"}));
275 dpavlin 454 },
276 dpavlin 424 style => 'display: none',
277     },{
278     # 1
279 dpavlin 454 code_arr => sub { @{$_[0]} },
280 dpavlin 424 filter_code => sub { shift }, # nop
281     lookup_v900 => sub { shift @{$l->{"code:".$_[0]}} },
282     lookup_term => sub { shift @{$l->{"d:".$_[0]}} },
283     lookup_mfn => sub { shift @{$l->{"900_mfn:".$_[0]}} },
284 dpavlin 454 have_children => sub { return $l->{"a:".$_[1].":"} },
285 dpavlin 424 style => 'display: none',
286     },{
287     # 2
288 dpavlin 454 code_arr => sub { @{$_[0]} },
289 dpavlin 424 filter_code => sub { shift },
290 dpavlin 460 lookup_v900 => sub { shift @{$l->{"code:".$_[0]}} },
291 dpavlin 424 lookup_term => sub { shift @{$l->{"d:".$_[0]}} },
292     lookup_mfn => sub { shift @{$l->{"900_mfn:".$_[0]}} },
293 dpavlin 460 have_children => sub { return $l->{"a:".$_[1].":".$_[0]} },
294 dpavlin 454 #style => 'display: none',
295 dpavlin 425 },{
296     # 3 u¾i pojam
297 dpavlin 454 code_arr => sub { @{$_[0]} },
298 dpavlin 425 filter_code => sub { shift },
299 dpavlin 460 lookup_v900 => sub { shift @{$l->{"code:".$_[0]}} },
300 dpavlin 426 lookup_term => sub { shift @{$l->{"d:".$_[0]}} },
301     lookup_mfn => sub { shift @{$l->{"900_mfn:".$_[0]}} },
302 dpavlin 460 have_children => sub { return $l->{"a:".$_[1].":".$_[0]} },
303 dpavlin 426 },{
304     # 4
305 dpavlin 454 code_arr => sub { @{$_[0]} },
306 dpavlin 426 filter_code => sub { shift },
307 dpavlin 460 lookup_v900 => sub { shift @{$l->{"code:".$_[0]}} },
308 dpavlin 426 lookup_term => sub { shift @{$l->{"d:".$_[0]}} },
309     lookup_mfn => sub { shift @{$l->{"900_mfn:".$_[0]}} },
310 dpavlin 460 have_children => sub { return $l->{"a:".$_[1].":".$_[0]} },
311 dpavlin 427 },{
312     # 5
313 dpavlin 454 code_arr => sub { @{$_[0]} },
314 dpavlin 427 filter_code => sub { shift },
315 dpavlin 460 lookup_v900 => sub { shift @{$l->{"code:".$_[0]}} },
316 dpavlin 427 lookup_term => sub { shift @{$l->{"d:".$_[0]}} },
317     lookup_mfn => sub { shift @{$l->{"900_mfn:".$_[0]}} },
318 dpavlin 460 have_children => sub { return $l->{"a:".$_[1].":".$_[0]} },
319 dpavlin 427 },{
320     # 6
321 dpavlin 454 code_arr => sub { @{$_[0]} },
322 dpavlin 427 filter_code => sub { shift },
323 dpavlin 460 lookup_v900 => sub { shift @{$l->{"code:".$_[0]}} },
324 dpavlin 427 lookup_term => sub { shift @{$l->{"d:".$_[0]}} },
325     lookup_mfn => sub { shift @{$l->{"900_mfn:".$_[0]}} },
326 dpavlin 460 have_children => sub { return $l->{"a:".$_[1].":".$_[0]} },
327 dpavlin 427 },{
328     # 7
329 dpavlin 454 code_arr => sub { @{$_[0]} },
330 dpavlin 427 filter_code => sub { shift },
331 dpavlin 460 lookup_v900 => sub { shift @{$l->{"code:".$_[0]}} },
332 dpavlin 427 lookup_term => sub { shift @{$l->{"d:".$_[0]}} },
333     lookup_mfn => sub { shift @{$l->{"900_mfn:".$_[0]}} },
334 dpavlin 460 have_children => sub { return $l->{"a:".$_[1].":".$_[0]} },
335 dpavlin 427 },{
336     # 8
337 dpavlin 454 code_arr => sub { @{$_[0]} },
338 dpavlin 427 filter_code => sub { shift },
339 dpavlin 460 lookup_v900 => sub { shift @{$l->{"code:".$_[0]}} },
340 dpavlin 427 lookup_term => sub { shift @{$l->{"d:".$_[0]}} },
341     lookup_mfn => sub { shift @{$l->{"900_mfn:".$_[0]}} },
342 dpavlin 460 # have_children => sub { return $l->{"a:".$_[1].":".$_[0]} },
343 dpavlin 424 have_children => sub { 0 },
344 dpavlin 454 },{
345     # 9 - level which is never reached except explicitly
346 dpavlin 471 code_arr => sub { @{$_[0]} },
347 dpavlin 454 filter_code => sub { shift },
348 dpavlin 460 lookup_v900 => sub { shift @{$l->{"code:".$_[0]}} },
349 dpavlin 454 lookup_term => sub { shift @{$l->{"d:".$_[0]}} },
350     lookup_mfn => sub { shift @{$l->{"900_mfn:".$_[0]}} },
351     have_children => sub { 0 },
352 dpavlin 471 have_children_at_level => sub { defined($l->{"a:".$_[1].":".$_[0]}) && return (9,$l->{"a:".$_[1].":".$_[0]}) },
353 dpavlin 454 },{
354 dpavlin 424 });
355 dpavlin 419
356 dpavlin 441 my $tree = new WebPAC::Tree(
357 dpavlin 530 tree => \@tree,
358     );
359    
360     $tree->output(
361     dir => './out',
362     html => 'browse.html',
363 dpavlin 441 template => './output_template/tree.tt',
364     js => 'tree-ids.js',
365     );
366 dpavlin 428
367 dpavlin 530 $tree->output(
368     dir => './eurovoc',
369     html => 'hijerarhija.html',
370     template => './output_template/hijerarhija.tt',
371     js => 'tree-ids.js',
372     );
373    
374    
375 dpavlin 428 $log->info("closing index");
376 dpavlin 390 $index->close;
377    
378 dpavlin 422 $log->info("elapsed time: ",$webpac->fmt_time(time() - $webpac->{'start_t'}));

Properties

Name Value
cvs2svn:cvs-rev 1.64
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26