source: trunk/gsdl/perllib/mgbuildproc.pm@ 222

Last change on this file since 222 was 222, checked in by sjboddie, 25 years ago

Unfixed the 'bug'.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 17.4 KB
Line 
1# This document processor outputs a document
2# for mg to process
3
4
5package mgbuildproc;
6
7use classify;
8use doc;
9use docproc;
10use util;
11
12
13BEGIN {
14 @ISA = ('docproc');
15}
16
17
18sub new {
19 my ($class, $collection, $source_dir, $build_dir, $verbosity) = @_;
20 my $self = new docproc ();
21
22 $self->{'collection'} = $collection;
23 $self->{'source_dir'} = $source_dir;
24 $self->{'build_dir'} = $build_dir;
25 $self->{'verbosity'} = $verbosity;
26 $self->{'classifiers'} = [];
27 $self->{'mode'} = "text";
28 $self->{'index'} = "section:text";
29 $self->{'indexexparr'} = [];
30 $self->{'output_handle'} = "STDOUT";
31 $self->{'num_docs'} = 0;
32 $self->{'num_sections'} = 0;
33 $self->{'num_bytes'} = 0;
34
35 return bless $self, $class;
36}
37
38sub reset {
39 my $self = shift (@_);
40
41 $self->{'num_docs'} = 0;
42 $self->{'num_sections'} = 0;
43 $self->{'num_bytes'} = 0;
44}
45
46sub get_num_docs {
47 my $self = shift (@_);
48
49 return $self->{'num_docs'};
50}
51
52sub get_num_sections {
53 my $self = shift (@_);
54
55 return $self->{'num_sections'};
56}
57
58sub get_num_bytes {
59 my $self = shift (@_);
60
61 return $self->{'num_bytes'};
62}
63
64sub set_output_handle {
65 my $self = shift (@_);
66 my ($handle) = @_;
67
68 $self->{'output_handle'} = $handle;
69}
70
71sub set_mode {
72 my $self = shift (@_);
73 my ($mode) = @_;
74
75 $self->{'mode'} = $mode;
76}
77
78sub set_index {
79 my $self = shift (@_);
80 my ($index, $indexexparr) = @_;
81
82 $self->{'index'} = $index;
83 $self->{'indexexparr'} = $indexexparr if defined $indexexparr;
84}
85
86sub set_classifiers {
87 my $self = shift (@_);
88 my ($classifiers) = @_;
89
90 $self->{'classifiers'} = $classifiers;
91}
92
93sub process {
94 my $self = shift (@_);
95 my $method = $self->{'mode'};
96
97 $self->$method(@_);
98}
99
100sub newinfodb {
101 my $self = shift (@_);
102 my ($doc_obj, $filename) = @_;
103 my $handle = $self->{'output_handle'};
104# $handle = "main::STDOUT";
105
106 # this was used in the old version
107 return if ($doc_obj eq 'classifications');
108
109 my $doctype = $doc_obj->get_doc_type();
110
111 # only output this document if it is one to be indexed
112 return if ($doctype ne "indexed_doc");
113
114 # this is another document
115 $self->{'num_docs'} += 1 unless ($doctype eq "classification");
116
117 my $section = $doc_obj->get_top_section ();
118 my $doc_OID = $doc_obj->get_OID();
119 while (defined $section) {
120 # update a few statistics
121 $self->{'num_bytes'} += $doc_obj->get_text_length ($section);
122 $self->{'num_sections'} += 1 unless ($doctype eq "classification");
123
124 # output the section name
125 if ($section eq "") { print $handle "[$doc_OID]\n"; }
126 else { print $handle "[$doc_OID.$section]\n"; }
127
128 # output the fact that this document is a document
129 print $handle "<doctype>doc\n";
130
131 # output whether this node contains text
132 if ($doc_obj->get_text_length($section) > 0) {
133 print $handle "<hastxt>1\n";
134 } else {
135 print $handle "<hastxt>0\n";
136 }
137
138 # output all the section metadata
139 my $metadata = $doc_obj->get_all_metadata ($section);
140 foreach $pair (@$metadata) {
141 my ($field, $value) = (@$pair);
142
143 if ($field ne "Identifier" && $field !~ /^gsdl/ &&
144 defined $value && $value ne "") {
145 # escape problematic stuff
146 $value =~ s/\\/\\\\/g;
147 $value =~ s/\n/\\n/g;
148 $value =~ s/\r/\\r/g;
149
150 print $handle "<$field>$value\n";
151 }
152 }
153
154 # output a list of children
155 my $children = $doc_obj->get_children ($section);
156 if (scalar(@$children) > 0) {
157 print $handle "<contains>";
158 my $firstchild = 1;
159 foreach $child (@$children) {
160 print $handle ";" unless $firstchild;
161 $firstchild = 0;
162 print $handle "\".$child";
163# if ($child eq "") { print $handle "$doc_OID"; }
164# elsif ($section eq "") { print $handle "$doc_OID.$child"; }
165# else { print $handle "$doc_OID.$section.$child"; }
166 }
167 print $handle "\n";
168 }
169
170 # output the matching document number
171 print $handle "<docnum>$self->{'num_sections'}\n";
172
173 print $handle '-' x 70, "\n";
174
175
176 # output a database entry for the document number
177 print $handle "[$self->{'num_sections'}]\n";
178 if ($section eq "") { print $handle "<section>$doc_OID\n"; }
179 else { print $handle "<section>$doc_OID.$section\n"; }
180 print $handle '-' x 70, "\n";
181
182
183 $section = $doc_obj->get_next_section($section);
184 }
185
186 # classify this document
187 &classify::classify_doc ($self->{'classifiers'}, $doc_obj);
188
189}
190
191
192
193sub infodb {
194 my $self = shift (@_);
195 my ($doc_obj, $filename) = @_;
196 my $handle = $self->{'output_handle'};
197
198 if ($doc_obj eq 'classifications') {
199 # output classifications if all books have been processed
200 foreach $key (keys %$saved_classifications) {
201 $saved_classifications->{$key}->{'contains'} = undef if
202 $saved_classifications->{$key}->{'contains'} eq "";
203 $saved_classifications->{$key}->{'parent'} = undef if
204 (!defined $saved_classifications->{$key}->{'parent'}) ||
205 ($saved_classifications->{$key}->{'parent'} eq "");
206 $self->write_to_gdbm ($handle, $key, $saved_classifications->{$key}->{'title'},
207 undef, undef, undef, undef,
208 $saved_classifications->{$key}->{'contains'}, undef,
209 $saved_classifications->{$key}->{'parent'}, undef, undef);
210 }
211 return;
212 }
213
214 my $doctype = $doc_obj->get_doc_type();
215
216 # only output this document if it is one to be indexed
217 return if ($doctype ne "indexed_doc" &&
218 $doctype ne "classification");
219
220 # found classification document
221 $saved_classifications = {} if ($doctype eq "classification");
222 $sectionmap = {};
223
224 # this is another document
225 $self->{'num_docs'} += 1 unless ($doctype eq "classification");
226
227 my $section = $doc_obj->get_top_section();
228 while (defined $section) {
229 # update a few statistics
230 $self->{'num_bytes'} += $doc_obj->get_text_length ($section);
231 $self->{'num_sections'} += 1 unless ($doctype eq "classification");
232
233 my $title = $doc_obj->get_metadata_element($section, "Title");
234 my $source = $doc_obj->get_metadata_element($section, "Source");
235 my $date = $doc_obj->get_metadata_element($section, "Date");
236 my $jobnumber = $doc_obj->get_source_filename();
237
238 my $mapped_section = $self->map_section($doctype, $section);
239 if ($doctype eq "classification") {
240 $mapped_section = $self->char_classification($mapped_section);
241 } else {
242 $mapped_section = "B.$self->{'num_docs'}.$mapped_section";
243 }
244 $mapped_section =~ s/\.+$//;
245
246 my ($parent, $classification, $creator);
247
248 $classification = $self->get_classifications($doc_obj, $section, $mapped_section)
249 unless $doctype eq "classification";
250
251 if ($section ne $doc_obj->get_top_section()) {
252 $parent = $self->map_section($doctype, $doc_obj->get_parent_section($section));
253 if ($doctype eq "classification") {
254 $parent = $self->char_classification($parent);
255 } else {
256 $parent = "B.$self->{'num_docs'}.$parent";
257 }
258 $parent =~ s/\.+$//;
259 } else {
260 $creator = $doc_obj->get_metadata_element($section, "Creator");
261
262 # need filename so we know what directory to look in for associated files
263 $filename =~ s/^\/?(.*?\.dir).*$/$1/ if (defined $filename);
264 }
265
266 if ($doc_obj->is_leaf_section($section)) {
267 if ($doctype eq "classification") {
268 if (defined $saved_classifications->{$mapped_section}) {
269 print STDERR "mgbuildproc:warning: classification $mapped_section " .
270 "declared multiple times\n";
271 } else {
272 $saved_classifications->{$mapped_section} = {'title' => $title, 'contains' => "",
273 'parent' => $parent};
274 }
275 } else {
276 $self->write_to_gdbm($handle, $mapped_section, $title, $creator, $source, $date, $jobnumber, undef,
277 $self->{'num_sections'}, $parent, $classification, $filename);
278 }
279 } else {
280
281 # add the introductory section if it exists
282 my $contains = "";
283 if ($doc_obj->get_text_length($section) > 0) {
284 $contains .= "B.$self->{'num_docs'}." . $self->map_section ($doctype, "$section.0");
285 }
286
287 # add the rest of the children
288 my @children = @{$doc_obj->get_children($section)};
289 if ($doctype eq "classification") {
290 map {$_ = $self->char_classification($_);} @children;
291 if (defined $saved_classifications->{$mapped_section}) {
292 print STDERR "mgbuildproc:warning: classification $mapped_section " .
293 "declared multiple times\n";
294 } else {
295 $saved_classifications->{$mapped_section} = {'title' => $title, 'contains' => ""};
296 $saved_classifications->{$mapped_section}->{'contains'} = join ":", @children;
297 $saved_classifications->{$mapped_section}->{'parent'} = $parent;
298 }
299 } else {
300 map {$_ = "B.$self->{'num_docs'}." . $self->map_section($doctype, $_);} @children;
301 $contains .= ":" if $contains ne "";
302 $contains .= join ":", @children;
303 $self->write_to_gdbm ($handle, $mapped_section, $title, $creator, $source,
304 $date, $jobnumber, $contains, $self->{'num_sections'},
305 $parent, $classification, $filename);
306
307 if ($doc_obj->get_text_length($section) > 0) {
308 my $intromapsection = "B.$self->{'num_docs'}." .
309 $self->map_section($doctype, "$section.0");
310 $self->write_to_gdbm ($handle, $intromapsection, "<i>(introductory text)</i>", $creator,
311 $source, $date, $jobnumber, undef, $self->{'num_sections'},
312 $mapped_section, $classification, $filename);
313 }
314 }
315 }
316 $section = $doc_obj->get_next_section($section);
317 }
318
319 # update classification list with those books that
320 # were processed before classification list
321 if ($doctype eq "classification" && defined $temp_classifications) {
322 foreach $key (keys(%$temp_classifications)) {
323 if (!defined $saved_classifications->{$key}) {
324 print STDERR "mgbuildproc:$temp_classifications->{$key} belong to " .
325 "undefined classification $key\n";
326 }
327 $saved_classifications->{$key}->{'contains'} = $temp_classifications->{$key}->{'contains'};
328 }
329 $temp_classifications = undef;
330 }
331}
332
333
334sub write_to_gdbm {
335 my $self = shift (@_);
336 my ($handle, $section, $title, $creator, $source, $date,
337 $jobnumber, $contains, $docnum, $parent, $classification, $OID) = @_;
338
339 print $handle "[$section]\n";
340 print $handle "<t>$title\n" if (defined $title && $title ne "");
341 print $handle "<a>$creator\n" if (defined $creator && $creator ne "");
342 print $handle "<s>$source\n" if (defined $source && $source ne "");
343 print $handle "<i>$date\n" if (defined $date && $date ne "");
344 print $handle "<j>$jobnumber\n" if (defined $jobnumber && $jobnumber ne "");
345 print $handle "<c>$contains\n" if (defined $contains && $contains ne "");
346 print $handle "<d>$docnum\n" if (defined $docnum && $docnum ne "");
347 print $handle "<p>$parent\n" if (defined $parent && $parent ne "");
348 print $handle "<x>$classification\n" if (defined $classification && $classification ne "");
349 print $handle "<o>$OID\n" if defined $OID;
350 print $handle '-' x 70, "\n";
351
352 if (defined $docnum) {
353 print $handle "[$docnum]\n";
354 print $handle "<x>$section\n";
355 print $handle '-' x 70, "\n";
356 }
357}
358
359sub get_classifications {
360 my $self = shift (@_);
361 my ($doc_obj, $section, $mapped_section) = @_;
362
363 my ($classificationsref);
364 if (defined $saved_classifications) {
365 # classification list has been processed
366 $classificationsref = $saved_classifications;
367 } else {
368 # classification list has yet to be processed, save
369 # books in temp_classifications until list is processed
370 # and they can be moved into saved_classifications
371 $temp_classifications = {} unless defined $temp_classifications;
372 $classificationsref = $temp_classifications;
373 }
374
375 my $classifications = $doc_obj->get_metadata($section, "Subject");
376
377 # need to save which books belong in each classification
378 # to output later
379 foreach $classification (@$classifications) {
380
381 if (!defined $classificationsref->{$classification}) {
382 $classificationsref->{$classification}->{'parent'} = $doc_obj->get_parent_section($section);
383 $classificationsref->{$classification}->{'contains'} = "";
384 }
385 $classificationsref->{$classification}->{'contains'} .= ":" unless
386 $classificationsref->{$classification}->{'contains'} eq "";
387 $classificationsref->{$classification}->{'contains'} .= $mapped_section;
388 }
389
390 return (join ":", @$classifications);
391}
392
393sub map_section {
394 my $self = shift (@_);
395 my ($doctype, $section) = @_;
396
397 # classifications should never need to be mapped
398 return $section if $doctype eq "classification";
399
400 return "" unless (defined $section) && ($section ne "");
401
402 $sectionmap = {} unless defined $sectionmap;
403
404
405
406 # get the section into a standard format
407 $section =~ s/^\.+|\.+$//g;
408
409 # return the mapped section if it has been seen before
410 if (defined $sectionmap->{$section}) {
411 return $sectionmap->{$section};
412 }
413
414 # find out the parent section
415 my ($parentsection, $num);
416 if ($section =~ /^(.+)\.(\d+)$/) {
417 $parentsection = $1;
418 $num = $2;
419 } elsif ($section =~ /^(\d+)$/) {
420 $parentsection = "";
421 $num = $1;
422 } else {
423 print STDERR "mgbuildproc:map_section - misformed section $section\n";
424 }
425 $parentsection = "" unless defined $parentsection;
426
427 if ($parentsection eq "") {
428 $num ++ if $num == 1 and defined $sectionmap->{'0'};
429 } else {
430 $num ++ if $num == 1 and defined $sectionmap->{"$parentsection.0"};
431 }
432
433 # find out the mapped parent section
434 my $mappedparentsection = $self->map_section ($doctype, $parentsection);
435
436 # find the next unused child section
437 my $previousnum = $num - 1;
438 my $previoussection = "";
439 if ($parentsection eq "") {
440 $previoussection = $previousnum;
441 $previoussection = 0 if ($section == 1) && (defined $sectionmap->{'0'});
442 } else{
443 $previoussection = "$parentsection.$previousnum";
444 $previoussection = "$parentsection.0" if ($section =~ /\.1$/) && (defined $sectionmap->{"$parentsection.0"});
445 }
446 while (($previousnum > 0) && (!defined $sectionmap->{$previoussection})) {
447 $previousnum--;
448 $previoussection = "$parentsection.$previousnum";
449 $previoussection = $previousnum if $parentsection eq "";
450 }
451
452 # there has been no children under this parent, this section will be number 1
453 if ($previousnum <= 0) {
454 if ($mappedparentsection eq "") {
455 $sectionmap->{$section} = "1";
456 } else {
457 $sectionmap->{$section} = "$mappedparentsection.1";
458 }
459
460 } else {
461 # get the previous mapped number
462 my $previousmapnum = 0;
463 if ($sectionmap->{$previoussection} =~ /(^|\.)?(\d+)$/) {
464 $previousmapnum = $2;
465 }
466
467 # increment it to get this mapped child
468 my $mappednum = $previousmapnum+1;
469
470 if ($mappedparentsection eq "") {
471 $sectionmap->{$section} = $mappednum;
472 } else {
473 $sectionmap->{$section} = "$mappedparentsection.$mappednum";
474 }
475 }
476
477 return $sectionmap->{$section};
478}
479
480sub text {
481 my $self = shift (@_);
482 my ($doc_obj) = @_;
483 my $handle = $self->{'output_handle'};
484 my $indexed_doc = 1;
485
486 # only output this document if it is one to be indexed
487 return if ($doc_obj->get_doc_type() ne "indexed_doc");
488
489 # see if this document belongs to this subcollection
490 foreach $indexexp (@{$self->{'indexexparr'}}) {
491 $indexed_doc = 0;
492 my ($field, $exp, $options) = split /\//, $indexexp;
493 if (defined ($field) && defined ($exp)) {
494 my ($bool) = $field =~ /^(.)/;
495 $field =~ s/^.// if $bool eq '!';
496 if ($field eq "filename") {
497 $field = $doc_obj->get_source_filename();
498 } else {
499 $field = $doc_obj->get_metadata_element($doc_obj->get_top_section(), $field);
500 }
501 next unless defined $field;
502 if ($bool eq '!') {
503 if ($options =~ /^i$/i) {
504 if ($field !~ /$exp/i) {$indexed_doc = 1; last;}
505 } else {
506 if ($field !~ /$exp/) {$indexed_doc = 1; last;}
507 }
508 } else {
509 if ($options =~ /^i$/i) {
510 if ($field =~ /$exp/i) {$indexed_doc = 1; last;}
511 } else {
512 if ($field =~ /$exp/) {$indexed_doc = 1; last;}
513 }
514 }
515 }
516 }
517
518 # this is another document
519 $self->{'num_docs'} += 1;
520
521 # get the parameters for the output
522 my ($level, $fields) = split (/:/, $self->{'index'});
523 $fields =~ s/\ball\b/Title,Creator,text/;
524 $fields =~ s/\btopall\b/topTitle,topCreator,toptext/;
525
526 my $doc_section = 0; # just for this document
527 my $text = "";
528 my $text_extra = "";
529
530 # get the text for this document
531 my $section = $doc_obj->get_top_section();
532 while (defined $section) {
533 # update a few statistics
534 $doc_section++;
535 $self->{'num_sections'} += 1;
536
537 if ($indexed_doc) {
538 $self->{'num_bytes'} += $doc_obj->get_text_length ($section);
539 foreach $field (split (/,/, $fields)) {
540 # only deal with this field if it doesn't start with top or
541 # this is the first section
542 my $real_field = $field;
543 if (!($real_field =~ s/^top//) || ($doc_section == 1)) {
544 my $new_text = "";
545 if ($real_field eq "text") {
546 $new_text = $doc_obj->get_text ($section);
547 $new_text =~ s/[\cB\cC]//g;
548 $new_text =~ s/(<p\b)/\cC$1/gi;
549
550 } else {
551 $new_text = join ("\cC", @{$doc_obj->get_metadata ($section, $real_field)});
552 }
553
554 $text .= "$new_text\cC";
555 }
556 }
557 }
558
559 if ($level eq "document") { $text_extra .= "\cB"; }
560 else { $text .= "\cB"; }
561
562 $section = $doc_obj->get_next_section($section);
563 }
564
565 print $handle "$text$text_extra";
566}
567
568# converts leading number in classification back
569# to letter it represents
570# i.e 67.2.4 becomes C.2.4
571sub char_classification {
572 my $self = shift (@_);
573 my ($classification) = @_;
574
575 return $classification if $classification eq "";
576 my ($c) = $classification =~ /^\.?(\d+)/;
577 $c = chr($c);
578 $classification =~ s/^\d+/$c/;
579
580 return $classification;
581}
582
583# converts leading letter of a classification into its ascii equivalent
584# i.e C.2.4 becomes 67.2.4
585sub int_classification {
586 my $self = shift (@_);
587 my ($classification) = @_;
588 my $c = ord($classification);
589 $classification =~ s/^./$c/;
590
591 return $classification;
592}
5931;
594
Note: See TracBrowser for help on using the repository browser.