source: main/trunk/greenstone2/perllib/mgbuilder.pm@ 24404

Last change on this file since 24404 was 24404, checked in by ak19, 11 years ago

Changes to perl code to do with removing the ex. prefix: ex. is only removed if it is the sole prefix (i.e. ex.dc.* prefixes are not removed).

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