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

Last change on this file since 24404 was 24404, checked in by ak19, 13 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: 28.3 KB
RevLine 
[932]1###########################################################################
2#
[1852]3# mgppbuilder.pm -- MGBuilder object
[932]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 mgppbuilder;
27
[10468]28use basebuilder;
[932]29use colcfg;
30use plugin;
[15715]31use strict; no strict 'refs';
[932]32use util;
33
[15715]34
[10468]35sub BEGIN {
36 @mgppbuilder::ISA = ('basebuilder');
[1694]37}
38
39
40
[9157]41our %level_map = ('document'=>'Doc',
[4811]42 'section'=>'Sec',
43 'paragraph'=>'Para',
44 'Doc'=>'_textdocument_',
45 'Sec'=>'_textsection_',
46 'Para'=>'_textparagraph_');
[1852]47
[9157]48our %wanted_index_files = ('td'=>1,
[932]49 't'=>1,
[1852]50 'tl'=>1,
51 'ti'=>1,
[932]52 'idb'=>1,
53 'ib1'=>1,
54 'ib2'=>1,
55 'ib3'=>1,
[12910]56 'ib4'=>1,
57 'ib5'=>1,
58 'ib6'=>1,
59 'ib7'=>1,
[932]60 'i'=>1,
[1852]61 'il'=>1,
62 'w'=>1,
[932]63 'wa'=>1);
64
65
[10468]66my $maxdocsize = $basebuilder::maxdocsize;
67
[932]68sub new {
[7953]69 my $class = shift(@_);
70
[10468]71 my $self = new basebuilder (@_);
72 $self = bless $self, $class;
[932]73
[17564]74 #$self->{'indexfieldmap'} = \%static_indexfield_map;
[6407]75
[1852]76 # get the levels (Section, Paragraph) for indexing and compression
77 $self->{'levels'} = {};
[4811]78 $self->{'levelorder'} = ();
[1852]79 if (defined $self->{'collect_cfg'}->{'levels'}) {
[8716]80 foreach my $level ( @{$self->{'collect_cfg'}->{'levels'}} ){
[4811]81 $level =~ tr/A-Z/a-z/;
[1852]82 $self->{'levels'}->{$level} = 1;
[4811]83 push (@{$self->{'levelorder'}}, $level);
[1852]84 }
[4811]85 } else { # default to document
86 $self->{'levels'}->{'document'} = 1;
87 push (@{$self->{'levelorder'}}, 'document');
88 }
89
[7953]90 $self->{'buildtype'} = "mgpp";
[932]91
92 return $self;
93}
94
[10468]95sub generate_index_list {
96 my $self = shift (@_);
97
98 # sort out the indexes
99 #indexes are specified with spaces, but we put them into one index
100 my $indexes = $self->{'collect_cfg'}->{'indexes'};
[19218]101 if (defined $indexes) {
102 $self->{'collect_cfg'}->{'indexes'} = [];
[24404]103
104 # remove any ex. from index spec but iff it is the only namespace in the metadata name
105 my @indexes_copy = @$indexes; # make a copy, as 'map' changes entry in array
106 #map { $_ =~ s/(^|,|;)ex\.([^.]+)$/$1$2/; } @indexes_copy; # No. Will replace metanames like flex.Image with fl.Image
107 map { $_ =~ s/(,|;)/$1 /g; } @indexes_copy; # introduce a space after every separator
108 map { $_ =~ s/(^| )ex\.([^.,:]+)(,|;|$)/$1$2$3/g; } @indexes_copy; # replace all <ex.> at start of metanames or <, ex.> when in a comma separated list
109 map { $_ =~ s/(,|:) /$1/g; } @indexes_copy; # remove space introduced after every separator
110 my $single_index = join(';', @indexes_copy).";";
111
[22352]112 push (@{$self->{'collect_cfg'}->{'indexes'}}, $single_index);
[19218]113 }
[932]114}
115
[12910]116sub generate_index_options {
117 my $self = shift (@_);
118
[17110]119 $self->SUPER::generate_index_options();
120
[12910]121 $self->{'casefold'} = 0;
122 $self->{'stem'} = 0;
123 $self->{'accentfold'} = 0;
124
125 if (!defined($self->{'collect_cfg'}->{'indexoptions'})) {
126 # just use default options
127 $self->{'casefold'} = 1;
128 $self->{'stem'} = 1;
129 $self->{'accentfold'} = 1;
130 } else {
131 foreach my $option (@{$self->{'collect_cfg'}->{'indexoptions'}}) {
132 if ($option =~ /stem/) {
133 $self->{'stem'} = 1;
134 } elsif ($option =~ /casefold/) {
135 $self->{'casefold'} = 1;
136 } elsif ($option =~ /accentfold/) {
137 $self->{'accentfold'} = 1;
138 }
139 }
140 }
141
142 # now we record this for the build cfg
143 $self->{'stemindexes'} = 0;
144 if ($self->{'casefold'}) {
145 $self->{'stemindexes'} += 1;
146 }
147 if ($self->{'stem'}) {
148 $self->{'stemindexes'} += 2;
149 }
150 if ($self->{'accentfold'}) {
151 $self->{'stemindexes'} += 4;
152 }
[13341]153
[12910]154}
155
[10468]156sub default_buildproc {
157 my $self = shift (@_);
158
159 return "mgppbuildproc";
[932]160}
161
162sub compress_text {
163
164 my $self = shift (@_);
[10961]165
166 # we don't do anything if we don't want compressed text
167 return if $self->{'no_text'};
168
[932]169 my ($textindex) = @_;
170
[2478]171 my $exedir = "$ENV{'GSDLHOME'}/bin/$ENV{'GSDLOS'}";
[932]172 my $exe = &util::get_os_exe ();
[2478]173 my $mgpp_passes_exe = &util::filename_cat($exedir, "mgpp_passes$exe");
174 my $mgpp_compression_dict_exe = &util::filename_cat($exedir, "mgpp_compression_dict$exe");
[1694]175 my $outhandle = $self->{'outhandle'};
[932]176
[12340]177 my $maxnumeric = $self->{'maxnumeric'};
[12325]178
[932]179 &util::mk_all_dir (&util::filename_cat($self->{'build_dir'}, "text"));
180
[15003]181 my $collect_tail = &util::get_dirsep_tail($self->{'collection'});
182 my $basefilename = &util::filename_cat("text",$collect_tail);
[2700]183 my $fulltextprefix = &util::filename_cat ($self->{'build_dir'}, $basefilename);
[7904]184
[15003]185 my $osextra = "";
[2478]186 if ($ENV{'GSDLOS'} =~ /^windows$/i) {
[3115]187 $fulltextprefix =~ s@/@\\@g;
[2478]188 }
[2700]189 else {
190 $osextra = " -d /";
191 }
[1852]192
193
[4811]194 # define the section names and possibly the doc name for mgpasses
[1852]195 # the compressor doesn't need to know about paragraphs - never want to
196 # retrieve them
[13590]197
198 # always use Doc and Sec levels
199 my $mgpp_passes_sections = "-J ". $level_map{"document"} ." -K " . $level_map{"section"} ." ";
[12911]200
[1694]201 print $outhandle "\n*** creating the compressed text\n" if ($self->{'verbosity'} >= 1);
[6407]202 print STDERR "<Stage name='CompressText'>\n" if $self->{'gli'};
[932]203
204 # collect the statistics for the text
[1694]205 # -b $maxdocsize sets the maximum document size to be 12 meg
[2478]206 print $outhandle "\n collecting text statistics (mgpp_passes -T1)\n" if ($self->{'verbosity'} >= 1);
[6407]207 print STDERR "<Phase name='CollectTextStats'/>\n" if $self->{'gli'};
[932]208
209 my ($handle);
210 if ($self->{'debug'}) {
[15715]211 $handle = *STDOUT;
212 }
213 else {
[2478]214 if (!-e "$mgpp_passes_exe" ||
[15715]215 !open($handle, "| mgpp_passes$exe -M $maxnumeric $mgpp_passes_sections -f \"$fulltextprefix\" -T1 $osextra")) {
[6407]216 print STDERR "<FatalError name='NoRunMGPasses'>\n</Stage>\n" if $self->{'gli'};
[2478]217 die "mgppbuilder::compress_text - couldn't run $mgpp_passes_exe\n";
[932]218 }
219 }
[13590]220
[15685]221 my $db_level = "section";
[9919]222
[932]223 $self->{'buildproc'}->set_output_handle ($handle);
224 $self->{'buildproc'}->set_mode ('text');
225 $self->{'buildproc'}->set_index ($textindex);
226 $self->{'buildproc'}->set_indexing_text (0);
[17564]227 #$self->{'buildproc'}->set_indexfieldmap ($self->{'indexfieldmap'});
[1852]228 $self->{'buildproc'}->set_levels ($self->{'levels'});
[15685]229 $self->{'buildproc'}->set_db_level ($db_level);
[932]230 $self->{'buildproc'}->reset();
231 &plugin::begin($self->{'pluginfo'}, $self->{'source_dir'},
232 $self->{'buildproc'}, $self->{'maxdocs'});
233 &plugin::read ($self->{'pluginfo'}, $self->{'source_dir'},
[16379]234 "", {}, {}, $self->{'buildproc'}, $self->{'maxdocs'}, 0, $self->{'gli'});
[932]235 &plugin::end($self->{'pluginfo'});
236
237 close ($handle) unless $self->{'debug'};
238
[2478]239 $self->print_stats();
240
[932]241 # create the compression dictionary
242 # the compression dictionary is built by assuming the stats are from a seed
243 # dictionary (-S), if a novel word is encountered it is spelled out (-H),
244 # and the resulting dictionary must be less than 5 meg with the most
245 # frequent words being put into the dictionary first (-2 -k 5120)
[1852]246 # note: these options are left over from mg version
[932]247 if (!$self->{'debug'}) {
[1694]248 print $outhandle "\n creating the compression dictionary\n" if ($self->{'verbosity'} >= 1);
[6407]249 print STDERR "<Phase name='CreatingCompress'/>\n" if $self->{'gli'};
[2478]250 if (!-e "$mgpp_compression_dict_exe") {
[6407]251 print STDERR "<FatalError name='NoRunMGCompress'/>\n</Stage>\n" if $self->{'gli'};
[2478]252 die "mgppbuilder::compress_text - couldn't run $mgpp_compression_dict_exe\n";
[932]253 }
[2700]254 system ("mgpp_compression_dict$exe -f \"$fulltextprefix\" -S -H -2 -k 5120 $osextra");
[932]255
256 if (!$self->{'debug'}) {
[2478]257 if (!-e "$mgpp_passes_exe" ||
[12325]258 !open ($handle, "| mgpp_passes$exe -M $maxnumeric $mgpp_passes_sections -f \"$fulltextprefix\" -T2 $osextra")) {
[6407]259 print STDERR "<FatalError name='NoRunMGPasses'/>\n</Stage>\n" if $self->{'gli'};
[2478]260 die "mgppbuilder::compress_text - couldn't run $mgpp_passes_exe\n";
[932]261 }
262 }
263 }
[6407]264 else {
265 print STDERR "<Phase name='SkipCreatingComp'/>\n" if $self->{'gli'};
266 }
[932]267
[22820]268 $self->{'buildproc'}->set_output_handle ($handle);
[932]269 $self->{'buildproc'}->reset();
[22820]270
[932]271 # compress the text
[2478]272 print $outhandle "\n compressing the text (mgpp_passes -T2)\n" if ($self->{'verbosity'} >= 1);
[6407]273 print STDERR "<Phase name='CompressingText'/>\n" if $self->{'gli'};
274
[932]275 &plugin::read ($self->{'pluginfo'}, $self->{'source_dir'},
[16379]276 "", {}, {}, $self->{'buildproc'}, $self->{'maxdocs'}, 0, $self->{'gli'});
[932]277 close ($handle) unless $self->{'debug'};
[1694]278
279 $self->print_stats();
[6407]280 print STDERR "</Stage>\n" if $self->{'gli'};
[932]281}
282
283
[10468]284sub build_indexes_extra {
285 my $self = shift(@_);
[5617]286 #define the final field lists
287 $self->make_final_field_list();
[10468]288}
[5617]289
[932]290# creates directory names for each of the index descriptions
291sub create_index_mapping {
292 my $self = shift (@_);
293 my ($indexes) = @_;
294
295 my %mapping = ();
[5935]296
[19218]297 return \%mapping if !(scalar @$indexes);
298
[932]299 $mapping{'indexmaporder'} = [];
300 $mapping{'subcollectionmaporder'} = [];
301 $mapping{'languagemaporder'} = [];
302
303 # dirnames is used to check for collisions. Start this off
304 # with the manditory directory names
305 my %dirnames = ('text'=>'text',
306 'extra'=>'extra');
[8716]307 my %pnames = ('index' => {}, 'subcollection' => {}, 'languages' => {});
[932]308
[8716]309 foreach my $index (@$indexes) {
[932]310 my ($fields, $subcollection, $languages) = split (":", $index);
[13590]311
312 # we only ever have one index, and its called 'idx'
[8716]313 my $pindex = 'idx';
[4768]314
[932]315 # next comes a processed version of the subcollection if there is one.
316 my $psub = $self->process_field ($subcollection);
317 $psub = lc ($psub);
318
319 # next comes a processed version of the language if there is one.
320 my $plang = $self->process_field ($languages);
321 $plang = lc ($plang);
322
323 my $dirname = $pindex . $psub . $plang;
324
325 # check to be sure all index names are unique
326 while (defined ($dirnames{$dirname})) {
327 $dirname = $self->make_unique (\%pnames, $index, \$pindex, \$psub, \$plang);
328 }
329
[2478]330 $mapping{$index} = $dirname;
331
[932]332 # store the mapping orders as well as the maps
[2478]333 # also put index, subcollection and language fields into the mapping thing -
[4794]334 # (the full index name (eg text:subcol:lang) is not used on
[2478]335 # the query page) -these are used for collectionmeta later on
[932]336 if (!defined $mapping{'indexmap'}{"$fields"}) {
337 $mapping{'indexmap'}{"$fields"} = $pindex;
338 push (@{$mapping{'indexmaporder'}}, "$fields");
[2478]339 if (!defined $mapping{"$fields"}) {
340 $mapping{"$fields"} = $pindex;
341 }
[932]342 }
343 if ($psub =~ /\w/ && !defined ($mapping{'subcollectionmap'}{$subcollection})) {
344 $mapping{'subcollectionmap'}{$subcollection} = $psub;
345 push (@{$mapping{'subcollectionmaporder'}}, $subcollection);
[2478]346 $mapping{$subcollection} = $psub;
[932]347 }
348 if ($plang =~ /\w/ && !defined ($mapping{'languagemap'}{$languages})) {
349 $mapping{'languagemap'}{$languages} = $plang;
[6544]350 push (@{$mapping{'languagemaporder'}}, $languages);
[2478]351 $mapping{$languages} = $plang;
[932]352 }
353 $dirnames{$dirname} = $index;
[8716]354 $pnames{'index'}->{$pindex} = "$fields";
355 $pnames{'subcollection'}->{$psub} = $subcollection;
356 $pnames{'languages'}->{$plang} = $languages;
[932]357 }
358
359 return \%mapping;
360}
361
362sub make_unique {
363 my $self = shift (@_);
364 my ($namehash, $index, $indexref, $subref, $langref) = @_;
365 my ($fields, $subcollection, $languages) = split (":", $index);
366
367 if ($namehash->{'index'}->{$$indexref} ne "$fields") {
368 $self->get_next_version ($indexref);
369 } elsif ($namehash->{'subcollection'}->{$$subref} ne $subcollection) {
370 $self->get_next_version ($subref);
371 } elsif ($namehash->{'languages'}->{$$langref} ne $languages) {
372 $self->get_next_version ($langref);
373 }
374 return "$$indexref$$subref$$langref";
375}
376
377
378sub build_index {
379 my $self = shift (@_);
380 my ($index) = @_;
[1694]381 my $outhandle = $self->{'outhandle'};
[932]382
383 # get the full index directory path and make sure it exists
384 my $indexdir = $self->{'index_mapping'}->{$index};
385 &util::mk_all_dir (&util::filename_cat($self->{'build_dir'}, $indexdir));
[15003]386
387 my $collect_tail = &util::get_dirsep_tail($self->{'collection'});
[2700]388 my $fullindexprefix = &util::filename_cat ($self->{'build_dir'},
389 $indexdir,
[15003]390 $collect_tail);
[2700]391 my $fulltextprefix = &util::filename_cat ($self->{'build_dir'}, "text",
[15003]392 $collect_tail);
[932]393
394 # get any os specific stuff
[2478]395 my $exedir = "$ENV{'GSDLHOME'}/bin/$ENV{'GSDLOS'}";
[932]396
397 my $exe = &util::get_os_exe ();
[2478]398 my $mgpp_passes_exe = &util::filename_cat($exedir, "mgpp_passes$exe");
[1852]399
400 # define the section names for mgpasses
[13590]401 my $mgpp_passes_sections = "-J ". $level_map{"document"} ." -K " . $level_map{"section"} ." ";
402 if ($self->{'levels'}->{'paragraph'}) {
403 $mgpp_passes_sections .= "-K " . $level_map{'paragraph'}. " ";
[1852]404 }
405
[2478]406 my $mgpp_perf_hash_build_exe =
407 &util::filename_cat($exedir, "mgpp_perf_hash_build$exe");
408 my $mgpp_weights_build_exe =
409 &util::filename_cat ($exedir, "mgpp_weights_build$exe");
410 my $mgpp_invf_dict_exe =
411 &util::filename_cat ($exedir, "mgpp_invf_dict$exe");
412 my $mgpp_stem_idx_exe =
413 &util::filename_cat ($exedir, "mgpp_stem_idx$exe");
[932]414
[12340]415 my $maxnumeric = $self->{'maxnumeric'};
[12325]416
417 my $osextra = "";
[2700]418 if ($ENV{'GSDLOS'} =~ /^windows$/i) {
[3115]419 $fullindexprefix =~ s@/@\\@g;
[2700]420 } else {
421 $osextra = " -d /";
[3115]422 if ($outhandle ne "STDERR") {
423 # so mgpp_passes doesn't print to stderr if we redirect output
424 $osextra .= " 2>/dev/null";
425 }
[2478]426 }
[2700]427
[932]428 # get the index expression if this index belongs
429 # to a subcollection
430 my $indexexparr = [];
[9669]431 my $langarr = [];
[2478]432 # there may be subcollection info, and language info.
433 my ($fields, $subcollection, $language) = split (":", $index);
[932]434 my @subcollections = ();
435 @subcollections = split /,/, $subcollection if (defined $subcollection);
436
437 foreach $subcollection (@subcollections) {
438 if (defined ($self->{'collect_cfg'}->{'subcollection'}->{$subcollection})) {
439 push (@$indexexparr, $self->{'collect_cfg'}->{'subcollection'}->{$subcollection});
440 }
441 }
442
443 # add expressions for languages if this index belongs to
[2478]444 # a language subcollection - only put languages expressions for the
445 # ones we want in the index
[6544]446
[2478]447 my @languages = ();
[20418]448 my $languagemetadata = "Language";
449 if (defined ($self->{'collect_cfg'}->{'languagemetadata'})) {
450 $languagemetadata = $self->{'collect_cfg'}->{'languagemetadata'};
[9548]451 }
[2478]452 @languages = split /,/, $language if (defined $language);
[9548]453 foreach my $language (@languages) {
[2478]454 my $not=0;
[932]455 if ($language =~ s/^\!//) {
[2478]456 $not = 1;
[932]457 }
[9548]458 if($not) {
[9669]459 push (@$langarr, "!$language");
[6544]460 } else {
[9669]461 push (@$langarr, "$language");
[2478]462 }
[932]463 }
464
465 # Build index dictionary. Uses verbatim stem method
[2478]466 print $outhandle "\n creating index dictionary (mgpp_passes -I1)\n" if ($self->{'verbosity'} >= 1);
[6407]467 print STDERR "<Phase name='CreatingIndexDic'/>\n" if $self->{'gli'};
[932]468 my ($handle);
469 if ($self->{'debug'}) {
[15715]470 $handle = *STDOUT;
471 }
472 else {
[2478]473 if (!-e "$mgpp_passes_exe" ||
[15715]474 !open($handle, "| mgpp_passes$exe -M $maxnumeric $mgpp_passes_sections -f \"$fullindexprefix\" -I1 $osextra")) {
[6407]475 print STDERR "<FatalError name='NoRunMGPasses'/>\n</Stage>\n" if $self->{'gli'};
[2478]476 die "mgppbuilder::build_index - couldn't run $mgpp_passes_exe\n";
[932]477 }
478 }
[9919]479
[15685]480 # db_level is always section
481 my $db_level = "section";
[9919]482
[4794]483 # set up the document processr
[932]484 $self->{'buildproc'}->set_output_handle ($handle);
485 $self->{'buildproc'}->set_mode ('text');
486 $self->{'buildproc'}->set_index ($index, $indexexparr);
[20418]487 $self->{'buildproc'}->set_index_languages ($languagemetadata, $langarr) if (defined $language);
[932]488 $self->{'buildproc'}->set_indexing_text (1);
[17564]489 #$self->{'buildproc'}->set_indexfieldmap ($self->{'indexfieldmap'});
[9919]490 $self->{'buildproc'}->set_levels ($self->{'levels'});
[15685]491 $self->{'buildproc'}->set_db_level ($db_level);
[9919]492
[932]493 $self->{'buildproc'}->reset();
[22820]494
[932]495 &plugin::read ($self->{'pluginfo'}, $self->{'source_dir'},
[16379]496 "", {}, {}, $self->{'buildproc'}, $self->{'maxdocs'}, 0, $self->{'gli'});
[932]497 close ($handle) unless $self->{'debug'};
498
[1694]499 $self->print_stats();
500
[5768]501 # 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.
502 # we check on the .id file - index dictionary
503 my $dict_file = "$fullindexprefix.id";
504 if (!-e $dict_file) {
505 print $outhandle "mgppbuilder::build_index - Couldn't create index $index\n";
[6407]506 print STDERR "<Warning name='NoIndex'/>\n</Stage>\n" if $self->{'gli'};
[5768]507 $self->{'notbuilt'}->{$index}=1;
508 return;
509 }
510
[932]511 if (!$self->{'debug'}) {
512 # create the perfect hash function
[2478]513 if (!-e "$mgpp_perf_hash_build_exe") {
[6407]514 print STDERR "<FatalError name='NoRunMGHash'/>\n</Stage>\n" if $self->{'gli'};
[2478]515 die "mgppbuilder::build_index - couldn't run $mgpp_perf_hash_build_exe\n";
[932]516 }
[2700]517 system ("mgpp_perf_hash_build$exe -f \"$fullindexprefix\" $osextra");
[932]518
[2478]519 if (!-e "$mgpp_passes_exe" ||
[12325]520 !open ($handle, "| mgpp_passes$exe -M $maxnumeric $mgpp_passes_sections -f \"$fullindexprefix\" -I2 $osextra")) {
[6407]521 print STDERR "<FatalError name='NoRunMGPasses'/>\n</Stage>\n" if $self->{'gli'};
[2478]522 die "mgppbuilder::build_index - couldn't run $mgpp_passes_exe\n";
[932]523 }
524 }
525
526 # invert the text
[2478]527 print $outhandle "\n inverting the text (mgpp_passes -I2)\n" if ($self->{'verbosity'} >= 1);
[6407]528 print STDERR "<Phase name='InvertingText'/>\n" if $self->{'gli'};
[22820]529
530 $self->{'buildproc'}->set_output_handle ($handle);
[932]531 $self->{'buildproc'}->reset();
[22820]532
[932]533 &plugin::read ($self->{'pluginfo'}, $self->{'source_dir'},
[16379]534 "", {}, {}, $self->{'buildproc'}, $self->{'maxdocs'}, 0, $self->{'gli'});
[1694]535
536 $self->print_stats ();
[932]537
538 if (!$self->{'debug'}) {
539
540 close ($handle);
541
542 # create the weights file
[1694]543 print $outhandle "\n create the weights file\n" if ($self->{'verbosity'} >= 1);
[6407]544 print STDERR "<Phase name='CreateTheWeights'/>\n" if $self->{'gli'};
[2478]545 if (!-e "$mgpp_weights_build_exe") {
[6407]546 print STDERR "<FatalError name='NoRunMGWeights'/>\n</Stage>\n" if $self->{'gli'};
[2478]547 die "mgppbuilder::build_index - couldn't run $mgpp_weights_build_exe\n";
[932]548 }
[2700]549 system ("mgpp_weights_build$exe -f \"$fullindexprefix\" $osextra");
[932]550
551 # create 'on-disk' stemmed dictionary
[1694]552 print $outhandle "\n creating 'on-disk' stemmed dictionary\n" if ($self->{'verbosity'} >= 1);
[2478]553 if (!-e "$mgpp_invf_dict_exe") {
[6407]554 print STDERR "<FatalError name='NoRunMGInvf'/>\n</Stage>\n" if $self->{'gli'};
[2478]555 die "mgppbuilder::build_index - couldn't run $mgpp_invf_dict_exe\n";
[932]556 }
[2700]557 system ("mgpp_invf_dict$exe -f \"$fullindexprefix\" $osextra" );
[932]558
559
560 # creates stem index files for the various stemming methods
[1694]561 print $outhandle "\n creating stem indexes\n" if ($self->{'verbosity'} >= 1);
[6407]562 print STDERR "<Phase name='CreatingStemIndx'/>\n" if $self->{'gli'};
[2478]563 if (!-e "$mgpp_stem_idx_exe") {
[6407]564 print STDERR "<FatalError name='NoRunMGStem'/>\n</Stage>\n" if $self->{'gli'};
[2478]565 die "mgppbuilder::build_index - couldn't run $mgpp_stem_idx_exe\n";
[932]566 }
[12910]567 my $accent_folding_enabled = 1;
568 if ($self->{'accentfold'}) {
569 # the first time we do this, we test for accent folding enabled
[13813]570 if (system ("mgpp_stem_idx$exe -b 4096 -s4 -f \"$fullindexprefix\" $osextra") == 2) {
[12910]571 # accent folding has not been enabled in mgpp
572 $accent_folding_enabled = 0;
573 $self->{'stemindexes'} -= 4;
574 }
575 }
576 if ($self->{'casefold'}) {
577 system ("mgpp_stem_idx$exe -b 4096 -s1 -f \"$fullindexprefix\" $osextra");
578 if ($accent_folding_enabled && $self->{'accentfold'}) {
579 system ("mgpp_stem_idx$exe -b 4096 -s5 -f \"$fullindexprefix\" $osextra");
580 }
581 }
582 if ($self->{'stem'}) {
583 system ("mgpp_stem_idx$exe -b 4096 -s2 -f \"$fullindexprefix\" $osextra");
584 if ($accent_folding_enabled && $self->{'accentfold'}) {
585 system ("mgpp_stem_idx$exe -b 4096 -s6 -f \"$fullindexprefix\" $osextra");
586 }
587 }
588 if ($self->{'casefold'} && $self->{'stem'}) {
589 system ("mgpp_stem_idx$exe -b 4096 -s3 -f \"$fullindexprefix\" $osextra");
590 if ($accent_folding_enabled && $self->{'accentfold'}) {
591 system ("mgpp_stem_idx$exe -b 4096 -s7 -f \"$fullindexprefix\" $osextra");
592 }
593 }
594
[932]595 # remove unwanted files
[1852]596 my $tmpdir = &util::filename_cat ($self->{'build_dir'}, $indexdir);
597 opendir (DIR, $tmpdir) || die
598 "mgppbuilder::build_index - couldn't read directory $tmpdir\n";
[8716]599 foreach my $file (readdir(DIR)) {
[1852]600 next if $file =~ /^\./;
601 my ($suffix) = $file =~ /\.([^\.]+)$/;
602 if (defined $suffix && !defined $wanted_index_files{$suffix}) {
[932]603 # delete it!
[1852]604 print $outhandle "deleting $file\n" if $self->{'verbosity'} > 2;
[2772]605 #&util::rm (&util::filename_cat ($tmpdir, $file));
[1852]606 }
607 }
608 closedir (DIR);
[4794]609 }
[6407]610 print STDERR "</Stage>\n" if $self->{'gli'};
[932]611}
612
[15709]613
614sub get_collection_meta_indexes
615{
[10468]616 my $self = shift(@_);
[15709]617 my $collection_infodb = shift(@_);
[1694]618
[10477]619 # define the indexed field mapping if not already done so (ie if infodb called separately from build_index)
620 if (!defined $self->{'build_cfg'}) {
621 $self->read_final_field_list();
622 }
623
[4794]624 # first do the collection meta stuff - everything without a dot
625 my $collmetadefined = 0;
[10468]626 my $metadata_entry;
[932]627 if (defined $self->{'collect_cfg'}->{'collectionmeta'}) {
[4794]628 $collmetadefined = 1;
629 }
[11965]630
[4811]631 #add the index field macros to [collection]
[4794]632 # eg <TI>Title
633 # <SU>Subject
[17565]634 # these now come from collection meta. if that is not defined, uses the metadata name
[8716]635 my $collmeta = "";
[19218]636 if (defined $self->{'build_cfg'}->{'indexfields'}) {
637 foreach my $longfield (@{$self->{'build_cfg'}->{'indexfields'}}){
638 my $shortfield = $self->{'buildproc'}->{'indexfieldmap'}->{$longfield};
639 next if $shortfield eq 1;
640
641 # we need to check if some coll meta has been defined - don't output
642 # any that have
643 $collmeta = ".$longfield";
644 if (!$collmetadefined || !defined $self->{'collect_cfg'}->{'collectionmeta'}->{$collmeta}) {
645 if ($longfield eq "allfields") {
646 $collection_infodb->{$shortfield} = [ "_query:textallfields_" ];
647 } elsif ($longfield eq "text") {
648 $collection_infodb->{$shortfield} = [ "_query:texttextonly_" ];
649 } else {
650 $collection_infodb->{$shortfield} = [ $longfield ];
651 }
[4794]652 }
[932]653 }
654 }
[19218]655
[4811]656 # now add the level names
[8716]657 my $level_entry = "";
658 foreach my $level (@{$self->{'collect_cfg'}->{'levels'}}) {
659 $collmeta = ".$level"; # based on the original specification
[4811]660 $level =~ tr/A-Z/a-z/; # make it lower case
[7090]661 my $levelid = $level_map{$level}; # find the actual value we used in the index
[11965]662 if (!$collmetadefined || !defined $self->{'collect_cfg'}->{'collectionmeta'}->{$collmeta}) {
[4811]663 # use the default macro
[15709]664 $collection_infodb->{$levelid} = [ $level_map{$levelid} ];
[4811]665 }
666 }
[5935]667
668 # now add subcoll meta
[8716]669 my $subcoll_entry = "";
670 my $shortname = "";
671 my $one_entry = "";
672 foreach my $subcoll (@{$self->{'index_mapping'}->{'subcollectionmaporder'}}) {
[11965]673 $shortname = $self->{'index_mapping'}->{$subcoll};
674 if (!$collmetadefined || !defined $self->{'collect_cfg'}->{'collectionmeta'}->{".$subcoll"}) {
[15709]675 $collection_infodb->{$shortname} = [ $subcoll ];
[5935]676 }
677 }
[10158]678
679 # now add language meta
[8716]680 my $lang_entry = "";
681 foreach my $lang (@{$self->{'index_mapping'}->{'languagemaporder'}}) {
[11965]682 $shortname = $self->{'index_mapping'}->{$lang};
683 if (!$collmetadefined || !defined $self->{'collect_cfg'}->{'collectionmeta'}->{".$lang"}) {
[15709]684 $collection_infodb->{$shortname} = [ $lang ];
[6544]685 }
686 }
[15709]687}
[932]688
[15709]689
690# default is to output the metadata sets (prefixes) used in collection
691sub output_collection_meta
692{
693 my $self = shift(@_);
694 my $infodb_handle = shift(@_);
695
696 my %collection_infodb = ();
697 $self->get_collection_meta_sets(\%collection_infodb);
698 $self->get_collection_meta_indexes(\%collection_infodb);
[15725]699 &dbutil::write_infodb_entry($self->{'infodbtype'}, $infodb_handle, "collection", \%collection_infodb);
[932]700}
[7150]701
[15709]702
[10158]703# at the end of building, we have an indexfieldmap with all the mappings,
704# plus some extras, and indexmap with any indexes in it that weren't
705# specified in the index definition. we want to make an ordered list of
706# fields that are indexed, and a list of mappings that are used. this will
707# be used for the build.cfg file, and for collection meta definition we
708# store these in a build.cfg bit
[4794]709sub make_final_field_list {
710 my $self = shift (@_);
711
712 $self->{'build_cfg'} = {};
[10158]713
[4794]714 # store the indexfieldmap information
715 my @indexfieldmap = ();
716 my @indexfields = ();
717 my $specifiedfields = {};
718 my @specifiedfieldorder = ();
[10158]719
720 # go through the index definition and add each thing to a map, so we
721 # can easily check if it is already specified - when doing the
722 # metadata, we print out all the individual fields, but some may
723 # already be specified in the index definition, so we dont want to add
724 # those again.
725
[10468]726 my $field;
727 foreach $field (@{$self->{'collect_cfg'}->{'indexes'}}) {
[5617]728 # remove subcoll stuff
729 my $parts = $field;
730 $parts =~ s/:.*$//;
[10961]731 # *************
732 my @fs = split(';', $parts);
[8716]733 foreach my $f(@fs) {
[5617]734 if (!defined $specifiedfields->{$f}) {
735 $specifiedfields->{$f}=1;
736 push (@specifiedfieldorder, "$f");
737 }
[4794]738 }
739 }
[5643]740
[4794]741 #add all fields bit
[17574]742 my $ifm = $self->{'buildproc'}->{'indexfieldmap'};
743
[10468]744 foreach $field (@specifiedfieldorder) {
[4794]745 if ($field eq "metadata") {
[8716]746 foreach my $newfield (keys %{$self->{'buildproc'}->{'indexfields'}}) {
[4794]747 if (!defined $specifiedfields->{$newfield}) {
748 push (@indexfieldmap, "$newfield\-\>$self->{'buildproc'}->{'indexfieldmap'}->{$newfield}");
749 push (@indexfields, "$newfield");
750 }
751 }
752
753 } elsif ($field eq 'text') {
754 push (@indexfieldmap, "text\-\>TX");
755 push (@indexfields, "text");
756 } elsif ($field eq 'allfields') {
757 push (@indexfieldmap, "allfields\-\>ZZ");
758 push (@indexfields, "allfields");
759 } else {
[17574]760 # we only add in the ones that have been processed
[11996]761 if (defined $ifm->{$field}) {
762 push (@indexfieldmap, "$field\-\>$ifm->{$field}");
763 push (@indexfields, "$field");
764 }
765
[4794]766
767 }
768 }
[10158]769
[19218]770 if (scalar @indexfieldmap) {
771 $self->{'build_cfg'}->{'indexfieldmap'} = \@indexfieldmap;
772 }
773 if (scalar @indexfields) {
774 $self->{'build_cfg'}->{'indexfields'} = \@indexfields;
775 }
[4794]776}
777
778
[10158]779# recreate the field list from the build.cfg file, look first in building,
780# then in index to find it. if there is no build.cfg, we can't do the field
781# list (there is unlikely to be any index anyway.)
[4794]782sub read_final_field_list {
783 my $self = shift (@_);
784 $self->{'build_cfg'} = {};
785 my @indexfieldmap = ();
786 my @indexfields = ();
[14666]787 my @indexmap = ();
788
[4794]789 # we read the stuff in from the build.cfg file - if its there
[17574]790 my $buildcfg = $self->read_build_cfg();
791 return unless defined $buildcfg;
[10158]792
[10468]793 my $field;
[4794]794 if (defined $buildcfg->{'indexfields'}) {
[10468]795 foreach $field (@{$buildcfg->{'indexfields'}}) {
[4794]796 push (@indexfields, "$field");
797 }
798 }
[10158]799
[4794]800 if (defined $buildcfg->{'indexfieldmap'}) {
[10468]801 foreach $field (@{$buildcfg->{'indexfieldmap'}}) {
[4794]802 push (@indexfieldmap, "$field");
[8716]803 my ($f, $v) = $field =~ /^(.*)\-\>(.*)$/;
[4794]804 $self->{'buildproc'}->{'indexfieldmap'}->{$f} = $v;
805 }
806 }
[10158]807
[14666]808 if (defined $buildcfg->{'indexmap'}) {
809 foreach $field (@{$buildcfg->{'indexmap'}}) {
810 push (@indexmap, "$field");
811 }
812 }
813
[4794]814 $self->{'build_cfg'}->{'indexfieldmap'} = \@indexfieldmap;
815 $self->{'build_cfg'}->{'indexfields'} = \@indexfields;
[14666]816 $self->{'build_cfg'}->{'indexmap'} = \@indexmap;
[4794]817}
[10158]818
[10468]819
820sub build_cfg_extra {
[932]821 my $self = shift (@_);
[10468]822 my ($build_cfg) = @_;
823
824 $build_cfg->{'numsections'} = $self->{'buildproc'}->get_num_sections();
[4794]825
[4811]826 # store the level info
827 my @indexlevels = ();
[9936]828 my @levelmap = ();
[8716]829 foreach my $l (@{$self->{'levelorder'}}) {
[7090]830 push (@indexlevels, $level_map{$l});
[9936]831 push (@levelmap, "$l\-\>$level_map{$l}");
[4811]832 }
833 $build_cfg->{'indexlevels'} = \@indexlevels;
[9936]834 $build_cfg->{'levelmap'} = \@levelmap;
835
[15687]836 # text level (and database level) is always section
[13590]837 $build_cfg->{'textlevel'} = $level_map{'section'};
[10468]838
[932]839}
840
8411;
842
843
Note: See TracBrowser for help on using the repository browser.