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