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

Last change on this file since 12697 was 12340, checked in by kjdon, 18 years ago

maxnumeric is set using set_maxnumeric (by buildcol.pl) rather than the builder looking directly in the collect.cfg file

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