source: trunk/gsdl/perllib/mgppbuildproc.pm@ 1838

Last change on this file since 1838 was 1772, checked in by kjm18, 24 years ago

removed Paragraph stuff - now only has Document and Section; added </Document>

tags at end of docs; the metadata fields that are indexed are written to

indexfields line in build.cfg

  • Property svn:keywords set to Author Date Id Revision
File size: 13.7 KB
Line 
1###########################################################################
2#
3# mgbuildproc.pm --
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# This document processor outputs a document
27# for mg to process
28
29
30package mgppbuildproc;
31
32use classify;
33use doc;
34use docproc;
35use util;
36
37
38BEGIN {
39 @ISA = ('docproc');
40}
41
42
43sub new {
44 my ($class, $collection, $source_dir, $build_dir,
45 $verbosity, $outhandle) = @_;
46 my $self = new docproc ();
47
48 # outhandle is where all the debugging info goes
49 # output_handle is where the output of the plugins is piped
50 # to (i.e. mg, gdbm etc.)
51 $outhandle = STDERR unless defined $outhandle;
52
53 $self->{'collection'} = $collection;
54 $self->{'source_dir'} = $source_dir;
55 $self->{'build_dir'} = $build_dir;
56 $self->{'verbosity'} = $verbosity;
57 $self->{'classifiers'} = [];
58 $self->{'mode'} = "text";
59 $self->{'assocdir'} = $build_dir;
60 $self->{'dontgdbm'} = {};
61 $self->{'index'} = "text";
62 $self->{'indexexparr'} = [];
63 $self->{'output_handle'} = "STDOUT";
64 $self->{'num_docs'} = 0;
65 $self->{'num_sections'} = 0;
66 $self->{'num_bytes'} = 0;
67 $self->{'num_processed_bytes'} = 0;
68 $self->{'outhandle'} = $outhandle;
69
70 $self->{'indexing_text'} = 0;
71 $self->{'indexfields'} = {};
72
73 return bless $self, $class;
74}
75
76sub reset {
77 my $self = shift (@_);
78
79 $self->{'num_docs'} = 0;
80 $self->{'num_sections'} = 0;
81 $self->{'num_processed_bytes'} = 0;
82 $self->{'num_bytes'} = 0;
83}
84
85sub get_num_docs {
86 my $self = shift (@_);
87
88 return $self->{'num_docs'};
89}
90
91sub get_num_sections {
92 my $self = shift (@_);
93
94 return $self->{'num_sections'};
95}
96
97# num_bytes is the actual number of bytes in the collection
98# this is normally the same as what's processed during text compression
99sub get_num_bytes {
100 my $self = shift (@_);
101
102 return $self->{'num_bytes'};
103}
104
105# num_processed_bytes is the number of bytes actually passed
106# to mgpp for the current index
107sub get_num_processed_bytes {
108 my $self = shift (@_);
109
110 return $self->{'num_processed_bytes'};
111}
112
113sub set_output_handle {
114 my $self = shift (@_);
115 my ($handle) = @_;
116
117 $self->{'output_handle'} = $handle;
118}
119
120sub set_mode {
121 my $self = shift (@_);
122 my ($mode) = @_;
123
124 $self->{'mode'} = $mode;
125}
126
127sub set_assocdir {
128 my $self = shift (@_);
129 my ($assocdir) = @_;
130
131 $self->{'assocdir'} = $assocdir;
132}
133
134sub set_dontgdbm {
135 my $self = shift (@_);
136 my ($dontgdbm) = @_;
137
138 $self->{'dontgdbm'} = $dontgdbm;
139}
140
141sub set_index {
142 my $self = shift (@_);
143 my ($index, $indexexparr) = @_;
144
145 $self->{'index'} = $index;
146 $self->{'indexexparr'} = $indexexparr if defined $indexexparr;
147}
148
149sub get_index {
150 my $self = shift (@_);
151
152 return $self->{'index'};
153}
154
155sub set_classifiers {
156 my $self = shift (@_);
157 my ($classifiers) = @_;
158
159 $self->{'classifiers'} = $classifiers;
160}
161
162sub set_indexing_text {
163 my $self = shift (@_);
164 my ($indexing_text) = @_;
165
166 $self->{'indexing_text'} = $indexing_text;
167}
168
169sub get_indexing_text {
170 my $self = shift (@_);
171
172 return $self->{'indexing_text'};
173}
174
175sub process {
176 my $self = shift (@_);
177 my $method = $self->{'mode'};
178
179 $self->$method(@_);
180}
181
182# use 'Paged' if document has no more than 2 levels
183# and each section at second level has a number for
184# Title metadata
185sub get_document_type {
186 my $self = shift (@_);
187 my ($doc_obj) = @_;
188
189 my $thistype = "VList";
190 my $childtype = "VList";
191 my $title;
192 my @tmp = ();
193
194 my $section = $doc_obj->get_top_section ();
195 my $first = 1;
196 while (defined $section) {
197 @tmp = split /\./, $section;
198 if (scalar(@tmp) > 1) {
199 return ($thistype, $childtype);
200 }
201 if (!$first) {
202 $title = $doc_obj->get_metadata_element ($section, "Title");
203 if (!defined $title || $title !~ /^\d+$/) {
204 return ($thistype, $childtype);
205 }
206 }
207 $first = 0;
208 $section = $doc_obj->get_next_section($section);
209 }
210 if ($doc_obj->get_text_length ($doc_obj->get_top_section())) {
211 $thistype = "Paged";
212 } else {
213 $thistype = "Invisible";
214 }
215 $childtype = "Paged";
216 return ($thistype, $childtype);
217}
218
219sub assoc_files {
220 my $self = shift (@_);
221 my ($doc_obj, $archivedir) = @_;
222 my ($afile);
223
224 foreach my $assoc_file (@{$doc_obj->get_assoc_files()}) {
225 # if assoc file contains directory structure of
226 # its own use it, otherwise use HASH... directory
227 if ($assoc_file->[1] =~ /[\/\\]/) {
228 $afile = &util::filename_cat($self->{'assocdir'}, $assoc_file->[1]);
229 } else {
230 $afile = &util::filename_cat($self->{'assocdir'}, $archivedir, $assoc_file->[1]);
231 }
232 &util::hard_link ($assoc_file->[0], $afile);
233 }
234}
235
236sub infodb {
237 my $self = shift (@_);
238 my ($doc_obj, $filename) = @_;
239 my $handle = $self->{'output_handle'};
240# $handle = "main::STDOUT";
241
242 my $doctype = $doc_obj->get_doc_type();
243
244 # only output this document if it is one to be indexed
245 return if ($doctype ne "indexed_doc");
246
247 my ($archivedir) = $filename =~ /^(.*?)(?:\/|\\)[^\/\\]*$/;
248 $archivedir = "" unless defined $archivedir;
249 $archivedir =~ s/\\/\//g;
250 $archivedir =~ s/^\/+//;
251 $archivedir =~ s/\/+$//;
252
253 $self->assoc_files ($doc_obj, $archivedir);
254
255 # this is another document
256 $self->{'num_docs'} += 1 unless ($doctype eq "classification");
257
258 # is this a paged or a hierarchical document
259 my ($thistype, $childtype) = $self->get_document_type ($doc_obj);
260
261 my $section = $doc_obj->get_top_section ();
262 my $doc_OID = $doc_obj->get_OID();
263 my $first = 1;
264 my $url = "";
265 while (defined $section) {
266 # update a few statistics
267 $self->{'num_bytes'} += $doc_obj->get_text_length ($section);
268 $self->{'num_sections'} += 1 unless ($doctype eq "classification");
269
270 # output the section name
271 if ($section eq "") { print $handle "[$doc_OID]\n"; }
272 else { print $handle "[$doc_OID.$section]\n"; }
273
274 # output the fact that this document is a document (unless doctype
275 # has been set to something else from within a plugin
276 my $dtype = $doc_obj->get_metadata_element ($section, "doctype");
277 if (!defined $dtype || $dtype !~ /\w/) {
278 print $handle "<doctype>doc\n";
279 }
280
281 # output whether this node contains text
282 if ($doc_obj->get_text_length($section) > 0) {
283 print $handle "<hastxt>1\n";
284 } else {
285 print $handle "<hastxt>0\n";
286 }
287
288 # output all the section metadata
289 #my $found_doctype = 0;
290 my $metadata = $doc_obj->get_all_metadata ($section);
291 foreach $pair (@$metadata) {
292 my ($field, $value) = (@$pair);
293
294 #$found_doctype = 1 if $field eq "doctype";
295 if ($field ne "Identifier" && $field !~ /^gsdl/ &&
296 defined $value && $value ne "") {
297
298 # escape problematic stuff
299 $value =~ s/\\/\\\\/g;
300 $value =~ s/\n/\\n/g;
301 $value =~ s/\r/\\r/g;
302
303 # special case for URL metadata
304 if ($field =~ /^URL$/i) {
305 $url .= "[$value]\n";
306 if ($section eq "") {$url .= "<section>$doc_OID\n";}
307 else {$url .= "<section>$doc_OID.$section\n";}
308 $url .= '-' x 70 . "\n";
309 }
310
311 if (!defined $self->{'dontgdbm'}->{$field}) {
312 print $handle "<$field>$value\n";
313 }
314 }
315 }
316
317 # output the fact that this document is a document
318 # (unless doctype was already output as part of
319 # metadata)
320 #if (!$found_doctype && !defined $self->{'dontgdbm'}->{'doctype'}) {
321 # print $handle "<doctype>doc\n";
322 #}
323
324
325
326 # output archivedir if at top level
327 if ($section eq $doc_obj->get_top_section()) {
328 print $handle "<archivedir>$archivedir\n";
329 }
330
331 # output document display type
332 if ($first) {
333 print $handle "<thistype>$thistype\n";
334 }
335
336 # output a list of children
337 my $children = $doc_obj->get_children ($section);
338 if (scalar(@$children) > 0) {
339 print $handle "<childtype>$childtype\n";
340 print $handle "<contains>";
341 my $firstchild = 1;
342 foreach $child (@$children) {
343 print $handle ";" unless $firstchild;
344 $firstchild = 0;
345 if ($child =~ /^.*?\.(\d+)$/) {
346 print $handle "\".$1";
347 } else {
348 print $handle "\".$child";
349 }
350# if ($child eq "") { print $handle "$doc_OID"; }
351# elsif ($section eq "") { print $handle "$doc_OID.$child"; }
352# else { print $handle "$doc_OID.$section.$child"; }
353 }
354 print $handle "\n";
355 }
356
357 # output the matching document number
358 print $handle "<docnum>$self->{'num_sections'}\n";
359
360 print $handle '-' x 70, "\n";
361
362
363 # output a database entry for the document number
364 print $handle "[$self->{'num_sections'}]\n";
365 if ($section eq "") { print $handle "<section>$doc_OID\n"; }
366 else { print $handle "<section>$doc_OID.$section\n"; }
367 print $handle '-' x 70, "\n";
368
369 # output entry for url
370 if ($url ne "") {
371 print $handle $url;
372 }
373
374 $first = 0;
375 $section = $doc_obj->get_next_section($section);
376 }
377
378 # classify this document
379 &classify::classify_doc ($self->{'classifiers'}, $doc_obj);
380
381}
382
383sub find_paragraphs {
384 $_[1] =~ s/(<p\b)/<Paragraph>$1/gi;
385}
386
387sub filter_text {
388 # $self->filter_text ($field, $new_text);
389 # don't want to do anything for this version, however,
390 # in a particular collection you might want to override
391 # this method to post-process certain fields depending on
392 # the field, or whether we are outputting it for indexing
393}
394
395sub text {
396 my $self = shift (@_);
397 my ($doc_obj) = @_;
398 my $handle = $self->{'output_handle'};
399 my $indexed_doc = 1;
400
401 # only output this document if it is one to be indexed
402 return if ($doc_obj->get_doc_type() ne "indexed_doc");
403
404 # see if this document belongs to this subcollection
405 foreach $indexexp (@{$self->{'indexexparr'}}) {
406 $indexed_doc = 0;
407 my ($field, $exp, $options) = split /\//, $indexexp;
408 if (defined ($field) && defined ($exp)) {
409 my ($bool) = $field =~ /^(.)/;
410 $field =~ s/^.// if $bool eq '!';
411 if ($field =~ /^filename$/i) {
412 $field = $doc_obj->get_source_filename();
413 } else {
414 $field = $doc_obj->get_metadata_element($doc_obj->get_top_section(), $field);
415 }
416 next unless defined $field;
417 if ($bool eq '!') {
418 if ($options =~ /^i$/i) {
419 if ($field !~ /$exp/i) {$indexed_doc = 1; last;}
420 } else {
421 if ($field !~ /$exp/) {$indexed_doc = 1; last;}
422 }
423 } else {
424 if ($options =~ /^i$/i) {
425 if ($field =~ /$exp/i) {$indexed_doc = 1; last;}
426 } else {
427 if ($field =~ /$exp/) {$indexed_doc = 1; last;}
428 }
429 }
430 }
431 }
432
433 # this is another document
434 $self->{'num_docs'} += 1;
435
436 # get the parameters for the output
437 my ($fields) = $self->{'index'};
438 #print STDERR "fields are $fields\n";
439 $fields =~ s/\ball\b/Title,Creator,text/; # add in others here
440
441 my $doc_section = 0; # just for this document
442 my $text = "";
443 if ($self->{'num_docs'} == 1) {
444 $text = "<Document>\n";
445 }
446 else {
447 $text = "</Document><Document>\n";
448 }
449 my $text_extra = "";
450
451 # get the text for this document
452 my $section = $doc_obj->get_top_section();
453 while (defined $section) {
454 # update a few statistics
455 $doc_section++;
456 $self->{'num_sections'} += 1;
457 $text .= "<Section>\n";
458 if ($indexed_doc) {
459 $self->{'num_bytes'} += $doc_obj->get_text_length ($section);
460 foreach $field (split (/,/, $fields)) {
461 # only deal with this field if it doesn't start with top or
462 # this is the first section
463 my $real_field = $field;
464 if (!($real_field =~ s/^top//) || ($doc_section == 1)) {
465 my $new_text = "";
466 if ($real_field eq "text") {
467 #print STDERR "in text bit";
468 #$new_text = "<Paragraph>";
469 $new_text .= $doc_obj->get_text ($section);
470 #$self->find_paragraphs($new_text);
471 } else { # metadata field
472 if ($real_field eq "metadata") { # insert all metadata
473 #except gsdl stuff
474 #print STDERR "in metadata bit\n";
475 my $metadata = $doc_obj->get_all_metadata ($section);
476 foreach $pair (@$metadata) {
477 my ($mfield, $mvalue) = (@$pair);
478 #print STDERR "$mfield, $mvalue\n";
479 # check fields here, maybe others dont want
480 if ($mfield ne "Identifier" && $mfield ne "classifytype" &&
481 $mfield !~ /^gsdl/ && defined $mvalue && $mvalue ne "") {
482
483 $new_text .= "<$mfield>$mvalue</$mfield>\n";
484 #print STDERR "metadata=$mfield:$mvalue";
485 if (!defined $self->{'indexfields'}->{$mfield}) {
486 $self->{'indexfields'}->{$mfield} = 1;
487 }
488 }
489 }
490
491 }
492 else { #individual metadata specified
493 if (!defined $self->{'indexfields'}->{$real_field}) {
494 $self->{'indexfields'}->{$real_field} = 1;
495 }
496 foreach $item (@{$doc_obj->get_metadata ($section, $real_field)}) {
497 $new_text .= "<$real_field>$item</$real_field>\n";
498 }
499 }
500
501 }
502
503 # filter the text
504 $self->filter_text ($field, $new_text);
505
506 #????????????????????
507 if ($self->{'indexing_text'} &&
508 $new_text =~ /[\(\)\{\}]/) {
509 }
510
511 $text .= "$new_text";
512 }
513 }
514 } # if (indexed_doc)
515
516 $section = $doc_obj->get_next_section($section);
517 } #while defined section
518 print $handle "$text";
519}
520
5211;
522
Note: See TracBrowser for help on using the repository browser.