source: main/tags/2.53/gsdl/perllib/mgppbuildproc.pm@ 32727

Last change on this file since 32727 was 9157, checked in by kjdon, 19 years ago

changed the my declarations of the global variables to our - so lucene code can use them

  • Property svn:keywords set to Author Date Id Revision
File size: 19.8 KB
Line 
1###########################################################################
2#
3# mgppbuildproc.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 mgpp to process
28
29
30package mgppbuildproc;
31
32eval {require bytes};
33
34use classify;
35use doc;
36use docproc;
37use util;
38
39
40BEGIN {
41 @mgppbuildproc::ISA = ('docproc');
42}
43
44#this must be the same as in mgppbuilder
45our %level_map = ('document'=>'Doc',
46 'section'=>'Sec',
47 'paragraph'=>'Para');
48
49sub new {
50 my ($class, $collection, $source_dir, $build_dir,
51 $verbosity, $outhandle) = @_;
52 my $self = new docproc ();
53
54 # outhandle is where all the debugging info goes
55 # output_handle is where the output of the plugins is piped
56 # to (i.e. mg, gdbm etc.)
57 $outhandle = STDERR unless defined $outhandle;
58
59 $self->{'collection'} = $collection;
60 $self->{'source_dir'} = $source_dir;
61 $self->{'build_dir'} = $build_dir;
62 $self->{'verbosity'} = $verbosity;
63 $self->{'classifiers'} = [];
64 $self->{'mode'} = "text";
65 $self->{'assocdir'} = $build_dir;
66 $self->{'dontgdbm'} = {};
67 $self->{'index'} = "text";
68 $self->{'indexexparr'} = [];
69 $self->{'output_handle'} = "STDOUT";
70 $self->{'num_docs'} = 0;
71 $self->{'num_sections'} = 0;
72 $self->{'num_bytes'} = 0;
73 $self->{'num_processed_bytes'} = 0;
74 $self->{'store_text'} = 1;
75 $self->{'outhandle'} = $outhandle;
76
77 #used by browse interface
78 $self->{'doclist'} = [];
79
80 $self->{'indexing_text'} = 0;
81
82 #new ones for mgpp
83 $self->{'dontindex'} = {};
84 $self->{'indexfieldmap'} = {};
85 $self->{'indexfields'} = {}; # only put in the ones that are not specified directly in the index
86 $self->{'strip_html'}=1;
87
88
89 return bless $self, $class;
90}
91
92sub reset {
93 my $self = shift (@_);
94
95 $self->{'num_docs'} = 0;
96 $self->{'num_sections'} = 0;
97 $self->{'num_processed_bytes'} = 0;
98 $self->{'num_bytes'} = 0;
99}
100
101sub get_num_docs {
102 my $self = shift (@_);
103
104 return $self->{'num_docs'};
105}
106
107sub get_num_sections {
108 my $self = shift (@_);
109
110 return $self->{'num_sections'};
111}
112
113# num_bytes is the actual number of bytes in the collection
114# this is normally the same as what's processed during text compression
115sub get_num_bytes {
116 my $self = shift (@_);
117
118 return $self->{'num_bytes'};
119}
120
121# num_processed_bytes is the number of bytes actually passed
122# to mgpp for the current index
123sub get_num_processed_bytes {
124 my $self = shift (@_);
125
126 return $self->{'num_processed_bytes'};
127}
128
129sub set_output_handle {
130 my $self = shift (@_);
131 my ($handle) = @_;
132
133 $self->{'output_handle'} = $handle;
134}
135
136sub set_mode {
137 my $self = shift (@_);
138 my ($mode) = @_;
139
140 $self->{'mode'} = $mode;
141}
142
143sub set_assocdir {
144 my $self = shift (@_);
145 my ($assocdir) = @_;
146
147 $self->{'assocdir'} = $assocdir;
148}
149
150sub set_dontgdbm {
151 my $self = shift (@_);
152 my ($dontgdbm) = @_;
153
154 $self->{'dontgdbm'} = $dontgdbm;
155}
156
157sub set_index {
158 my $self = shift (@_);
159 my ($index, $indexexparr) = @_;
160
161 $self->{'index'} = $index;
162 $self->{'indexexparr'} = $indexexparr if defined $indexexparr;
163}
164
165sub get_index {
166 my $self = shift (@_);
167
168 return $self->{'index'};
169}
170
171sub set_classifiers {
172 my $self = shift (@_);
173 my ($classifiers) = @_;
174
175 $self->{'classifiers'} = $classifiers;
176}
177
178sub set_indexing_text {
179 my $self = shift (@_);
180 my ($indexing_text) = @_;
181
182 $self->{'indexing_text'} = $indexing_text;
183}
184
185sub get_indexing_text {
186 my $self = shift (@_);
187
188 return $self->{'indexing_text'};
189}
190
191sub set_store_text {
192 my $self = shift (@_);
193 my ($store_text) = @_;
194
195 $self->{'store_text'} = $store_text;
196}
197
198sub get_doc_list {
199 my $self = shift(@_);
200
201 return @{$self->{'doclist'}};
202}
203
204sub set_indexfieldmap {
205 my $self = shift (@_);
206 my ($indexmap) = @_;
207
208 $self->{'indexfieldmap'} = $indexmap;
209}
210
211sub get_indexfieldmap {
212 my $self = shift (@_);
213
214 return $self->{'indexfieldmap'};
215}
216
217sub set_levels {
218 my $self = shift (@_);
219 my ($levels) = @_;
220
221 $self->{'levels'} = $levels;
222}
223
224sub set_strip_html {
225 my $self = shift (@_);
226 my ($strip) = @_;
227 $self->{'strip_html'}=$strip;
228}
229
230sub process {
231 my $self = shift (@_);
232 my $method = $self->{'mode'};
233
234 $self->$method(@_);
235}
236
237# use 'Paged' if document has no more than 2 levels
238# and each section at second level has a number for
239# Title metadata
240# also use Paged if gsdlthistype metadata is set to Paged
241sub get_document_type {
242 my $self = shift (@_);
243 my ($doc_obj) = @_;
244
245 my $thistype = "VList";
246 my $childtype = "VList";
247 my $title;
248 my @tmp = ();
249
250 my $section = $doc_obj->get_top_section ();
251
252 my $gsdlthistype = $doc_obj->get_metadata_element ($section, "gsdlthistype");
253 if (defined $gsdlthistype) {
254 if ($gsdlthistype eq "Paged") {
255 $childtype = "Paged";
256 if ($doc_obj->get_text_length ($doc_obj->get_top_section())) {
257 $thistype = "Paged";
258 } else {
259 $thistype = "Invisible";
260 }
261
262 return ($thistype, $childtype);
263 } elsif ($gsdlthistype eq "Hierarchy") {
264 return ($thistype, $childtype); # use VList, VList
265 }
266 }
267 my $first = 1;
268 while (defined $section) {
269 @tmp = split /\./, $section;
270 if (scalar(@tmp) > 1) {
271 return ($thistype, $childtype);
272 }
273 if (!$first) {
274 $title = $doc_obj->get_metadata_element ($section, "Title");
275 if (!defined $title || $title !~ /^\d+$/) {
276 return ($thistype, $childtype);
277 }
278 }
279 $first = 0;
280 $section = $doc_obj->get_next_section($section);
281 }
282 if ($doc_obj->get_text_length ($doc_obj->get_top_section())) {
283 $thistype = "Paged";
284 } else {
285 $thistype = "Invisible";
286 }
287 $childtype = "Paged";
288 return ($thistype, $childtype);
289}
290
291sub assoc_files {
292 my $self = shift (@_);
293 my ($doc_obj, $archivedir) = @_;
294 my ($afile);
295
296 foreach my $assoc_file (@{$doc_obj->get_assoc_files()}) {
297 # if assoc file starts with a slash, we put it relative to the assoc
298 # dir, otherwise it is relative to the HASH... directory
299 if ($assoc_file->[1] =~ m@^[/\\]@) {
300 $afile = &util::filename_cat($self->{'assocdir'},$assoc_file->[1]);
301 } else {
302 $afile = &util::filename_cat($self->{'assocdir'}, $archivedir, $assoc_file->[1]);
303 }
304 &util::hard_link ($assoc_file->[0], $afile);
305 }
306}
307
308sub infodb {
309 my $self = shift (@_);
310 my ($doc_obj, $filename) = @_;
311 my $handle = $self->{'output_handle'};
312
313 my $doctype = $doc_obj->get_doc_type();
314
315 # only output this document if it is one to be indexed
316 return if ($doctype ne "indexed_doc");
317
318 #if a Section level index is not built, the gdbm file should be at doc
319 #level not Section
320 my $docs_only = 1;
321 if ($self->{'levels'}->{'section'}) {
322 $docs_only = 0;
323 }
324
325 my ($archivedir) = $filename =~ /^(.*?)(?:\/|\\)[^\/\\]*$/;
326 $archivedir = "" unless defined $archivedir;
327 $archivedir =~ s/\\/\//g;
328 $archivedir =~ s/^\/+//;
329 $archivedir =~ s/\/+$//;
330
331 # resolve the final filenames of the files associated with this document
332 $self->assoc_files ($doc_obj, $archivedir);
333
334 #GRB: moved 1/06/2004 from GRB01062004
335 #add this document to the browse structure
336 push(@{$self->{'doclist'}},$doc_obj->get_OID())
337 unless ($doctype eq "classification");
338
339 # classify this document
340 &classify::classify_doc ($self->{'classifiers'}, $doc_obj);
341 #GRB: end of moved block
342
343 # this is another document
344 $self->{'num_docs'} += 1 unless ($doctype eq "classification");
345
346 # is this a paged or a hierarchical document
347 my ($thistype, $childtype) = $self->get_document_type ($doc_obj);
348
349 my $section = $doc_obj->get_top_section ();
350 my $doc_OID = $doc_obj->get_OID();
351 my $first = 1;
352 my $url = "";
353 while (defined $section) {
354 # update a few statistics
355 $self->{'num_bytes'} += $doc_obj->get_text_length ($section);
356 $self->{'num_sections'} += 1 unless ($doctype eq "classification");
357
358 # output the section name
359 if ($section eq "") { print $handle "[$doc_OID]\n"; }
360 else { print $handle "[$doc_OID.$section]\n"; }
361
362 # output the fact that this document is a document (unless doctype
363 # has been set to something else from within a plugin
364 my $dtype = $doc_obj->get_metadata_element ($section, "doctype");
365 if (!defined $dtype || $dtype !~ /\w/) {
366 print $handle "<doctype>doc\n";
367 }
368
369 # output whether this node contains text
370 if ($doc_obj->get_text_length($section) > 0) {
371 print $handle "<hastxt>1\n";
372 } else {
373 print $handle "<hastxt>0\n";
374 }
375
376 # output all the section metadata
377 my $metadata = $doc_obj->get_all_metadata ($section);
378 foreach my $pair (@$metadata) {
379 my ($field, $value) = (@$pair);
380
381 if ($field ne "Identifier" && $field !~ /^gsdl/ &&
382 defined $value && $value ne "") {
383
384 # escape problematic stuff
385 $value =~ s/\\/\\\\/g;
386 $value =~ s/\n/\\n/g;
387 $value =~ s/\r/\\r/g;
388
389 # special case for URL metadata
390 if ($field =~ /^URL$/i) {
391 $url .= "[$value]\n";
392 if ($section eq "") {$url .= "<section>$doc_OID\n";}
393 else {$url .= "<section>$doc_OID.$section\n";}
394 $url .= '-' x 70 . "\n";
395 }
396
397 if (!defined $self->{'dontgdbm'}->{$field}) {
398 print $handle "<$field>$value\n";
399 }
400 }
401 }
402
403 # output archivedir if at top level
404 if ($section eq $doc_obj->get_top_section()) {
405 print $handle "<archivedir>$archivedir\n";
406 }
407
408 # output document display type
409 if ($first) {
410 print $handle "<thistype>$thistype\n";
411 }
412
413 if (!$docs_only) {
414 # output a list of children
415 my $children = $doc_obj->get_children ($section);
416 if (scalar(@$children) > 0) {
417 print $handle "<childtype>$childtype\n";
418 print $handle "<contains>";
419 my $firstchild = 1;
420 foreach my $child (@$children) {
421 print $handle ";" unless $firstchild;
422 $firstchild = 0;
423 if ($child =~ /^.*?\.(\d+)$/) {
424 print $handle "\".$1";
425 } else {
426 print $handle "\".$child";
427 }
428# if ($child eq "") { print $handle "$doc_OID"; }
429# elsif ($section eq "") { print $handle "$doc_OID.$child"; }
430# else { print $handle "$doc_OID.$section.$child"; }
431 }
432 print $handle "\n";
433 }
434 #output the matching doc number
435 print $handle "<docnum>$self->{'num_sections'}\n";
436
437 } # if (!$docs_only)
438 else { #docs only, doc num is num_docs not num_sections
439 # output the matching document number
440 print $handle "<docnum>$self->{'num_docs'}\n";
441 }
442
443 print $handle '-' x 70, "\n";
444
445
446 # output a database entry for the document number
447 if ($docs_only) {
448 print $handle "[$self->{'num_docs'}]\n";
449 print $handle "<section>$doc_OID\n";
450 }
451 else {
452 print $handle "[$self->{'num_sections'}]\n";
453 if ($section eq "") { print $handle "<section>$doc_OID\n"; }
454 else { print $handle "<section>$doc_OID.$section\n"; }
455 }
456 print $handle '-' x 70, "\n";
457
458 # output entry for url
459 if ($url ne "") {
460 print $handle $url;
461 }
462
463 $first = 0;
464 $section = $doc_obj->get_next_section($section);
465 last if ($docs_only); # if no sections wanted, only gdbm the docs
466 }
467
468 #GRB01062004: see code above moved from here
469}
470
471#sub find_paragraphs {
472# $_[1] =~ s/(<p\b)/<Paragraph>$1/gi;
473#}
474
475#this function strips the html tags from the doc if ($strip_html) and
476# if ($para) replaces <p> with <Paragraph> tags.
477# if both are false, the original text is returned
478#assumes that <pre> and </pre> have no spaces, and removes all < and > inside
479#these tags
480sub preprocess_text {
481 my $self = shift (@_);
482 my ($text, $strip_html, $para) = @_;
483 my ($outtext) = "";
484 if ($strip_html) {
485 while ($text =~ /<([^>]*)>/ && $text ne "") {
486
487 my $tag = $1;
488 $outtext .= $`." "; #add everything before the matched tag
489 $text = $'; #everything after the matched tag
490 if ($para && $tag =~ /^\s*p\s/i) {
491 $outtext .= $para;
492 }
493 elsif ($tag =~ /^pre$/) { # a pre tag
494 $text =~ /<\/pre>/; # find the closing pre tag
495 my $tmp_text = $`; #everything before the closing pre tag
496 $text = $'; #everything after the </pre>
497 $tmp_text =~ s/[<>]//g; # remove all < and >
498 $outtext.= $tmp_text . " ";
499 }
500 }
501
502 $outtext .= $text; # add any remaining text
503 return $outtext;
504 } #if strip_html
505
506 #if ($para) {
507 #$text =~ s/(<p\b)/$para$1/gi;
508 #return $text;
509 # }
510 return $text;
511}
512
513
514
515sub filter_text {
516 # $self->filter_text ($field, $new_text);
517 # don't want to do anything for this version, however,
518 # in a particular collection you might want to override
519 # this method to post-process certain fields depending on
520 # the field, or whether we are outputting it for indexing
521}
522
523sub text {
524 my $self = shift (@_);
525 my ($doc_obj) = @_;
526 my $handle = $self->{'output_handle'};
527 my $outhandle = $self->{'outhandle'};
528 my $indexed_doc = 1;
529
530 # only output this document if it is one to be indexed
531 return if ($doc_obj->get_doc_type() ne "indexed_doc");
532
533 # see if this document belongs to this subcollection
534 foreach my $indexexp (@{$self->{'indexexparr'}}) {
535 $indexed_doc = 0;
536 my ($field, $exp, $options) = split /\//, $indexexp;
537 if (defined ($field) && defined ($exp)) {
538 my ($bool) = $field =~ /^(.)/;
539 $field =~ s/^.// if $bool eq '!';
540 if ($field =~ /^filename$/i) {
541 $field = $doc_obj->get_source_filename();
542 } else {
543 $field = $doc_obj->get_metadata_element($doc_obj->get_top_section(), $field);
544 }
545 next unless defined $field;
546 if ($bool eq '!') {
547 if ($options =~ /^i$/i) {
548 if ($field !~ /$exp/i) {$indexed_doc = 1; last;}
549 } else {
550 if ($field !~ /$exp/) {$indexed_doc = 1; last;}
551 }
552 } else {
553 if ($options =~ /^i$/i) {
554 if ($field =~ /$exp/i) {$indexed_doc = 1; last;}
555 } else {
556 if ($field =~ /$exp/) {$indexed_doc = 1; last;}
557 }
558 }
559 }
560 }
561
562 # this is another document
563 $self->{'num_docs'} += 1;
564
565 # get the parameters for the output
566 # split on : just in case there is subcoll and lang stuff
567 my ($fields) = split (/:/, $self->{'index'});
568
569 my ($documenttag) = "";
570 my($documentendtag) = "";
571 if ($self->{'levels'}->{'document'}) {
572 $documenttag = "\n<". $level_map{'document'} . ">\n";
573 $documentendtag = "\n</". $level_map{'document'} . ">\n";
574 }
575 my ($sectiontag) = "";
576 if ($self->{'levels'}->{'section'}) {
577 $sectiontag = "\n<". $level_map{'section'} . ">\n";
578 }
579 my ($paratag) = "";
580 if ($self->{'levels'}->{'paragraph'}) {
581 if ($self->{'strip_html'}) {
582 $paratag = "<". $level_map{'paragraph'} . ">";
583 } else {
584 print $outhandle "Paragraph level can not be used with no_strip_html!. Not indexing Paragraphs.\n";
585 }
586 }
587
588 my $doc_section = 0; # just for this document
589
590 my $text = $documenttag;
591
592 # get the text for this document
593 my $section = $doc_obj->get_top_section();
594 while (defined $section) {
595 # update a few statistics
596 $doc_section++;
597 $self->{'num_sections'} += 1;
598 $text .= "$sectiontag";
599
600 if ($indexed_doc) {
601 if ($self->{'indexing_text'}) {
602 $text .= "$paratag"; # only add para tags for indexing
603 # note that we assume that metadata will not be asked for for the compressed text, so we add para tags without checking for indexing_text
604 }
605 $self->{'num_bytes'} += $doc_obj->get_text_length ($section);
606 foreach my $field (split (/,/, $fields)) {
607 # only deal with this field if it doesn't start with top or
608 # this is the first section
609 my $real_field = $field;
610 if (!($real_field =~ s/^top//) || ($doc_section == 1)) {
611 my $new_text = "";
612 my $tmp_text = "";
613 if ($real_field eq "text") {
614 if ($self->{'indexing_text'}) { #tag the text with <Text>...</Text>, add the <Paragraph> tags and strip out html if needed
615 $new_text .= "$paratag<TX>\n";
616 $tmp_text .= $doc_obj->get_text ($section);
617 $tmp_text = $self->preprocess_text($tmp_text, $self->{'strip_html'}, "</TX>$paratag<TX>");
618
619 $new_text .= "$tmp_text</TX>\n";
620 #if (!defined $self->{'indexfields'}->{'TextOnly'}) {
621 #$self->{'indexfields'}->{'TextOnly'} = 1;
622 #}
623 }
624 else { # leave html stuff in, and dont add Paragraph tags - never retrieve paras at the moment
625 $new_text .= $doc_obj->get_text ($section) if $self->{'store_text'};
626 }
627 } else { # metadata field
628 if ($real_field eq "allfields") { #ignore
629 }
630 elsif ($real_field eq "metadata") { # insert all metadata
631 #except gsdl stuff
632 my $shortname = "";
633 my $metadata = $doc_obj->get_all_metadata ($section);
634 foreach my $pair (@$metadata) {
635 my ($mfield, $mvalue) = (@$pair);
636 # check fields here, maybe others dont want - change to use dontindex!!
637 if ($mfield ne "Identifier"
638 && $mfield !~ /^gsdl/
639 && $mfield ne "classifytype"
640 && $mfield ne "assocfilepath"
641 && defined $mvalue && $mvalue ne "") {
642
643 if (defined $self->{'indexfieldmap'}->{$mfield}) {
644 $shortname = $self->{'indexfieldmap'}->{$mfield};
645 }
646 else {
647 $shortname = $self->create_shortname($mfield);
648 $self->{'indexfieldmap'}->{$mfield} = $shortname;
649 $self->{'indexfieldmap'}->{$shortname} = 1;
650 }
651 $new_text .= "$paratag<$shortname>$mvalue</$shortname>\n";
652 if (!defined $self->{'indexfields'}->{$mfield}) {
653 $self->{'indexfields'}->{$mfield} = 1;
654 }
655 }
656 }
657
658 }
659 else { #individual metadata specified
660 my $shortname="";
661 #if (!defined $self->{'indexfields'}->{$real_field}) {
662 #$self->{'indexfields'}->{$real_field} = 1;
663 #}
664 if (defined $self->{'indexfieldmap'}->{$real_field}) {
665 $shortname = $self->{'indexfieldmap'}->{$real_field};
666 }
667 else {
668 $shortname = $self->create_shortname($real_field);
669 $self->{'indexfieldmap'}->{$real_field} = $shortname;
670 $self->{'indexfieldmap'}->{$shortname} = 1;
671 }
672 foreach my $item (@{$doc_obj->get_metadata ($section, $real_field)}) {
673 $new_text .= "$paratag<$shortname>$item</$shortname>\n";
674 }
675 }
676
677 }
678
679 # filter the text
680 $self->filter_text ($field, $new_text);
681
682 $self->{'num_processed_bytes'} += length ($new_text);
683 $text .= "$new_text";
684 }
685 }
686 } # if (indexed_doc)
687
688 $section = $doc_obj->get_next_section($section);
689 } #while defined section
690 print $handle "$text\n$documentendtag";
691
692}
693
694#chooses the first two letters or digits for the shortname
695#now ignores non-letdig characters
696sub create_shortname {
697 my $self = shift(@_);
698
699 my ($realname) = @_;
700 #take the first two chars
701 my $shortname;
702 if ($realname =~ /^[^\w]*(\w)[^\w]*(\w)/) {
703 $shortname = "$1$2";
704 } else {
705 # there aren't two letdig's in the field - try arbitrary combinations
706 $realname = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
707 $shortname = "AB";
708 }
709 $shortname =~ tr/a-z/A-Z/;
710
711 #if already used, take the first and third letdigs and so on
712 my $count = 1;
713 while (defined $self->{'indexfieldmap'}->{$shortname}) {
714 if ($realname =~ /^[^\w]*(\w)([^\w]*\w){$count}[^\w]*(\w)/) {
715 $shortname = "$1$3";
716 $count++;
717 $shortname =~ tr/a-z/A-Z/;
718
719 }
720 else {
721 #remove up to and incl the first letdig
722 $realname =~ s/^[^\w]*\w//;
723 $count = 0;
724 }
725 }
726
727 return $shortname;
728}
729
7301;
731
Note: See TracBrowser for help on using the repository browser.