[537] | 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 |
|
---|
[17] | 26 | # This document processor outputs a document
|
---|
| 27 | # for mg to process
|
---|
[4] | 28 |
|
---|
| 29 |
|
---|
| 30 | package mgbuildproc;
|
---|
| 31 |
|
---|
[215] | 32 | use classify;
|
---|
| 33 | use doc;
|
---|
[4] | 34 | use docproc;
|
---|
| 35 | use util;
|
---|
| 36 |
|
---|
| 37 |
|
---|
| 38 | BEGIN {
|
---|
| 39 | @ISA = ('docproc');
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 |
|
---|
| 43 | sub new {
|
---|
| 44 | my ($class, $collection, $source_dir, $build_dir, $verbosity) = @_;
|
---|
| 45 | my $self = new docproc ();
|
---|
| 46 |
|
---|
| 47 | $self->{'collection'} = $collection;
|
---|
| 48 | $self->{'source_dir'} = $source_dir;
|
---|
| 49 | $self->{'build_dir'} = $build_dir;
|
---|
| 50 | $self->{'verbosity'} = $verbosity;
|
---|
[215] | 51 | $self->{'classifiers'} = [];
|
---|
[4] | 52 | $self->{'mode'} = "text";
|
---|
| 53 | $self->{'index'} = "section:text";
|
---|
[69] | 54 | $self->{'indexexparr'} = [];
|
---|
[4] | 55 | $self->{'output_handle'} = "STDOUT";
|
---|
| 56 | $self->{'num_docs'} = 0;
|
---|
| 57 | $self->{'num_sections'} = 0;
|
---|
| 58 | $self->{'num_bytes'} = 0;
|
---|
| 59 |
|
---|
[292] | 60 | $self->{'indexing_text'} = 0;
|
---|
| 61 |
|
---|
[4] | 62 | return bless $self, $class;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | sub reset {
|
---|
| 66 | my $self = shift (@_);
|
---|
| 67 |
|
---|
| 68 | $self->{'num_docs'} = 0;
|
---|
| 69 | $self->{'num_sections'} = 0;
|
---|
| 70 | $self->{'num_bytes'} = 0;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | sub get_num_docs {
|
---|
| 74 | my $self = shift (@_);
|
---|
| 75 |
|
---|
| 76 | return $self->{'num_docs'};
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | sub get_num_sections {
|
---|
| 80 | my $self = shift (@_);
|
---|
| 81 |
|
---|
| 82 | return $self->{'num_sections'};
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | sub get_num_bytes {
|
---|
| 86 | my $self = shift (@_);
|
---|
| 87 |
|
---|
| 88 | return $self->{'num_bytes'};
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | sub set_output_handle {
|
---|
| 92 | my $self = shift (@_);
|
---|
| 93 | my ($handle) = @_;
|
---|
| 94 |
|
---|
| 95 | $self->{'output_handle'} = $handle;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | sub set_mode {
|
---|
| 99 | my $self = shift (@_);
|
---|
| 100 | my ($mode) = @_;
|
---|
| 101 |
|
---|
| 102 | $self->{'mode'} = $mode;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | sub set_index {
|
---|
| 106 | my $self = shift (@_);
|
---|
[69] | 107 | my ($index, $indexexparr) = @_;
|
---|
[4] | 108 |
|
---|
| 109 | $self->{'index'} = $index;
|
---|
[69] | 110 | $self->{'indexexparr'} = $indexexparr if defined $indexexparr;
|
---|
[4] | 111 | }
|
---|
| 112 |
|
---|
[215] | 113 | sub set_classifiers {
|
---|
| 114 | my $self = shift (@_);
|
---|
| 115 | my ($classifiers) = @_;
|
---|
| 116 |
|
---|
| 117 | $self->{'classifiers'} = $classifiers;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
[292] | 120 | sub set_indexing_text {
|
---|
| 121 | my $self = shift (@_);
|
---|
| 122 | my ($indexing_text) = @_;
|
---|
| 123 |
|
---|
| 124 | $self->{'indexing_text'} = $indexing_text;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
[4] | 127 | sub process {
|
---|
| 128 | my $self = shift (@_);
|
---|
| 129 | my $method = $self->{'mode'};
|
---|
| 130 |
|
---|
[139] | 131 | $self->$method(@_);
|
---|
[4] | 132 | }
|
---|
| 133 |
|
---|
[677] | 134 | # use 'Paged' if document has no more than 2 levels
|
---|
| 135 | # and each section at second level has a number for
|
---|
| 136 | # Title metadata
|
---|
| 137 | sub get_document_type {
|
---|
| 138 | my $self = shift (@_);
|
---|
| 139 | my ($doc_obj) = @_;
|
---|
| 140 |
|
---|
| 141 | my $thistype = "VList";
|
---|
| 142 | my $childtype = "VList";
|
---|
| 143 | my $title;
|
---|
| 144 | my @tmp = ();
|
---|
| 145 |
|
---|
| 146 | my $section = $doc_obj->get_top_section ();
|
---|
| 147 | my $first = 1;
|
---|
| 148 | while (defined $section) {
|
---|
| 149 | @tmp = split /\./, $section;
|
---|
| 150 | if (scalar(@tmp) > 1) {
|
---|
| 151 | return ($thistype, $childtype);
|
---|
| 152 | }
|
---|
| 153 | if (!$first) {
|
---|
| 154 | $title = $doc_obj->get_metadata_element ($section, "Title");
|
---|
[706] | 155 | if (!defined $title || $title !~ /^\d+$/) {
|
---|
[677] | 156 | return ($thistype, $childtype);
|
---|
| 157 | }
|
---|
| 158 | }
|
---|
| 159 | $first = 0;
|
---|
| 160 | $section = $doc_obj->get_next_section($section);
|
---|
| 161 | }
|
---|
| 162 | if ($doc_obj->get_text_length ($doc_obj->get_top_section())) {
|
---|
| 163 | $thistype = "Paged";
|
---|
| 164 | } else {
|
---|
| 165 | $thistype = "Invisible";
|
---|
| 166 | }
|
---|
| 167 | $childtype = "Paged";
|
---|
| 168 | return ($thistype, $childtype);
|
---|
| 169 | }
|
---|
| 170 |
|
---|
[315] | 171 | sub infodb {
|
---|
[215] | 172 | my $self = shift (@_);
|
---|
| 173 | my ($doc_obj, $filename) = @_;
|
---|
| 174 | my $handle = $self->{'output_handle'};
|
---|
| 175 | # $handle = "main::STDOUT";
|
---|
| 176 |
|
---|
| 177 | my $doctype = $doc_obj->get_doc_type();
|
---|
| 178 |
|
---|
| 179 | # only output this document if it is one to be indexed
|
---|
| 180 | return if ($doctype ne "indexed_doc");
|
---|
| 181 |
|
---|
| 182 | # this is another document
|
---|
| 183 | $self->{'num_docs'} += 1 unless ($doctype eq "classification");
|
---|
| 184 |
|
---|
[677] | 185 | # is this a paged or a hierarchical document
|
---|
| 186 | my ($thistype, $childtype) = $self->get_document_type ($doc_obj);
|
---|
| 187 |
|
---|
[215] | 188 | my $section = $doc_obj->get_top_section ();
|
---|
| 189 | my $doc_OID = $doc_obj->get_OID();
|
---|
[677] | 190 | my $first = 1;
|
---|
[215] | 191 | while (defined $section) {
|
---|
| 192 | # update a few statistics
|
---|
| 193 | $self->{'num_bytes'} += $doc_obj->get_text_length ($section);
|
---|
| 194 | $self->{'num_sections'} += 1 unless ($doctype eq "classification");
|
---|
| 195 |
|
---|
| 196 | # output the section name
|
---|
| 197 | if ($section eq "") { print $handle "[$doc_OID]\n"; }
|
---|
| 198 | else { print $handle "[$doc_OID.$section]\n"; }
|
---|
| 199 |
|
---|
| 200 | # output the fact that this document is a document
|
---|
| 201 | print $handle "<doctype>doc\n";
|
---|
| 202 |
|
---|
| 203 | # output whether this node contains text
|
---|
| 204 | if ($doc_obj->get_text_length($section) > 0) {
|
---|
| 205 | print $handle "<hastxt>1\n";
|
---|
| 206 | } else {
|
---|
| 207 | print $handle "<hastxt>0\n";
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | # output all the section metadata
|
---|
| 211 | my $metadata = $doc_obj->get_all_metadata ($section);
|
---|
| 212 | foreach $pair (@$metadata) {
|
---|
| 213 | my ($field, $value) = (@$pair);
|
---|
| 214 |
|
---|
| 215 | if ($field ne "Identifier" && $field !~ /^gsdl/ &&
|
---|
| 216 | defined $value && $value ne "") {
|
---|
| 217 | # escape problematic stuff
|
---|
| 218 | $value =~ s/\\/\\\\/g;
|
---|
| 219 | $value =~ s/\n/\\n/g;
|
---|
| 220 | $value =~ s/\r/\\r/g;
|
---|
| 221 |
|
---|
| 222 | print $handle "<$field>$value\n";
|
---|
| 223 | }
|
---|
| 224 | }
|
---|
| 225 |
|
---|
[255] | 226 | # output archivedir if at top level
|
---|
| 227 | if ($section eq $doc_obj->get_top_section()) {
|
---|
| 228 | my ($archivedir) = $filename =~ /^(.*?)(?:\/|\\)[^\/\\]*$/;
|
---|
| 229 | $archivedir = "" unless defined $archivedir;
|
---|
[740] | 230 | $archivedir =~ s/\\/\//g;
|
---|
| 231 | $archivedir =~ s/^\/+//;
|
---|
| 232 | $archivedir =~ s/\/+$//;
|
---|
[255] | 233 | print $handle "<archivedir>$archivedir\n";
|
---|
| 234 | }
|
---|
| 235 |
|
---|
[677] | 236 | # output document display type
|
---|
| 237 | if ($first) {
|
---|
| 238 | print $handle "<thistype>$thistype\n";
|
---|
| 239 | }
|
---|
| 240 |
|
---|
[215] | 241 | # output a list of children
|
---|
[222] | 242 | my $children = $doc_obj->get_children ($section);
|
---|
[215] | 243 | if (scalar(@$children) > 0) {
|
---|
[677] | 244 | print $handle "<childtype>$childtype\n";
|
---|
[215] | 245 | print $handle "<contains>";
|
---|
| 246 | my $firstchild = 1;
|
---|
| 247 | foreach $child (@$children) {
|
---|
| 248 | print $handle ";" unless $firstchild;
|
---|
| 249 | $firstchild = 0;
|
---|
[252] | 250 | if ($child =~ /^.*?\.(\d+)$/) {
|
---|
| 251 | print $handle "\".$1";
|
---|
| 252 | } else {
|
---|
| 253 | print $handle "\".$child";
|
---|
| 254 | }
|
---|
[222] | 255 | # if ($child eq "") { print $handle "$doc_OID"; }
|
---|
| 256 | # elsif ($section eq "") { print $handle "$doc_OID.$child"; }
|
---|
| 257 | # else { print $handle "$doc_OID.$section.$child"; }
|
---|
[215] | 258 | }
|
---|
| 259 | print $handle "\n";
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | # output the matching document number
|
---|
| 263 | print $handle "<docnum>$self->{'num_sections'}\n";
|
---|
| 264 |
|
---|
| 265 | print $handle '-' x 70, "\n";
|
---|
| 266 |
|
---|
| 267 |
|
---|
| 268 | # output a database entry for the document number
|
---|
| 269 | print $handle "[$self->{'num_sections'}]\n";
|
---|
| 270 | if ($section eq "") { print $handle "<section>$doc_OID\n"; }
|
---|
| 271 | else { print $handle "<section>$doc_OID.$section\n"; }
|
---|
| 272 | print $handle '-' x 70, "\n";
|
---|
| 273 |
|
---|
[677] | 274 | $first = 0;
|
---|
[215] | 275 | $section = $doc_obj->get_next_section($section);
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | # classify this document
|
---|
| 279 | &classify::classify_doc ($self->{'classifiers'}, $doc_obj);
|
---|
| 280 |
|
---|
| 281 | }
|
---|
| 282 |
|
---|
[289] | 283 | sub find_paragraphs {
|
---|
| 284 | $_[1] =~ s/(<p\b)/\cC$1/gi;
|
---|
| 285 | }
|
---|
| 286 |
|
---|
[292] | 287 | sub filter_text {
|
---|
| 288 | # $self->filter_text ($field, $new_text);
|
---|
| 289 | # don't want to do anything for this version, however,
|
---|
| 290 | # in a particular collection you might want to override
|
---|
| 291 | # this method to post-process certain fields depending on
|
---|
| 292 | # the field, or whether we are outputting it for indexing
|
---|
| 293 | }
|
---|
| 294 |
|
---|
[4] | 295 | sub text {
|
---|
| 296 | my $self = shift (@_);
|
---|
| 297 | my ($doc_obj) = @_;
|
---|
| 298 | my $handle = $self->{'output_handle'};
|
---|
[69] | 299 | my $indexed_doc = 1;
|
---|
[4] | 300 |
|
---|
| 301 | # only output this document if it is one to be indexed
|
---|
| 302 | return if ($doc_obj->get_doc_type() ne "indexed_doc");
|
---|
| 303 |
|
---|
[69] | 304 | # see if this document belongs to this subcollection
|
---|
| 305 | foreach $indexexp (@{$self->{'indexexparr'}}) {
|
---|
| 306 | $indexed_doc = 0;
|
---|
| 307 | my ($field, $exp, $options) = split /\//, $indexexp;
|
---|
| 308 | if (defined ($field) && defined ($exp)) {
|
---|
| 309 | my ($bool) = $field =~ /^(.)/;
|
---|
| 310 | $field =~ s/^.// if $bool eq '!';
|
---|
[544] | 311 | if ($field =~ /^filename$/i) {
|
---|
[69] | 312 | $field = $doc_obj->get_source_filename();
|
---|
| 313 | } else {
|
---|
| 314 | $field = $doc_obj->get_metadata_element($doc_obj->get_top_section(), $field);
|
---|
| 315 | }
|
---|
| 316 | next unless defined $field;
|
---|
| 317 | if ($bool eq '!') {
|
---|
| 318 | if ($options =~ /^i$/i) {
|
---|
| 319 | if ($field !~ /$exp/i) {$indexed_doc = 1; last;}
|
---|
| 320 | } else {
|
---|
| 321 | if ($field !~ /$exp/) {$indexed_doc = 1; last;}
|
---|
| 322 | }
|
---|
| 323 | } else {
|
---|
| 324 | if ($options =~ /^i$/i) {
|
---|
| 325 | if ($field =~ /$exp/i) {$indexed_doc = 1; last;}
|
---|
| 326 | } else {
|
---|
| 327 | if ($field =~ /$exp/) {$indexed_doc = 1; last;}
|
---|
| 328 | }
|
---|
| 329 | }
|
---|
| 330 | }
|
---|
| 331 | }
|
---|
| 332 |
|
---|
[4] | 333 | # this is another document
|
---|
| 334 | $self->{'num_docs'} += 1;
|
---|
| 335 |
|
---|
| 336 | # get the parameters for the output
|
---|
| 337 | my ($level, $fields) = split (/:/, $self->{'index'});
|
---|
| 338 | $fields =~ s/\ball\b/Title,Creator,text/;
|
---|
| 339 | $fields =~ s/\btopall\b/topTitle,topCreator,toptext/;
|
---|
| 340 |
|
---|
| 341 | my $doc_section = 0; # just for this document
|
---|
| 342 | my $text = "";
|
---|
| 343 | my $text_extra = "";
|
---|
| 344 |
|
---|
| 345 | # get the text for this document
|
---|
| 346 | my $section = $doc_obj->get_top_section();
|
---|
| 347 | while (defined $section) {
|
---|
| 348 | # update a few statistics
|
---|
| 349 | $doc_section++;
|
---|
| 350 | $self->{'num_sections'} += 1;
|
---|
[69] | 351 |
|
---|
| 352 | if ($indexed_doc) {
|
---|
| 353 | $self->{'num_bytes'} += $doc_obj->get_text_length ($section);
|
---|
| 354 | foreach $field (split (/,/, $fields)) {
|
---|
| 355 | # only deal with this field if it doesn't start with top or
|
---|
| 356 | # this is the first section
|
---|
| 357 | my $real_field = $field;
|
---|
| 358 | if (!($real_field =~ s/^top//) || ($doc_section == 1)) {
|
---|
| 359 | my $new_text = "";
|
---|
| 360 | if ($real_field eq "text") {
|
---|
| 361 | $new_text = $doc_obj->get_text ($section);
|
---|
| 362 | $new_text =~ s/[\cB\cC]//g;
|
---|
[289] | 363 | $self->find_paragraphs($new_text);
|
---|
[69] | 364 |
|
---|
| 365 | } else {
|
---|
| 366 | $new_text = join ("\cC", @{$doc_obj->get_metadata ($section, $real_field)});
|
---|
| 367 | }
|
---|
[4] | 368 |
|
---|
[292] | 369 | # filter the text
|
---|
| 370 | $self->filter_text ($field, $new_text);
|
---|
| 371 |
|
---|
| 372 | if ($self->{'indexing_text'} &&
|
---|
| 373 | $new_text =~ /[\(\)\{\}]/) {
|
---|
| 374 | }
|
---|
| 375 |
|
---|
[69] | 376 | $text .= "$new_text\cC";
|
---|
[4] | 377 | }
|
---|
| 378 | }
|
---|
| 379 | }
|
---|
| 380 |
|
---|
| 381 | if ($level eq "document") { $text_extra .= "\cB"; }
|
---|
| 382 | else { $text .= "\cB"; }
|
---|
| 383 |
|
---|
| 384 | $section = $doc_obj->get_next_section($section);
|
---|
| 385 | }
|
---|
| 386 |
|
---|
| 387 | print $handle "$text$text_extra";
|
---|
| 388 | }
|
---|
| 389 |
|
---|
| 390 | 1;
|
---|
| 391 |
|
---|