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

Last change on this file since 11089 was 10468, checked in by kjdon, 19 years ago

made a base builder class, adn moved lots of the code to it. hoe I haven't stuffed anything up :-)

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