/[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 560 - (hide annotations)
Sat Oct 30 23:04:37 2004 UTC (14 years, 8 months ago) by dpavlin
File MIME type: text/plain
File size: 9688 byte(s)
removed upper case letters from encoding of index nodes (to support Windows
which is case-insensitive), added support for filters into TT templates,
changed crovoc symbol to #C# in data

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