source: gsdl/trunk/perllib/mgbuilder.pm@ 15716

Last change on this file since 15716 was 15716, checked in by mdewsnip, 16 years ago

Added "use strict" and fixed various problems that it found.

  • Property svn:keywords set to Author Date Id Revision
File size: 18.6 KB
Line 
1###########################################################################
2#
3# mgbuilder.pm -- MGBuilder object
4# A component of the Greenstone digital library software
5# from the New Zealand Digital Library Project at the
6# University of Waikato, New Zealand.
7#
8# Copyright (C) 1999 New Zealand Digital Library Project
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
25
26package mgbuilder;
27
28use basebuilder;
29use plugin;
30use strict; no strict 'refs';
31use util;
32
33
34BEGIN {
35 @mgbuilder::ISA = ('basebuilder');
36}
37
38
39my %wanted_index_files = ('td'=>1,
40 't'=>1,
41 'idb'=>1,
42 'ib1'=>1,
43 'ib2'=>1,
44 'ib3'=>1,
45 'i'=>1,
46 'ip'=>1,
47 'tiw'=>1,
48 'wa'=>1);
49
50my $maxdocsize = $basebuilder::maxdocsize;
51
52
53sub new {
54 my $class = shift(@_);
55
56 my $self = new basebuilder (@_);
57 $self = bless $self, $class;
58
59 $self->{'buildtype'} = "mg";
60 return $self;
61}
62
63sub default_buildproc {
64 my $self = shift (@_);
65
66 return "mgbuildproc";
67}
68
69sub generate_index_list {
70 my $self = shift (@_);
71
72 if (!defined($self->{'collect_cfg'}->{'indexes'})) {
73 $self->{'collect_cfg'}->{'indexes'} = [];
74 }
75 if (scalar(@{$self->{'collect_cfg'}->{'indexes'}}) == 0) {
76 # no indexes have been specified so we'll build a "dummy:text" index
77 push (@{$self->{'collect_cfg'}->{'indexes'}}, "dummy:text");
78 }
79
80}
81
82sub generate_index_options {
83 my $self = shift (@_);
84
85 $self->{'casefold'} = 0;
86 $self->{'stem'} = 0;
87 $self->{'accentfold'} = 0; #not yet implemented for mg
88
89 if (!defined($self->{'collect_cfg'}->{'indexoptions'})) {
90 # just use default options
91 $self->{'casefold'} = 1;
92 $self->{'stem'} = 1;
93 } else {
94 foreach my $option (@{$self->{'collect_cfg'}->{'indexoptions'}}) {
95 if ($option =~ /stem/) {
96 $self->{'stem'} = 1;
97 } elsif ($option =~ /casefold/) {
98 $self->{'casefold'} = 1;
99 }
100 }
101 }
102
103 # now we record this for the build cfg
104 $self->{'stemindexes'} = 0;
105 if ($self->{'casefold'}) {
106 $self->{'stemindexes'} += 1;
107 }
108 if ($self->{'stem'}) {
109 $self->{'stemindexes'} += 2;
110 }
111
112
113}
114
115sub compress_text {
116 my $self = shift (@_);
117 my ($textindex) = @_;
118 my $exedir = "$ENV{'GSDLHOME'}/bin/$ENV{'GSDLOS'}";
119 my $exe = &util::get_os_exe ();
120 my $mg_passes_exe = &util::filename_cat($exedir, "mg_passes$exe");
121 my $mg_compression_dict_exe = &util::filename_cat($exedir, "mg_compression_dict$exe");
122 my $outhandle = $self->{'outhandle'};
123
124 my $maxnumeric = $self->{'maxnumeric'};
125
126 &util::mk_all_dir (&util::filename_cat($self->{'build_dir'}, "text"));
127
128 my $collect_tail = &util::get_dirsep_tail($self->{'collection'});
129 my $basefilename = &util::filename_cat("text",$collect_tail);
130 my $fulltextprefix = &util::filename_cat ($self->{'build_dir'}, $basefilename);
131
132 my $osextra = "";
133 if ($ENV{'GSDLOS'} =~ /^windows$/i) {
134 $fulltextprefix =~ s@/@\\@g;
135 } else {
136 $osextra = " -d /";
137 }
138
139 print $outhandle "\n*** creating the compressed text\n" if ($self->{'verbosity'} >= 1);
140 print STDERR "<Stage name='CompressText'>\n" if $self->{'gli'};
141
142 # collect the statistics for the text
143 # -b $maxdocsize sets the maximum document size to be 12 meg
144 print $outhandle "\n collecting text statistics\n" if ($self->{'verbosity'} >= 1);
145 print STDERR "<Phase name='CollectTextStats'/>\n" if $self->{'gli'};
146
147 my ($handle);
148 if ($self->{'debug'}) {
149 $handle = *STDOUT;
150 }
151 else {
152 if (!-e "$mg_passes_exe" ||
153 !open($handle, "| mg_passes$exe -f \"$fulltextprefix\" -b $maxdocsize -T1 -M $maxnumeric $osextra")) {
154 print STDERR "<FatalError name='NoRunMGPasses'>\n</Stage>\n" if $self->{'gli'};
155 die "mgbuilder::compress_text - couldn't run $mg_passes_exe\n";
156 }
157 }
158
159 $self->{'buildproc'}->set_output_handle ($handle);
160 $self->{'buildproc'}->set_mode ('text');
161 $self->{'buildproc'}->set_index ($textindex);
162 $self->{'buildproc'}->set_indexing_text (0);
163
164
165 if ($self->{'no_text'}) {
166 $self->{'buildproc'}->set_store_text(0);
167 } else {
168 $self->{'buildproc'}->set_store_text(1);
169 }
170 $self->{'buildproc'}->reset();
171
172 &plugin::begin($self->{'pluginfo'}, $self->{'source_dir'},
173 $self->{'buildproc'}, $self->{'maxdocs'});
174 &plugin::read ($self->{'pluginfo'}, $self->{'source_dir'},
175 "", {}, $self->{'buildproc'}, $self->{'maxdocs'}, 0, $self->{'gli'});
176 &plugin::end($self->{'pluginfo'});
177
178
179 close ($handle) unless $self->{'debug'};
180
181 $self->print_stats();
182
183 # create the compression dictionary
184 # the compression dictionary is built by assuming the stats are from a seed
185 # dictionary (-S), if a novel word is encountered it is spelled out (-H),
186 # and the resulting dictionary must be less than 5 meg with the most frequent
187 # words being put into the dictionary first (-2 -k 5120)
188 if (!$self->{'debug'}) {
189 print $outhandle "\n creating the compression dictionary\n" if ($self->{'verbosity'} >= 1);
190 print STDERR "<Phase name='CreatingCompress'/>\n" if $self->{'gli'};
191 if (!-e "$mg_compression_dict_exe") {
192 die "mgbuilder::compress_text - couldn't run $mg_compression_dict_exe\n";
193 }
194 system ("mg_compression_dict$exe -f \"$fulltextprefix\" -S -H -2 -k 5120 $osextra");
195
196 # -b $maxdocsize sets the maximum document size to be 12 meg
197 if (!-e "$mg_passes_exe" ||
198 !open ($handle, "| mg_passes$exe -f \"$fulltextprefix\" -b $maxdocsize -T2 -M $maxnumeric $osextra")) {
199 print STDERR "<FatalError name='NoRunMGPasses'/>\n</Stage>\n" if $self->{'gli'};
200 die "mgbuilder::compress_text - couldn't run $mg_passes_exe\n";
201 }
202 }
203 else {
204 print STDERR "<Phase name='SkipCreatingComp'/>\n" if $self->{'gli'};
205 }
206
207 $self->{'buildproc'}->reset();
208 # compress the text
209 print $outhandle "\n compressing the text\n" if ($self->{'verbosity'} >= 1);
210 print STDERR "<Phase name='CompressingText'/>\n" if $self->{'gli'};
211
212 &plugin::read ($self->{'pluginfo'}, $self->{'source_dir'},
213 "", {}, $self->{'buildproc'}, $self->{'maxdocs'}, 0, $self->{'gli'});
214
215 close ($handle) unless $self->{'debug'};
216
217 $self->print_stats();
218 print STDERR "</Stage>\n" if $self->{'gli'};
219}
220
221
222# creates directory names for each of the index descriptions
223sub create_index_mapping {
224 my $self = shift (@_);
225 my ($indexes) = @_;
226
227 my %mapping = ();
228 $mapping{'indexmaporder'} = [];
229 $mapping{'subcollectionmaporder'} = [];
230 $mapping{'languagemaporder'} = [];
231
232 # dirnames is used to check for collisions. Start this off
233 # with the manditory directory names
234 my %dirnames = ('text'=>'text',
235 'extra'=>'extra');
236 my %pnames = ('index' => {}, 'subcollection' => {}, 'languages' => {});
237 foreach my $index (@$indexes) {
238 my ($level, $gran, $subcollection, $languages) = split (":", $index);
239
240 # the directory name starts with the first character of the index level
241 my ($pindex) = $level =~ /^(.)/;
242
243 # next comes a processed version of the index
244 $pindex .= $self->process_field ($gran);
245 $pindex = lc ($pindex);
246
247 # next comes a processed version of the subcollection if there is one.
248 my $psub = $self->process_field ($subcollection);
249 $psub = lc ($psub);
250
251 # next comes a processed version of the language if there is one.
252 my $plang = $self->process_field ($languages);
253 $plang = lc ($plang);
254
255 my $dirname = $pindex . $psub . $plang;
256
257 # check to be sure all index names are unique
258 while (defined ($dirnames{$dirname})) {
259 $dirname = $self->make_unique (\%pnames, $index, \$pindex, \$psub, \$plang);
260 }
261 $mapping{$index} = $dirname;
262
263 # store the mapping orders as well as the maps
264 # also put index, subcollection and language fields into the mapping thing -
265 # (the full index name (eg document:text:subcol:lang) is not used on
266 # the query page) -these are used for collectionmeta later on
267 if (!defined $mapping{'indexmap'}{"$level:$gran"}) {
268 $mapping{'indexmap'}{"$level:$gran"} = $pindex;
269 push (@{$mapping{'indexmaporder'}}, "$level:$gran");
270 if (!defined $mapping{"$level:$gran"}) {
271 $mapping{"$level:$gran"} = $pindex;
272 }
273 }
274 if ($psub =~ /\w/ && !defined ($mapping{'subcollectionmap'}{$subcollection})) {
275 $mapping{'subcollectionmap'}{$subcollection} = $psub;
276 push (@{$mapping{'subcollectionmaporder'}}, $subcollection);
277 $mapping{$subcollection} = $psub;
278 }
279 if ($plang =~ /\w/ && !defined ($mapping{'languagemap'}{$languages})) {
280 $mapping{'languagemap'}{$languages} = $plang;
281 push (@{$mapping{'languagemaporder'}}, $languages);
282 $mapping{$languages} = $plang;
283 }
284 $dirnames{$dirname} = $index;
285 $pnames{'index'}->{$pindex} = "$level:$gran";
286 $pnames{'subcollection'}->{$psub} = $subcollection;
287 $pnames{'languages'}->{$plang} = $languages;
288 }
289
290 return \%mapping;
291}
292
293
294sub make_unique {
295 my $self = shift (@_);
296 my ($namehash, $index, $indexref, $subref, $langref) = @_;
297 my ($level, $gran, $subcollection, $languages) = split (":", $index);
298
299 if ($namehash->{'index'}->{$$indexref} ne "$level:$gran") {
300 $self->get_next_version ($indexref);
301 } elsif ($namehash->{'subcollection'}->{$$subref} ne $subcollection) {
302 $self->get_next_version ($subref);
303 } elsif ($namehash->{'languages'}->{$$langref} ne $languages) {
304 $self->get_next_version ($langref);
305 }
306 return "$$indexref$$subref$$langref";
307}
308
309sub build_index {
310 my $self = shift (@_);
311 my ($index) = @_;
312 my $outhandle = $self->{'outhandle'};
313
314 # get the full index directory path and make sure it exists
315 my $indexdir = $self->{'index_mapping'}->{$index};
316 &util::mk_all_dir (&util::filename_cat($self->{'build_dir'}, $indexdir));
317
318 my $collect_tail = &util::get_dirsep_tail($self->{'collection'});
319 my $fullindexprefix = &util::filename_cat ($self->{'build_dir'}, $indexdir,
320 $collect_tail);
321 my $fulltextprefix = &util::filename_cat ($self->{'build_dir'}, "text",
322 $collect_tail);
323
324 # get any os specific stuff
325 my $exedir = "$ENV{'GSDLHOME'}/bin/$ENV{'GSDLOS'}";
326 my $exe = &util::get_os_exe ();
327 my $mg_passes_exe = &util::filename_cat($exedir, "mg_passes$exe");
328 my $mg_perf_hash_build_exe =
329 &util::filename_cat($exedir, "mg_perf_hash_build$exe");
330 my $mg_weights_build_exe =
331 &util::filename_cat ($exedir, "mg_weights_build$exe");
332 my $mg_invf_dict_exe =
333 &util::filename_cat ($exedir, "mg_invf_dict$exe");
334 my $mg_stem_idx_exe =
335 &util::filename_cat ($exedir, "mg_stem_idx$exe");
336
337 my $maxnumeric = $self->{'maxnumeric'};
338
339 my $osextra = "";
340 if ($ENV{'GSDLOS'} =~ /^windows$/i) {
341 $fullindexprefix =~ s@/@\\@g;
342 } else {
343 $osextra = " -d /";
344 if ($outhandle ne "STDERR") {
345 # so mg_passes doesn't print to stderr if we redirect output
346 $osextra .= " 2>/dev/null";
347 }
348 }
349
350 # get the index level from the index description
351 # the index will be level 2 unless we are building a
352 # paragraph level index
353 my $index_level = 2;
354 $index_level = 3 if $index =~ /^paragraph/i;
355
356 # get the index expression if this index belongs
357 # to a subcollection
358 my $indexexparr = [];
359 my $langarr = [];
360 # there may be subcollection info, and language info.
361 my ($level, $fields, $subcollection, $language) = split (":", $index);
362 my @subcollections = ();
363 @subcollections = split /,/, $subcollection if (defined $subcollection);
364
365 foreach my $subcollection (@subcollections) {
366 if (defined ($self->{'collect_cfg'}->{'subcollection'}->{$subcollection})) {
367 push (@$indexexparr, $self->{'collect_cfg'}->{'subcollection'}->{$subcollection});
368 }
369 }
370
371 # add expressions for languages if this index belongs to
372 # a language subcollection - only put languages expressions for the
373 # ones we want in the index
374
375 my @languages = ();
376 my $language_metadata = "Language";
377 if (defined ($self->{'collect_cfg'}->{'language_metadata'})) {
378 $language_metadata = $self->{'collect_cfg'}->{'language_metadata'};
379 }
380 @languages = split /,/, $language if (defined $language);
381 foreach my $language (@languages) {
382 my $not=0;
383 if ($language =~ s/^\!//) {
384 $not = 1;
385 }
386 if($not) {
387 push (@$langarr, "!$language");
388 } else {
389 push (@$langarr, "$language");
390 }
391 }
392
393 # Build index dictionary. Uses verbatim stem method
394 print $outhandle "\n creating index dictionary\n" if ($self->{'verbosity'} >= 1);
395 print STDERR "<Phase name='CreatingIndexDic'/>\n" if $self->{'gli'};
396 my ($handle);
397 if ($self->{'debug'}) {
398 $handle = *STDOUT;
399 }
400 else {
401 if (!-e "$mg_passes_exe" ||
402 !open($handle, "| mg_passes$exe -f \"$fullindexprefix\" -b $maxdocsize " .
403 "-$index_level -m 32 -s 0 -G -t 10 -N1 -M $maxnumeric $osextra")) {
404 print STDERR "<FatalError name='NoRunMGPasses'/>\n</Stage>\n" if $self->{'gli'};
405 die "mgbuilder::build_index - couldn't run $mg_passes_exe\n";
406 }
407 }
408
409 # set up the document processor
410 $self->{'buildproc'}->set_output_handle ($handle);
411 $self->{'buildproc'}->set_mode ('text');
412 $self->{'buildproc'}->set_index ($index, $indexexparr);
413 $self->{'buildproc'}->set_index_languages ($language_metadata, $langarr) if (defined $language);
414 $self->{'buildproc'}->set_indexing_text (1);
415 $self->{'buildproc'}->set_store_text(1);
416
417 $self->{'buildproc'}->reset();
418 &plugin::read ($self->{'pluginfo'}, $self->{'source_dir'},
419 "", {}, $self->{'buildproc'}, $self->{'maxdocs'},0, $self->{'gli'});
420 close ($handle) unless $self->{'debug'};
421
422 $self->print_stats();
423
424 # now we check to see if the required files have been produced - if not we quit building this index so the whole process doesn't crap out.
425 # we check on the .id file - index dictionary
426 my $dict_file = "$fullindexprefix.id";
427 if (!-e $dict_file) {
428 print $outhandle "mgbuilder::build_index - Couldn't create index $index\n";
429 $self->{'notbuilt'}->{$index}=1;
430 return;
431 }
432 if (!$self->{'debug'}) {
433 # create the perfect hash function
434 if (!-e "$mg_perf_hash_build_exe") {
435 print STDERR "<FatalError name='NoRunMGHash'/>\n</Stage>\n" if $self->{'gli'};
436 die "mgbuilder::build_index - couldn't run $mg_perf_hash_build_exe\n";
437 }
438 system ("mg_perf_hash_build$exe -f \"$fullindexprefix\" $osextra");
439
440 if (!-e "$mg_passes_exe" ||
441 !open ($handle, "| mg_passes$exe -f \"$fullindexprefix\" -b $maxdocsize " .
442 "-$index_level -c 3 -G -t 10 -N2 -M $maxnumeric $osextra")) {
443 print STDERR "<FatalError name='NoRunMGPasses'/>\n</Stage>\n" if $self->{'gli'};
444 die "mgbuilder::build_index - couldn't run $mg_passes_exe\n";
445 }
446 }
447
448 # invert the text
449 print $outhandle "\n inverting the text\n" if ($self->{'verbosity'} >= 1);
450 print STDERR "<Phase name='InvertingText'/>\n" if $self->{'gli'};
451 $self->{'buildproc'}->reset();
452 &plugin::read ($self->{'pluginfo'}, $self->{'source_dir'},
453 "", {}, $self->{'buildproc'}, $self->{'maxdocs'},0, $self->{'gli'});
454
455
456 $self->print_stats ();
457
458 if (!$self->{'debug'}) {
459
460 close ($handle);
461
462 # create the weights file
463 print $outhandle "\n create the weights file\n" if ($self->{'verbosity'} >= 1);
464 print STDERR "<Phase name='CreateTheWeights'/>\n" if $self->{'gli'};
465 if (!-e "$mg_weights_build_exe") {
466 print STDERR "<FatalError name='NoRunMGWeights'/>\n</Stage>\n" if $self->{'gli'};
467 die "mgbuilder::build_index - couldn't run $mg_weights_build_exe\n";
468 }
469 system ("mg_weights_build$exe -f \"$fullindexprefix\" -t \"$fulltextprefix\" $osextra");
470
471 # create 'on-disk' stemmed dictionary
472 print $outhandle "\n creating 'on-disk' stemmed dictionary\n" if ($self->{'verbosity'} >= 1);
473 print STDERR "<Phase name='CreateStemmedDic'/>\n" if $self->{'gli'};
474 if (!-e "$mg_invf_dict_exe") {
475 print STDERR "<FatalError name='NoRunMGInvf'/>\n</Stage>\n" if $self->{'gli'};
476 die "mgbuilder::build_index - couldn't run $mg_invf_dict_exe\n";
477 }
478 system ("mg_invf_dict$exe -f \"$fullindexprefix\" $osextra");
479
480
481 # creates stem index files for the various stemming methods
482 print $outhandle "\n creating stem indexes\n" if ($self->{'verbosity'} >= 1);
483 print STDERR "<Phase name='CreatingStemIndx'/>\n" if $self->{'gli'};
484 if (!-e "$mg_stem_idx_exe") {
485 print STDERR "<FatalError name='NoRunMGStem'/>\n</Stage>\n" if $self->{'gli'};
486 die "mgbuilder::build_index - couldn't run $mg_stem_idx_exe\n";
487 }
488 # currently mg wont work if we don't generate all the stem idexes
489 # so we generate them whatever, but don't advertise the fact
490 #if ($self->{'casefold'}) {
491 system ("mg_stem_idx$exe -b 4096 -s1 -f \"$fullindexprefix\" $osextra");
492 #}
493 #if ($self->{'stem'}) {
494 system ("mg_stem_idx$exe -b 4096 -s2 -f \"$fullindexprefix\" $osextra");
495 #}
496 #if ($self->{'casefold'} && $self->{'stem'}) {
497 system ("mg_stem_idx$exe -b 4096 -s3 -f \"$fullindexprefix\" $osextra");
498 #}
499
500 # remove unwanted files
501 my $tmpdir = &util::filename_cat ($self->{'build_dir'}, $indexdir);
502 opendir (DIR, $tmpdir) || die
503 "mgbuilder::build_index - couldn't read directory $tmpdir\n";
504 foreach my $file (readdir(DIR)) {
505 next if $file =~ /^\./;
506 my ($suffix) = $file =~ /\.([^\.]+)$/;
507 if (defined $suffix && !defined $wanted_index_files{$suffix}) {
508 # delete it!
509 print $outhandle "deleting $file\n" if $self->{'verbosity'} > 2;
510 &util::rm (&util::filename_cat ($tmpdir, $file));
511 }
512 }
513 closedir (DIR);
514 }
515 print STDERR "</Stage>\n" if $self->{'gli'};
516}
517
518sub build_cfg_extra {
519 my $self = shift(@_);
520 my ($build_cfg) = @_;
521
522 # get additional stats from mg
523 my $exedir = "$ENV{'GSDLHOME'}/bin/$ENV{'GSDLOS'}";
524 my $exe = &util::get_os_exe ();
525 my $mgstat_exe = &util::filename_cat($exedir, "mgstat$exe");
526
527 my $collect_tail = &util::get_dirsep_tail($self->{'collection'});
528 my $input_file = &util::filename_cat ("text", $collect_tail);
529 if (!-e "$mgstat_exe" || !open (PIPEIN, "mgstat$exe -d \"$self->{'build_dir'}\" -f \"$input_file\" |")) {
530 my $outhandle = $self->{'outhandle'};
531 print $outhandle "Warning: Couldn't open pipe to $mgstat_exe to get additional stats\n";
532 } else {
533 my $line = "";
534 while (defined ($line = <PIPEIN>)) {
535 if ($line =~ /^Words in collection \[dict\]\s+:\s+(\d+)/) {
536 ($build_cfg->{'numwords'}) = $1;
537 } elsif ($line =~ /^Documents\s+:\s+(\d+)/) {
538 ($build_cfg->{'numsections'}) = $1;
539 }
540 }
541 close PIPEIN;
542 }
543}
544
5451;
546
547
548
Note: See TracBrowser for help on using the repository browser.