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

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

fixed a small bug

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 17.5 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 if ($child =~ /^.*?\.(\d+)$/) {
163 print $handle "\".$1";
164 } else {
165 print $handle "\".$child";
166 }
167# if ($child eq "") { print $handle "$doc_OID"; }
168# elsif ($section eq "") { print $handle "$doc_OID.$child"; }
169# else { print $handle "$doc_OID.$section.$child"; }
170 }
171 print $handle "\n";
172 }
173
174 # output the matching document number
175 print $handle "<docnum>$self->{'num_sections'}\n";
176
177 print $handle '-' x 70, "\n";
178
179
180 # output a database entry for the document number
181 print $handle "[$self->{'num_sections'}]\n";
182 if ($section eq "") { print $handle "<section>$doc_OID\n"; }
183 else { print $handle "<section>$doc_OID.$section\n"; }
184 print $handle '-' x 70, "\n";
185
186
187 $section = $doc_obj->get_next_section($section);
188 }
189
190 # classify this document
191 &classify::classify_doc ($self->{'classifiers'}, $doc_obj);
192
193}
194
195
196
197sub infodb {
198 my $self = shift (@_);
199 my ($doc_obj, $filename) = @_;
200 my $handle = $self->{'output_handle'};
201
202 if ($doc_obj eq 'classifications') {
203 # output classifications if all books have been processed
204 foreach $key (keys %$saved_classifications) {
205 $saved_classifications->{$key}->{'contains'} = undef if
206 $saved_classifications->{$key}->{'contains'} eq "";
207 $saved_classifications->{$key}->{'parent'} = undef if
208 (!defined $saved_classifications->{$key}->{'parent'}) ||
209 ($saved_classifications->{$key}->{'parent'} eq "");
210 $self->write_to_gdbm ($handle, $key, $saved_classifications->{$key}->{'title'},
211 undef, undef, undef, undef,
212 $saved_classifications->{$key}->{'contains'}, undef,
213 $saved_classifications->{$key}->{'parent'}, undef, undef);
214 }
215 return;
216 }
217
218 my $doctype = $doc_obj->get_doc_type();
219
220 # only output this document if it is one to be indexed
221 return if ($doctype ne "indexed_doc" &&
222 $doctype ne "classification");
223
224 # found classification document
225 $saved_classifications = {} if ($doctype eq "classification");
226 $sectionmap = {};
227
228 # this is another document
229 $self->{'num_docs'} += 1 unless ($doctype eq "classification");
230
231 my $section = $doc_obj->get_top_section();
232 while (defined $section) {
233 # update a few statistics
234 $self->{'num_bytes'} += $doc_obj->get_text_length ($section);
235 $self->{'num_sections'} += 1 unless ($doctype eq "classification");
236
237 my $title = $doc_obj->get_metadata_element($section, "Title");
238 my $source = $doc_obj->get_metadata_element($section, "Source");
239 my $date = $doc_obj->get_metadata_element($section, "Date");
240 my $jobnumber = $doc_obj->get_source_filename();
241
242 my $mapped_section = $self->map_section($doctype, $section);
243 if ($doctype eq "classification") {
244 $mapped_section = $self->char_classification($mapped_section);
245 } else {
246 $mapped_section = "B.$self->{'num_docs'}.$mapped_section";
247 }
248 $mapped_section =~ s/\.+$//;
249
250 my ($parent, $classification, $creator);
251
252 $classification = $self->get_classifications($doc_obj, $section, $mapped_section)
253 unless $doctype eq "classification";
254
255 if ($section ne $doc_obj->get_top_section()) {
256 $parent = $self->map_section($doctype, $doc_obj->get_parent_section($section));
257 if ($doctype eq "classification") {
258 $parent = $self->char_classification($parent);
259 } else {
260 $parent = "B.$self->{'num_docs'}.$parent";
261 }
262 $parent =~ s/\.+$//;
263 } else {
264 $creator = $doc_obj->get_metadata_element($section, "Creator");
265
266 # need filename so we know what directory to look in for associated files
267 $filename =~ s/^\/?(.*?\.dir).*$/$1/ if (defined $filename);
268 }
269
270 if ($doc_obj->is_leaf_section($section)) {
271 if ($doctype eq "classification") {
272 if (defined $saved_classifications->{$mapped_section}) {
273 print STDERR "mgbuildproc:warning: classification $mapped_section " .
274 "declared multiple times\n";
275 } else {
276 $saved_classifications->{$mapped_section} = {'title' => $title, 'contains' => "",
277 'parent' => $parent};
278 }
279 } else {
280 $self->write_to_gdbm($handle, $mapped_section, $title, $creator, $source, $date, $jobnumber, undef,
281 $self->{'num_sections'}, $parent, $classification, $filename);
282 }
283 } else {
284
285 # add the introductory section if it exists
286 my $contains = "";
287 if ($doc_obj->get_text_length($section) > 0) {
288 $contains .= "B.$self->{'num_docs'}." . $self->map_section ($doctype, "$section.0");
289 }
290
291 # add the rest of the children
292 my @children = @{$doc_obj->get_children($section)};
293 if ($doctype eq "classification") {
294 map {$_ = $self->char_classification($_);} @children;
295 if (defined $saved_classifications->{$mapped_section}) {
296 print STDERR "mgbuildproc:warning: classification $mapped_section " .
297 "declared multiple times\n";
298 } else {
299 $saved_classifications->{$mapped_section} = {'title' => $title, 'contains' => ""};
300 $saved_classifications->{$mapped_section}->{'contains'} = join ":", @children;
301 $saved_classifications->{$mapped_section}->{'parent'} = $parent;
302 }
303 } else {
304 map {$_ = "B.$self->{'num_docs'}." . $self->map_section($doctype, $_);} @children;
305 $contains .= ":" if $contains ne "";
306 $contains .= join ":", @children;
307 $self->write_to_gdbm ($handle, $mapped_section, $title, $creator, $source,
308 $date, $jobnumber, $contains, $self->{'num_sections'},
309 $parent, $classification, $filename);
310
311 if ($doc_obj->get_text_length($section) > 0) {
312 my $intromapsection = "B.$self->{'num_docs'}." .
313 $self->map_section($doctype, "$section.0");
314 $self->write_to_gdbm ($handle, $intromapsection, "<i>(introductory text)</i>", $creator,
315 $source, $date, $jobnumber, undef, $self->{'num_sections'},
316 $mapped_section, $classification, $filename);
317 }
318 }
319 }
320 $section = $doc_obj->get_next_section($section);
321 }
322
323 # update classification list with those books that
324 # were processed before classification list
325 if ($doctype eq "classification" && defined $temp_classifications) {
326 foreach $key (keys(%$temp_classifications)) {
327 if (!defined $saved_classifications->{$key}) {
328 print STDERR "mgbuildproc:$temp_classifications->{$key} belong to " .
329 "undefined classification $key\n";
330 }
331 $saved_classifications->{$key}->{'contains'} = $temp_classifications->{$key}->{'contains'};
332 }
333 $temp_classifications = undef;
334 }
335}
336
337
338sub write_to_gdbm {
339 my $self = shift (@_);
340 my ($handle, $section, $title, $creator, $source, $date,
341 $jobnumber, $contains, $docnum, $parent, $classification, $OID) = @_;
342
343 print $handle "[$section]\n";
344 print $handle "<t>$title\n" if (defined $title && $title ne "");
345 print $handle "<a>$creator\n" if (defined $creator && $creator ne "");
346 print $handle "<s>$source\n" if (defined $source && $source ne "");
347 print $handle "<i>$date\n" if (defined $date && $date ne "");
348 print $handle "<j>$jobnumber\n" if (defined $jobnumber && $jobnumber ne "");
349 print $handle "<c>$contains\n" if (defined $contains && $contains ne "");
350 print $handle "<d>$docnum\n" if (defined $docnum && $docnum ne "");
351 print $handle "<p>$parent\n" if (defined $parent && $parent ne "");
352 print $handle "<x>$classification\n" if (defined $classification && $classification ne "");
353 print $handle "<o>$OID\n" if defined $OID;
354 print $handle '-' x 70, "\n";
355
356 if (defined $docnum) {
357 print $handle "[$docnum]\n";
358 print $handle "<x>$section\n";
359 print $handle '-' x 70, "\n";
360 }
361}
362
363sub get_classifications {
364 my $self = shift (@_);
365 my ($doc_obj, $section, $mapped_section) = @_;
366
367 my ($classificationsref);
368 if (defined $saved_classifications) {
369 # classification list has been processed
370 $classificationsref = $saved_classifications;
371 } else {
372 # classification list has yet to be processed, save
373 # books in temp_classifications until list is processed
374 # and they can be moved into saved_classifications
375 $temp_classifications = {} unless defined $temp_classifications;
376 $classificationsref = $temp_classifications;
377 }
378
379 my $classifications = $doc_obj->get_metadata($section, "Subject");
380
381 # need to save which books belong in each classification
382 # to output later
383 foreach $classification (@$classifications) {
384
385 if (!defined $classificationsref->{$classification}) {
386 $classificationsref->{$classification}->{'parent'} = $doc_obj->get_parent_section($section);
387 $classificationsref->{$classification}->{'contains'} = "";
388 }
389 $classificationsref->{$classification}->{'contains'} .= ":" unless
390 $classificationsref->{$classification}->{'contains'} eq "";
391 $classificationsref->{$classification}->{'contains'} .= $mapped_section;
392 }
393
394 return (join ":", @$classifications);
395}
396
397sub map_section {
398 my $self = shift (@_);
399 my ($doctype, $section) = @_;
400
401 # classifications should never need to be mapped
402 return $section if $doctype eq "classification";
403
404 return "" unless (defined $section) && ($section ne "");
405
406 $sectionmap = {} unless defined $sectionmap;
407
408
409
410 # get the section into a standard format
411 $section =~ s/^\.+|\.+$//g;
412
413 # return the mapped section if it has been seen before
414 if (defined $sectionmap->{$section}) {
415 return $sectionmap->{$section};
416 }
417
418 # find out the parent section
419 my ($parentsection, $num);
420 if ($section =~ /^(.+)\.(\d+)$/) {
421 $parentsection = $1;
422 $num = $2;
423 } elsif ($section =~ /^(\d+)$/) {
424 $parentsection = "";
425 $num = $1;
426 } else {
427 print STDERR "mgbuildproc:map_section - misformed section $section\n";
428 }
429 $parentsection = "" unless defined $parentsection;
430
431 if ($parentsection eq "") {
432 $num ++ if $num == 1 and defined $sectionmap->{'0'};
433 } else {
434 $num ++ if $num == 1 and defined $sectionmap->{"$parentsection.0"};
435 }
436
437 # find out the mapped parent section
438 my $mappedparentsection = $self->map_section ($doctype, $parentsection);
439
440 # find the next unused child section
441 my $previousnum = $num - 1;
442 my $previoussection = "";
443 if ($parentsection eq "") {
444 $previoussection = $previousnum;
445 $previoussection = 0 if ($section == 1) && (defined $sectionmap->{'0'});
446 } else{
447 $previoussection = "$parentsection.$previousnum";
448 $previoussection = "$parentsection.0" if ($section =~ /\.1$/) && (defined $sectionmap->{"$parentsection.0"});
449 }
450 while (($previousnum > 0) && (!defined $sectionmap->{$previoussection})) {
451 $previousnum--;
452 $previoussection = "$parentsection.$previousnum";
453 $previoussection = $previousnum if $parentsection eq "";
454 }
455
456 # there has been no children under this parent, this section will be number 1
457 if ($previousnum <= 0) {
458 if ($mappedparentsection eq "") {
459 $sectionmap->{$section} = "1";
460 } else {
461 $sectionmap->{$section} = "$mappedparentsection.1";
462 }
463
464 } else {
465 # get the previous mapped number
466 my $previousmapnum = 0;
467 if ($sectionmap->{$previoussection} =~ /(^|\.)?(\d+)$/) {
468 $previousmapnum = $2;
469 }
470
471 # increment it to get this mapped child
472 my $mappednum = $previousmapnum+1;
473
474 if ($mappedparentsection eq "") {
475 $sectionmap->{$section} = $mappednum;
476 } else {
477 $sectionmap->{$section} = "$mappedparentsection.$mappednum";
478 }
479 }
480
481 return $sectionmap->{$section};
482}
483
484sub text {
485 my $self = shift (@_);
486 my ($doc_obj) = @_;
487 my $handle = $self->{'output_handle'};
488 my $indexed_doc = 1;
489
490 # only output this document if it is one to be indexed
491 return if ($doc_obj->get_doc_type() ne "indexed_doc");
492
493 # see if this document belongs to this subcollection
494 foreach $indexexp (@{$self->{'indexexparr'}}) {
495 $indexed_doc = 0;
496 my ($field, $exp, $options) = split /\//, $indexexp;
497 if (defined ($field) && defined ($exp)) {
498 my ($bool) = $field =~ /^(.)/;
499 $field =~ s/^.// if $bool eq '!';
500 if ($field eq "filename") {
501 $field = $doc_obj->get_source_filename();
502 } else {
503 $field = $doc_obj->get_metadata_element($doc_obj->get_top_section(), $field);
504 }
505 next unless defined $field;
506 if ($bool eq '!') {
507 if ($options =~ /^i$/i) {
508 if ($field !~ /$exp/i) {$indexed_doc = 1; last;}
509 } else {
510 if ($field !~ /$exp/) {$indexed_doc = 1; last;}
511 }
512 } else {
513 if ($options =~ /^i$/i) {
514 if ($field =~ /$exp/i) {$indexed_doc = 1; last;}
515 } else {
516 if ($field =~ /$exp/) {$indexed_doc = 1; last;}
517 }
518 }
519 }
520 }
521
522 # this is another document
523 $self->{'num_docs'} += 1;
524
525 # get the parameters for the output
526 my ($level, $fields) = split (/:/, $self->{'index'});
527 $fields =~ s/\ball\b/Title,Creator,text/;
528 $fields =~ s/\btopall\b/topTitle,topCreator,toptext/;
529
530 my $doc_section = 0; # just for this document
531 my $text = "";
532 my $text_extra = "";
533
534 # get the text for this document
535 my $section = $doc_obj->get_top_section();
536 while (defined $section) {
537 # update a few statistics
538 $doc_section++;
539 $self->{'num_sections'} += 1;
540
541 if ($indexed_doc) {
542 $self->{'num_bytes'} += $doc_obj->get_text_length ($section);
543 foreach $field (split (/,/, $fields)) {
544 # only deal with this field if it doesn't start with top or
545 # this is the first section
546 my $real_field = $field;
547 if (!($real_field =~ s/^top//) || ($doc_section == 1)) {
548 my $new_text = "";
549 if ($real_field eq "text") {
550 $new_text = $doc_obj->get_text ($section);
551 $new_text =~ s/[\cB\cC]//g;
552 $new_text =~ s/(<p\b)/\cC$1/gi;
553
554 } else {
555 $new_text = join ("\cC", @{$doc_obj->get_metadata ($section, $real_field)});
556 }
557
558 $text .= "$new_text\cC";
559 }
560 }
561 }
562
563 if ($level eq "document") { $text_extra .= "\cB"; }
564 else { $text .= "\cB"; }
565
566 $section = $doc_obj->get_next_section($section);
567 }
568
569 print $handle "$text$text_extra";
570}
571
572# converts leading number in classification back
573# to letter it represents
574# i.e 67.2.4 becomes C.2.4
575sub char_classification {
576 my $self = shift (@_);
577 my ($classification) = @_;
578
579 return $classification if $classification eq "";
580 my ($c) = $classification =~ /^\.?(\d+)/;
581 $c = chr($c);
582 $classification =~ s/^\d+/$c/;
583
584 return $classification;
585}
586
587# converts leading letter of a classification into its ascii equivalent
588# i.e C.2.4 becomes 67.2.4
589sub int_classification {
590 my $self = shift (@_);
591 my ($classification) = @_;
592 my $c = ord($classification);
593 $classification =~ s/^./$c/;
594
595 return $classification;
596}
5971;
598
Note: See TracBrowser for help on using the repository browser.