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
RevLine 
[537]1###########################################################################
[4]2#
[537]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###########################################################################
[4]25
26package mgbuilder;
27
[10468]28use basebuilder;
[215]29use classify;
[4]30use cfgread;
31use colcfg;
32use plugin;
33use util;
[1304]34use FileHandle;
[4]35
[1304]36BEGIN {
[10468]37 @mgbuilder::ISA = ('basebuilder');
[1304]38}
39
40
[8716]41my %wanted_index_files = ('td'=>1,
[4]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
[10468]52my $maxdocsize = $basebuilder::maxdocsize;
[4]53
[10468]54
[4]55sub new {
[10468]56 my $class = shift(@_);
57
58 my ($collection, $source_dir, $build_dir, $verbosity,
[8361]59 $maxdocs, $debug, $keepold, $remove_empty_classifications,
[6332]60 $outhandle, $no_text, $failhandle, $gli) = @_;
[4]61
[10468]62 my $self = new basebuilder (@_);
63 $self = bless $self, $class;
[1424]64
[10468]65 $self->{'buildtype'} = "mg";
66 return $self;
67}
[4]68
[10468]69sub default_buildproc {
70 my $self = shift (@_);
[4]71
[10468]72 return "mgbuildproc";
73}
[4]74
[10468]75sub generate_index_list {
76 my $self = shift (@_);
77
[5225]78 if (!defined($self->{'collect_cfg'}->{'indexes'})) {
79 $self->{'collect_cfg'}->{'indexes'} = [];
80 }
[4743]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
[4]86}
87
88
89sub compress_text {
90 my $self = shift (@_);
[134]91 my ($textindex) = @_;
[4]92 my $exedir = "$ENV{'GSDLHOME'}/bin/$ENV{'GSDLOS'}";
93 my $exe = &util::get_os_exe ();
[486]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");
[1424]96 my $outhandle = $self->{'outhandle'};
[4]97
[12340]98 my $maxnumeric = $self->{'maxnumeric'};
[4192]99
[4]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) {
[3115]106 $fulltextprefix =~ s@/@\\@g;
[4]107 } else {
108 $osextra = " -d /";
109 }
110
[1424]111 print $outhandle "\n*** creating the compressed text\n" if ($self->{'verbosity'} >= 1);
[6332]112 print STDERR "<Stage name='CompressText'>\n" if $self->{'gli'};
[4]113
114 # collect the statistics for the text
115 # -b $maxdocsize sets the maximum document size to be 12 meg
[1424]116 print $outhandle "\n collecting text statistics\n" if ($self->{'verbosity'} >= 1);
[6407]117 print STDERR "<Phase name='CollectTextStats'/>\n" if $self->{'gli'};
[782]118
119 my ($handle);
120 if ($self->{'debug'}) {
121 $handle = STDOUT;
122 } else {
123 if (!-e "$mg_passes_exe" ||
[4192]124 !open (PIPEOUT, "| mg_passes$exe -f \"$fulltextprefix\" -b $maxdocsize -T1 -M $maxnumeric $osextra")) {
[6407]125 print STDERR "<FatalError name='NoRunMGPasses'>\n</Stage>\n" if $self->{'gli'};
[782]126 die "mgbuilder::compress_text - couldn't run $mg_passes_exe\n";
127 }
128 $handle = mgbuilder::PIPEOUT;
[4]129 }
[782]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);
[7904]135
136
[2336]137 if ($self->{'no_text'}) {
138 $self->{'buildproc'}->set_store_text(0);
139 } else {
140 $self->{'buildproc'}->set_store_text(1);
141 }
[4]142 $self->{'buildproc'}->reset();
[7904]143
[835]144 &plugin::begin($self->{'pluginfo'}, $self->{'source_dir'},
145 $self->{'buildproc'}, $self->{'maxdocs'});
146 &plugin::read ($self->{'pluginfo'}, $self->{'source_dir'},
[9853]147 "", {}, $self->{'buildproc'}, $self->{'maxdocs'}, 0, $self->{'gli'});
[835]148 &plugin::end($self->{'pluginfo'});
[7904]149
[4]150
[782]151 close ($handle) unless $self->{'debug'};
152
[1251]153 $self->print_stats();
154
[4]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)
[782]160 if (!$self->{'debug'}) {
[1424]161 print $outhandle "\n creating the compression dictionary\n" if ($self->{'verbosity'} >= 1);
[6407]162 print STDERR "<Phase name='CreatingCompress'/>\n" if $self->{'gli'};
[782]163 if (!-e "$mg_compression_dict_exe") {
164 die "mgbuilder::compress_text - couldn't run $mg_compression_dict_exe\n";
165 }
[1679]166 system ("mg_compression_dict$exe -f \"$fulltextprefix\" -S -H -2 -k 5120 $osextra");
[782]167
168 # -b $maxdocsize sets the maximum document size to be 12 meg
[1072]169 if (!-e "$mg_passes_exe" ||
[4192]170 !open ($handle, "| mg_passes$exe -f \"$fulltextprefix\" -b $maxdocsize -T2 -M $maxnumeric $osextra")) {
[6407]171 print STDERR "<FatalError name='NoRunMGPasses'/>\n</Stage>\n" if $self->{'gli'};
[1072]172 die "mgbuilder::compress_text - couldn't run $mg_passes_exe\n";
[782]173 }
[4]174 }
[6332]175 else {
[6407]176 print STDERR "<Phase name='SkipCreatingComp'/>\n" if $self->{'gli'};
[6332]177 }
[4]178
[782]179 $self->{'buildproc'}->reset();
[4]180 # compress the text
[1424]181 print $outhandle "\n compressing the text\n" if ($self->{'verbosity'} >= 1);
[6407]182 print STDERR "<Phase name='CompressingText'/>\n" if $self->{'gli'};
[6332]183
[4]184 &plugin::read ($self->{'pluginfo'}, $self->{'source_dir'},
[9853]185 "", {}, $self->{'buildproc'}, $self->{'maxdocs'}, 0, $self->{'gli'});
[7904]186
[782]187 close ($handle) unless $self->{'debug'};
[1251]188
189 $self->print_stats();
[6407]190 print STDERR "</Stage>\n" if $self->{'gli'};
[4]191}
192
[486]193
[4]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 = ();
[290]200 $mapping{'indexmaporder'} = [];
201 $mapping{'subcollectionmaporder'} = [];
202 $mapping{'languagemaporder'} = [];
[4]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');
[8716]208 my %pnames = ('index' => {}, 'subcollection' => {}, 'languages' => {});
209 foreach my $index (@$indexes) {
[139]210 my ($level, $gran, $subcollection, $languages) = split (":", $index);
[4]211
[139]212 # the directory name starts with the first character of the index level
213 my ($pindex) = $level =~ /^(.)/;
[4]214
[139]215 # next comes a processed version of the index
216 $pindex .= $self->process_field ($gran);
217 $pindex = lc ($pindex);
218
[69]219 # next comes a processed version of the subcollection if there is one.
[139]220 my $psub = $self->process_field ($subcollection);
221 $psub = lc ($psub);
[69]222
[139]223 # next comes a processed version of the language if there is one.
224 my $plang = $self->process_field ($languages);
225 $plang = lc ($plang);
[4]226
[139]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);
[4]232 }
[1973]233 $mapping{$index} = $dirname;
[139]234
[290]235 # store the mapping orders as well as the maps
[1973]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
[290]239 if (!defined $mapping{'indexmap'}{"$level:$gran"}) {
240 $mapping{'indexmap'}{"$level:$gran"} = $pindex;
241 push (@{$mapping{'indexmaporder'}}, "$level:$gran");
[1973]242 if (!defined $mapping{"$level:$gran"}) {
243 $mapping{"$level:$gran"} = $pindex;
244 }
[290]245 }
246 if ($psub =~ /\w/ && !defined ($mapping{'subcollectionmap'}{$subcollection})) {
247 $mapping{'subcollectionmap'}{$subcollection} = $psub;
248 push (@{$mapping{'subcollectionmaporder'}}, $subcollection);
[1973]249 $mapping{$subcollection} = $psub;
[290]250 }
251 if ($plang =~ /\w/ && !defined ($mapping{'languagemap'}{$languages})) {
252 $mapping{'languagemap'}{$languages} = $plang;
[1973]253 push (@{$mapping{'languagemaporder'}}, $languages);
254 $mapping{$languages} = $plang;
[290]255 }
[4]256 $dirnames{$dirname} = $index;
[8716]257 $pnames{'index'}->{$pindex} = "$level:$gran";
258 $pnames{'subcollection'}->{$psub} = $subcollection;
259 $pnames{'languages'}->{$plang} = $languages;
[4]260 }
261
262 return \%mapping;
263}
264
265
[139]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
[4]281sub build_index {
282 my $self = shift (@_);
283 my ($index) = @_;
[1424]284 my $outhandle = $self->{'outhandle'};
[4]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 ();
[486]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
[12340]307 my $maxnumeric = $self->{'maxnumeric'};
[4192]308
[4]309 my $osextra = "";
310 if ($ENV{'GSDLOS'} =~ /^windows$/i) {
[3115]311 $fullindexprefix =~ s@/@\\@g;
[4]312 } else {
313 $osextra = " -d /";
[3115]314 if ($outhandle ne "STDERR") {
315 # so mg_passes doesn't print to stderr if we redirect output
316 $osextra .= " 2>/dev/null";
317 }
[4]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
[69]326 # get the index expression if this index belongs
327 # to a subcollection
328 my $indexexparr = [];
[9669]329 my $langarr = [];
[1973]330 # there may be subcollection info, and language info.
331 my ($level, $fields, $subcollection, $language) = split (":", $index);
[85]332 my @subcollections = ();
333 @subcollections = split /,/, $subcollection if (defined $subcollection);
[69]334
[8716]335 foreach my $subcollection (@subcollections) {
[69]336 if (defined ($self->{'collect_cfg'}->{'subcollection'}->{$subcollection})) {
337 push (@$indexexparr, $self->{'collect_cfg'}->{'subcollection'}->{$subcollection});
338 }
339 }
340
[139]341 # add expressions for languages if this index belongs to
[1973]342 # a language subcollection - only put languages expressions for the
343 # ones we want in the index
344
345 my @languages = ();
[9548]346 my $language_metadata = "Language";
347 if (defined ($self->{'collect_cfg'}->{'language_metadata'})) {
348 $language_metadata = $self->{'collect_cfg'}->{'language_metadata'};
349 }
[1973]350 @languages = split /,/, $language if (defined $language);
[8716]351 foreach my $language (@languages) {
[1973]352 my $not=0;
[139]353 if ($language =~ s/^\!//) {
[1973]354 $not = 1;
[139]355 }
[6543]356 if($not) {
[9669]357 push (@$langarr, "!$language");
[6543]358 } else {
[9669]359 push (@$langarr, "$language");
[1973]360 }
[139]361 }
[9669]362
[782]363 # Build index dictionary. Uses verbatim stem method
[1424]364 print $outhandle "\n creating index dictionary\n" if ($self->{'verbosity'} >= 1);
[6407]365 print STDERR "<Phase name='CreatingIndexDic'/>\n" if $self->{'gli'};
[782]366 my ($handle);
367 if ($self->{'debug'}) {
368 $handle = STDOUT;
369 } else {
370 if (!-e "$mg_passes_exe" ||
[1679]371 !open (PIPEOUT, "| mg_passes$exe -f \"$fullindexprefix\" -b $maxdocsize " .
[4192]372 "-$index_level -m 32 -s 0 -G -t 10 -N1 -M $maxnumeric $osextra")) {
[6407]373 print STDERR "<FatalError name='NoRunMGPasses'/>\n</Stage>\n" if $self->{'gli'};
[782]374 die "mgbuilder::build_index - couldn't run $mg_passes_exe\n";
375 }
376 $handle = mgbuilder::PIPEOUT;
377 }
378
[4]379 # set up the document processor
[782]380 $self->{'buildproc'}->set_output_handle ($handle);
[4]381 $self->{'buildproc'}->set_mode ('text');
[69]382 $self->{'buildproc'}->set_index ($index, $indexexparr);
[9669]383 $self->{'buildproc'}->set_index_languages ($language_metadata, $langarr) if (defined $language);
[292]384 $self->{'buildproc'}->set_indexing_text (1);
[2336]385 $self->{'buildproc'}->set_store_text(1);
[4]386
387 $self->{'buildproc'}->reset();
388 &plugin::read ($self->{'pluginfo'}, $self->{'source_dir'},
[9853]389 "", {}, $self->{'buildproc'}, $self->{'maxdocs'},0, $self->{'gli'});
[782]390 close ($handle) unless $self->{'debug'};
[4]391
[1251]392 $self->print_stats();
393
[5768]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 }
[782]402 if (!$self->{'debug'}) {
403 # create the perfect hash function
404 if (!-e "$mg_perf_hash_build_exe") {
[6407]405 print STDERR "<FatalError name='NoRunMGHash'/>\n</Stage>\n" if $self->{'gli'};
[782]406 die "mgbuilder::build_index - couldn't run $mg_perf_hash_build_exe\n";
407 }
[1679]408 system ("mg_perf_hash_build$exe -f \"$fullindexprefix\" $osextra");
[782]409
410 if (!-e "$mg_passes_exe" ||
[1679]411 !open ($handle, "| mg_passes$exe -f \"$fullindexprefix\" -b $maxdocsize " .
[4192]412 "-$index_level -c 3 -G -t 10 -N2 -M $maxnumeric $osextra")) {
[6407]413 print STDERR "<FatalError name='NoRunMGPasses'/>\n</Stage>\n" if $self->{'gli'};
[782]414 die "mgbuilder::build_index - couldn't run $mg_passes_exe\n";
415 }
[4]416 }
[782]417
[4]418 # invert the text
[1424]419 print $outhandle "\n inverting the text\n" if ($self->{'verbosity'} >= 1);
[6407]420 print STDERR "<Phase name='InvertingText'/>\n" if $self->{'gli'};
[4]421 $self->{'buildproc'}->reset();
422 &plugin::read ($self->{'pluginfo'}, $self->{'source_dir'},
[9853]423 "", {}, $self->{'buildproc'}, $self->{'maxdocs'},0, $self->{'gli'});
[1304]424
[7904]425
[1251]426 $self->print_stats ();
427
[782]428 if (!$self->{'debug'}) {
[4]429
[782]430 close ($handle);
431
432 # create the weights file
[1424]433 print $outhandle "\n create the weights file\n" if ($self->{'verbosity'} >= 1);
[6407]434 print STDERR "<Phase name='CreateTheWeights'/>\n" if $self->{'gli'};
[782]435 if (!-e "$mg_weights_build_exe") {
[6407]436 print STDERR "<FatalError name='NoRunMGWeights'/>\n</Stage>\n" if $self->{'gli'};
[782]437 die "mgbuilder::build_index - couldn't run $mg_weights_build_exe\n";
438 }
[1679]439 system ("mg_weights_build$exe -f \"$fullindexprefix\" -t \"$fulltextprefix\" $osextra");
[4]440
[782]441 # create 'on-disk' stemmed dictionary
[1424]442 print $outhandle "\n creating 'on-disk' stemmed dictionary\n" if ($self->{'verbosity'} >= 1);
[6407]443 print STDERR "<Phase name='CreateStemmedDic'/>\n" if $self->{'gli'};
[782]444 if (!-e "$mg_invf_dict_exe") {
[6407]445 print STDERR "<FatalError name='NoRunMGInvf'/>\n</Stage>\n" if $self->{'gli'};
[782]446 die "mgbuilder::build_index - couldn't run $mg_invf_dict_exe\n";
447 }
[1679]448 system ("mg_invf_dict$exe -f \"$fullindexprefix\" $osextra");
[4]449
450
[782]451 # creates stem index files for the various stemming methods
[1424]452 print $outhandle "\n creating stem indexes\n" if ($self->{'verbosity'} >= 1);
[6407]453 print STDERR "<Phase name='CreatingStemIndx'/>\n" if $self->{'gli'};
[782]454 if (!-e "$mg_stem_idx_exe") {
[6407]455 print STDERR "<FatalError name='NoRunMGStem'/>\n</Stage>\n" if $self->{'gli'};
[782]456 die "mgbuilder::build_index - couldn't run $mg_stem_idx_exe\n";
457 }
[1679]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");
[4]461
[782]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";
[8716]466 foreach my $file (readdir(DIR)) {
[782]467 next if $file =~ /^\./;
468 my ($suffix) = $file =~ /\.([^\.]+)$/;
469 if (defined $suffix && !defined $wanted_index_files{$suffix}) {
470 # delete it!
[1424]471 print $outhandle "deleting $file\n" if $self->{'verbosity'} > 2;
[782]472 &util::rm (&util::filename_cat ($tmpdir, $file));
473 }
[4]474 }
[782]475 closedir (DIR);
[4]476 }
[6407]477 print STDERR "</Stage>\n" if $self->{'gli'};
[4]478}
479
[10468]480sub build_cfg_extra {
481 my $self = shift(@_);
482 my ($build_cfg) = @_;
[2506]483
[1252]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'});
[1679]489 if (!-e "$mgstat_exe" || !open (PIPEIN, "mgstat$exe -d \"$self->{'build_dir'}\" -f \"$input_file\" |")) {
[1424]490 print $outhandle "Warning: Couldn't open pipe to $mgstat_exe to get additional stats\n";
[1252]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 }
[4]502}
503
5041;
[7904]505
506
507
Note: See TracBrowser for help on using the repository browser.