source: trunk/gsdl/perllib/mgppbuilder.pm@ 13341

Last change on this file since 13341 was 13341, checked in by kjdon, 17 years ago

removed a comment

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