source: trunk/gsdl/perllib/plugins/BasPlug.pm@ 2816

Last change on this file since 2816 was 2816, checked in by sjboddie, 23 years ago

Added cover_image option to BasPlug for associating a jpeg image as a
cover image (like the old HBPlug used to do).

  • Property svn:keywords set to Author Date Id Revision
File size: 25.6 KB
Line 
1###########################################################################
2#
3# BasPlug.pm -- base class for all the import plugins
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
26package BasPlug;
27
28# suppress the annoying "subroutine redefined" warning that various
29# plugins cause under perl 5.6
30$SIG{__WARN__} = sub {warn($_[0]) unless ($_[0] =~ /Subroutine\s+\S+\sredefined/)};
31
32use Kea;
33use parsargv;
34use multiread;
35use encodings;
36use cnseg;
37use acronym;
38use textcat;
39use doc;
40use diagnostics;
41use DateExtract;
42use ghtml;
43
44sub print_general_usage {
45 my ($plugin_name) = @_;
46
47 print STDERR "\n usage: plugin $plugin_name [options]\n\n";
48
49 print STDERR " -process_exp A perl regular expression to match against filenames.\n";
50 print STDERR " Matching filenames will be processed by this plugin.\n";
51 print STDERR " Each plugin has its own default process_exp. e.g HTMLPlug\n";
52 print STDERR " defaults to '(?i)\.html?\$' i.e. all documents ending in\n";
53 print STDERR " .htm or .html (case-insensitive).\n\n";
54
55 print STDERR " -block_exp Files matching this regular expression will be blocked from\n";
56 print STDERR " being passed to any later plugins in the list. This has no\n";
57 print STDERR " real effect other than to prevent lots of warning messages\n";
58 print STDERR " about input files you don't care about. Each plugin might\n";
59 print STDERR " have a default block_exp. e.g. by default HTMLPlug blocks\n";
60 print STDERR " any files with .gif, .jpg, .jpeg, .png or .css\n";
61 print STDERR " file extensions.\n\n";
62
63
64 print STDERR " -input_encoding The encoding of the source documents. Documents will be\n";
65 print STDERR " converted from these encodings and stored internally as\n";
66 print STDERR " utf8. The default input_encoding is 'auto'. Accepted values\n";
67 print STDERR " are:\n";
68
69 print STDERR " auto: Use text categorization algorithm to automatically\n";
70 print STDERR " identify the encoding of each source document. This\n";
71 print STDERR " will be slower than explicitly setting the encoding\n";
72 print STDERR " but will work where more than one encoding is used\n";
73 print STDERR " within the same collection.\n";
74
75 print STDERR " ascii: Plain 7 bit ascii. This may be a bit faster than\n";
76 print STDERR " using iso_8859_1. Beware of using this on a collection\n";
77 print STDERR " of documents that may contain characters outside the\n";
78 print STDERR " plain 7 bit ascii set though (e.g. German or French\n";
79 print STDERR " documents containing accents), use iso_8859_1 instead.\n";
80
81 print STDERR " utf8: either utf8 or unicode -- automatically detected\n";
82 print STDERR " unicode: just unicode\n";
83
84 my $e = $encodings::encodings;
85 foreach my $enc (sort {$e->{$a}->{'name'} cmp $e->{$b}->{'name'}} keys (%$e)) {
86 print STDERR " $enc: $e->{$enc}->{'name'}\n";
87 }
88 print STDERR "\n";
89 print STDERR " -default_encoding Use this encoding if -input_encoding is set to 'auto' and\n";
90 print STDERR " the text categorization algorithm fails to extract the\n";
91 print STDERR " encoding or extracts an encoding unsupported by Greenstone.\n";
92 print STDERR " The default is iso_8859_1.\n\n";
93
94 print STDERR " -extract_language Identify the language of each document and set 'Language'\n";
95 print STDERR " metadata. Note that this will be done automatically if\n";
96 print STDERR " -input_encoding is 'auto'.\n\n";
97 print STDERR " -default_language If Greenstone fails to work out what language a document is\n";
98 print STDERR " the 'Language' metadata element will be set to this value.\n";
99 print STDERR " The default is 'en' (ISO 639 language symbols are used:\n";
100 print STDERR " en = English). Note that if -input_encoding is not set to\n";
101 print STDERR " 'auto' and -extract_language is not set, all documents will\n";
102 print STDERR " have their 'Language' metadata set to this value.\n\n";
103
104 print STDERR " -extract_acronyms Extract acronyms from within text and set as metadata\n";
105
106 print STDERR " -markup_acronyms Add acronym metadata into document text\n\n";
107
108 print STDERR " -first Comma separated list of first sizes to extract from the\n";
109 print STDERR " text into a metadata field. The field is called 'FirstNNN'.\n\n";
110
111 print STDERR " -extract_email Extract email addresses as metadata\n\n";
112
113 print STDERR " -extract_date Extract dates pertaining to the content of documents about\n";
114 print STDERR " history\n";
115 print STDERR " -maximum_date The maximum historical date to be used as metadata (in a\n";
116 print STDERR " Common Era date, such as 1950)\n";
117 print STDERR " -maximum_century The maximum named century to be extracted as historical\n";
118 print STDERR " metadata (e.g. 14 will extract all references up to the\n";
119 print STDERR " 14th century)\n";
120 print STDERR " -no_bibliography Do not try and block bibliographic dates when extracting\n";
121 print STDERR " historical dates.\n";
122 print STDERR " -cover_image Will look for a prefix.jpg file (where prefix is the same\n";
123 print STDERR " prefix as the file being processed) and associate it as a\n";
124 print STDERR " cover image\n\n";
125}
126
127# print_usage should be overridden for any sub-classes having
128# their own plugin specific options
129sub print_usage {
130 print STDERR "\nThis plugin has no plugin specific options\n\n";
131}
132
133sub new {
134 my $class = shift (@_);
135 my $plugin_name = shift (@_);
136 my $self = {};
137
138 my $enc = "^(";
139 map {$enc .= "$_|";} keys %$encodings::encodings;
140 my $denc = $enc . "ascii|utf8|unicode)\$";
141 $enc .= "ascii|utf8|unicode|auto)\$";
142
143 $self->{'outhandle'} = STDERR;
144 my $year = (localtime)[5]+1900;
145
146 $self->{'textcat'} = new textcat();
147
148 $self->{'num_processed'} = 0;
149 $self->{'num_not_processed'} = 0;
150 $self->{'num_blocked'} = 0;
151 $self->{'num_archives'} = 0;
152
153 # general options available to all plugins
154 if (!parsargv::parse(\@_,
155 q^process_exp/.*/^, \$self->{'process_exp'},
156 q^block_exp/.*/^, \$self->{'block_exp'},
157 q^extract_acronyms^, \$self->{'extract_acronyms'},
158 q^extract_keyphrases^, \$self->{'kea'}, #with extra options
159 q^extract_keyphrase_options/.*/^, \$self->{'kea_options'}, #no extra options
160 qq^input_encoding/$enc/auto^, \$self->{'input_encoding'},
161 qq^default_encoding/$denc/iso_8859_1^, \$self->{'default_encoding'},
162 q^extract_email^, \$self->{'extract_email'},
163 q^markup_acronyms^, \$self->{'markup_acronyms'},
164 q^default_language/.{2}/en^, \$self->{'default_language'},
165 q^first/.*/^, \$self->{'first'},
166 q^extract_date^, \$self->{'date_extract'},
167 qq^maximum_date/\\d{4}/$year^, \$self->{'max_year'},
168 q^no_bibliography^, \$self->{'no_biblio'},
169 qq^maximum_century/-?\\d{1,2}( ?B\\.C\\.E\\.)?/-1^, \$self->{'max_century'},
170 q^cover_image^, \$self->{'cover_image'},
171 "allow_extra_options")) {
172
173 print STDERR "\nThe $plugin_name plugin uses an incorrect general option (general options are those\n";
174 print STDERR "available to all plugins). Check your collect.cfg configuration file.\n";
175 &print_general_usage($plugin_name);
176 die "\n";
177 }
178
179 return bless $self, $class;
180}
181
182# initialize BasPlug options
183# if init() is overridden in a sub-class, remember to call BasPlug::init()
184sub init {
185 my $self = shift (@_);
186 my ($verbosity, $outhandle, $failhandle) = @_;
187
188 # verbosity is passed through from the processor
189 $self->{'verbosity'} = $verbosity;
190
191 # as are the outhandle and failhandle
192 $self->{'outhandle'} = $outhandle if defined $outhandle;
193 $self->{'failhandle'} = $failhandle;
194
195 # set process_exp and block_exp to defaults unless they were
196 # explicitly set
197
198 if ((!$self->is_recursive()) and
199 (!defined $self->{'process_exp'}) || ($self->{'process_exp'} eq "")) {
200
201 $self->{'process_exp'} = $self->get_default_process_exp ();
202 if ($self->{'process_exp'} eq "") {
203 warn ref($self) . " Warning: Non-recursive plugin has no process_exp\n";
204 }
205 }
206
207 if ((!defined $self->{'block_exp'}) || ($self->{'block_exp'} eq "")) {
208 $self->{'block_exp'} = $self->get_default_block_exp ();
209 }
210}
211
212sub begin {
213 my $self = shift (@_);
214 my ($pluginfo, $base_dir, $processor, $maxdocs) = @_;
215 $self->initialise_extractors();
216}
217
218sub end {
219 my ($self) = @_;
220 $self->finalise_extractors();
221}
222
223# this function should be overridden to return 1
224# in recursive plugins
225sub is_recursive {
226 my $self = shift (@_);
227
228 return 0;
229}
230
231sub get_default_block_exp {
232 my $self = shift (@_);
233
234 return "";
235}
236
237sub get_default_process_exp {
238 my $self = shift (@_);
239
240 return "";
241}
242
243# The BasPlug read() function. This function does all the right things
244# to make general options work for a given plugin. It calls the process()
245# function which does all the work specific to a plugin (like the old
246# read functions used to do). Most plugins should define their own
247# process() function and let this read() function keep control.
248#
249# recursive plugins (e.g. RecPlug) and specialized plugins like those
250# capable of processing many documents within a single file (e.g.
251# GMLPlug) should normally implement their own version of read()
252#
253# Return number of files processed, undef if can't process
254# Note that $base_dir might be "" and that $file might
255# include directories
256
257sub read {
258 my $self = shift (@_);
259
260 my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs) = @_;
261
262 if ($self->is_recursive()) {
263 die "BasPlug::read function must be implemented in sub-class for recursive plugins\n";
264 }
265
266 my $outhandle = $self->{'outhandle'};
267
268 my $filename = $file;
269 $filename = &util::filename_cat ($base_dir, $file) if $base_dir =~ /\w/;
270
271 if ($self->{'block_exp'} ne "" && $filename =~ /$self->{'block_exp'}/) {
272 $self->{'num_blocked'} ++;
273 return 0;
274 }
275 if ($filename !~ /$self->{'process_exp'}/ || !-f $filename) {
276 return undef;
277 }
278 $file =~ s/^[\/\\]+//; # $file often begins with / so we'll tidy it up
279
280 # Do encoding stuff
281 my ($language, $encoding) = $self->textcat_get_language_encoding ($filename);
282
283 # create a new document
284 my $doc_obj = new doc ($filename, "indexed_doc");
285 $doc_obj->set_OIDtype ($processor->{'OIDtype'});
286 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Language", $language);
287 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Encoding", $encoding);
288 my ($filemeta) = $file =~ /([^\\\/]+)$/;
289 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Source", &ghtml::dmsafe($filemeta));
290 if ($self->{'cover_image'}) {
291 $self->associate_cover_image($doc_obj, $filename);
292 }
293
294 # read in file ($text will be in utf8)
295 my $text = "";
296 $self->read_file ($filename, $encoding, $language, \$text);
297
298 if (!length ($text)) {
299 my $plugin_name = ref ($self);
300 print $outhandle "$plugin_name: ERROR: $file contains no text\n" if $self->{'verbosity'};
301
302 my $failhandle = $self->{'failhandle'};
303 print $failhandle "$file: " . ref($self) . ": file contains no text\n";
304 $self->{'num_not_processed'} ++;
305
306 return 0;
307 }
308
309 # include any metadata passed in from previous plugins
310 # note that this metadata is associated with the top level section
311 $self->extra_metadata ($doc_obj, $doc_obj->get_top_section(), $metadata);
312
313 # do plugin specific processing of doc_obj
314 return undef unless defined ($self->process (\$text, $pluginfo, $base_dir, $file, $metadata, $doc_obj));
315
316 # do any automatic metadata extraction
317 $self->auto_extract_metadata ($doc_obj);
318
319 # add an OID
320 $doc_obj->set_OID();
321
322 # process the document
323 $processor->process($doc_obj);
324
325 $self->{'num_processed'} ++;
326
327 return 1; # processed the file
328}
329
330# returns undef if file is rejected by the plugin
331sub process {
332 my $self = shift (@_);
333 my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj) = @_;
334
335 die "Basplug::process function must be implemented in sub-class\n";
336
337 return undef; # never gets here
338}
339
340# uses the multiread package to read in the entire file pointed to
341# by filename and loads the resulting text into $$textref. Input text
342# may be in any of the encodings handled by multiread, output text
343# will be in utf8
344sub read_file {
345 my $self = shift (@_);
346 my ($filename, $encoding, $language, $textref) = @_;
347
348 if (!-r $filename)
349 {
350 my $outhandle = $self->{'outhandle'};
351 print $outhandle "Read permission denied for $filename\n" if $self->{'verbosity'};
352 return;
353 }
354
355 $$textref = "";
356
357 open (FILE, $filename) || die "BasPlug::read_file could not open $filename for reading ($!)\n";
358
359 if ($encoding eq "ascii") {
360 undef $/;
361 $$textref = <FILE>;
362 $/ = "\n";
363 } else {
364 my $reader = new multiread();
365 $reader->set_handle ('BasPlug::FILE');
366 $reader->set_encoding ($encoding);
367 $reader->read_file ($textref);
368
369 if ($language eq "zh") {
370 # segment the Chinese words
371 $$textref = &cnseg::segment($$textref);
372 }
373 }
374
375 close FILE;
376}
377
378sub textcat_get_language_encoding {
379 my $self = shift (@_);
380 my ($filename) = @_;
381
382 my ($language, $encoding, $extracted_encoding);
383 if ($self->{'input_encoding'} eq "auto") {
384 # use textcat to automatically work out the input encoding and language
385 ($language, $encoding) = $self->get_language_encoding ($filename);
386 } elsif ($self->{'extract_language'}) {
387 # use textcat to get language metadata
388 ($language, $extracted_encoding) = $self->get_language_encoding ($filename);
389 $encoding = $self->{'input_encoding'};
390 if ($extracted_encoding ne $encoding && $self->{'verbosity'}) {
391 my $plugin_name = ref ($self);
392 my $outhandle = $self->{'outhandle'};
393 print $outhandle "$plugin_name: WARNING: $filename was read using $encoding encoding but ";
394 print $outhandle "appears to be encoded as $extracted_encoding.\n";
395 }
396 } else {
397 $language = $self->{'default_language'};
398 $encoding = $self->{'input_encoding'};
399 }
400 return ($language, $encoding);
401}
402
403# Uses textcat to work out the encoding and language of the text in
404# $filename. All html tags are removed before processing.
405# returns an array containing "language" and "encoding"
406sub get_language_encoding {
407 my $self = shift (@_);
408 my ($filename) = @_;
409 my $outhandle = $self->{'outhandle'};
410
411 # read in file
412 open (FILE, $filename) || die "BasPlug::get_language_encoding could not open $filename for reading ($!)\n";
413 undef $/;
414 my $text = <FILE>;
415 $/ = "\n";
416 close FILE;
417
418 # remove <title>stuff</title> -- as titles tend often to be in English
419 # for foreign language documents
420 $text =~ s/<title>.*?<\/title>//i;
421
422 # remove all HTML tags
423 $text =~ s/<[^>]*>//sg;
424
425 # get the language/encoding
426 my $results = $self->{'textcat'}->classify(\$text);
427
428 # if textcat returns 3 or less possibilities we'll use the
429 # first one in the list - otherwise use the defaults
430 if (scalar @$results > 3) {
431
432 if ($self->{'input_encoding'} ne 'auto') {
433 if ($self->{'extract_language'} && $self->{'verbosity'}) {
434 print $outhandle "BasPlug: WARNING: language could not be extracted from $filename - ";
435 print $outhandle "defaulting to $self->{'default_language'}\n";
436 }
437 return ($self->{'default_language'}, $self->{'input_encoding'});
438
439 } else {
440 if ($self->{'verbosity'}) {
441 print $outhandle "BASPlug: WARNING: language/encoding could not be extracted from $filename - ";
442 print $outhandle "defaulting to $self->{'default_language'}/$self->{'default_encoding'}\n";
443 }
444 return ($self->{'default_language'}, $self->{'default_encoding'});
445 }
446 }
447
448 # format language/encoding
449 my ($language, $encoding) = $results->[0] =~ /^([^-]*)(?:-(.*))?$/;
450 if (!defined $language) {
451 if ($self->{'verbosity'}) {
452 print $outhandle "BasPlug: WARNING: language could not be extracted from $filename - ";
453 print $outhandle "defaulting to $self->{'default_language'}\n";
454 }
455 $language = $self->{'default_language'};
456 }
457 if (!defined $encoding) {
458 if ($self->{'verbosity'}) {
459 print $outhandle "BasPlug: WARNING: encoding could not be extracted from $filename - ";
460 print $outhandle "defaulting to $self->{'default_encoding'}\n";
461 }
462 $encoding = $self->{'default_encoding'};
463 }
464
465 if ($encoding !~ /^(ascii|utf8|unicode)$/ &&
466 !defined $encodings::encodings->{$encoding}) {
467 if ($self->{'verbosity'}) {
468 print $outhandle "BasPlug: WARNING: $filename appears to be encoded in an unsupported encoding ($encoding) - ";
469 print $outhandle "using $self->{'default_encoding'}\n";
470 }
471 $encoding = $self->{'default_encoding'};
472 }
473
474 return ($language, $encoding);
475}
476
477# add any extra metadata that's been passed around from one
478# plugin to another.
479# extra_metadata uses add_utf8_metadata so it expects metadata values
480# to already be in utf8
481sub extra_metadata {
482 my $self = shift (@_);
483 my ($doc_obj, $cursection, $metadata) = @_;
484
485 foreach my $field (keys(%$metadata)) {
486 # $metadata->{$field} may be an array reference
487 if (ref ($metadata->{$field}) eq "ARRAY") {
488 map {
489 $doc_obj->add_utf8_metadata ($cursection, $field, $_);
490 } @{$metadata->{$field}};
491 } else {
492 $doc_obj->add_utf8_metadata ($cursection, $field, $metadata->{$field});
493 }
494 }
495}
496
497# initialise metadata extractors
498sub initialise_extractors {
499 my $self = shift (@_);
500
501 if ($self->{'extract_acronyms'} || $self->{'markup_acronyms'}) {
502 &acronym::initialise_acronyms();
503 }
504}
505
506# finalise metadata extractors
507sub finalise_extractors {
508 my $self = shift (@_);
509
510 if ($self->{'extract_acronyms'} || $self->{'markup_acronyms'}) {
511 &acronym::finalise_acronyms();
512 }
513}
514
515# FIRSTNNN: extract the first NNN characters as metadata
516sub extract_first_NNNN_characters {
517 my $self = shift (@_);
518 my ($textref, $doc_obj, $thissection) = @_;
519
520 foreach my $size (split /,/, $self->{'first'}) {
521 my $tmptext = $$textref;
522 $tmptext =~ s/^\s+//;
523 $tmptext =~ s/\s+$//;
524 $tmptext =~ s/\s+/ /gs;
525 $tmptext = substr ($tmptext, 0, $size);
526 $tmptext =~ s/\s\S*$/&#8230;/;
527 $doc_obj->add_utf8_metadata ($thissection, "First$size", $tmptext);
528 }
529}
530
531sub extract_email {
532 my $self = shift (@_);
533 my ($textref, $doc_obj, $thissection) = @_;
534 my $outhandle = $self->{'outhandle'};
535
536 print $outhandle " extracting email addresses ...\n"
537 if ($self->{'verbosity'} > 2);
538
539 my @email = ($$textref =~ m/([-a-z0-9\.@+_=]+@(?:[-a-z0-9]+\.)+(?:com|org|edu|mil|int|net|[a-z][a-z]))/g);
540 @email = sort @email;
541
542 my @email2 = ();
543 foreach my $address (@email) {
544 if (!(join(" ",@email2) =~ m/$address/ )) {
545 push @email2, $address;
546 $doc_obj->add_utf8_metadata ($thissection, "emailAddress", $address);
547 print $outhandle " extracting $address\n"
548 if ($self->{'verbosity'} > 3);
549 }
550 }
551 print $outhandle " done extracting email addresses.\n"
552 if ($self->{'verbosity'} > 2);
553
554}
555
556# extract metadata
557sub auto_extract_metadata {
558
559
560 my $self = shift (@_);
561 my ($doc_obj) = @_;
562
563 if ($self->{'extract_email'}) {
564 my $thissection = $doc_obj->get_top_section();
565 while (defined $thissection) {
566 my $text = $doc_obj->get_text($thissection);
567 $self->extract_email (\$text, $doc_obj, $thissection) if $text =~ /./;
568 $thissection = $doc_obj->get_next_section ($thissection);
569 }
570 }
571
572
573#adding kea keyphrases
574 if ($self->{'kea'}) {
575
576 my $thissection = $doc_obj->get_top_section();
577 my $text = "";
578 my @list;
579
580 while (defined $thissection) { #loop through sections to gather whole doc
581 my $sectiontext = $doc_obj->get_text($thissection);
582 $text = $text.$sectiontext;
583 $thissection = $doc_obj->get_next_section ($thissection);
584 }
585
586 if($self->{'kea_options'}) { #if kea options flag is set, call Kea with specified options
587 @list = &Kea::extract_KeyPhrases ($text, $self->{'kea_options'});
588 } else { #otherwise call Kea with no options
589 @list = &Kea::extract_KeyPhrases ($text);
590 }
591
592 if(@list){ #if a list of kea keyphrases was returned (ie not empty)
593 my $keyphrases = $list[0]; #first arg is keyphrase list
594 my $stems = $list[1]; #second arg is stemmed keyphrase list
595 print STDERR "keyphrases: $keyphrases\n";
596 print STDERR "stems: $stems\n";
597 $thissection = $doc_obj->get_top_section(); #add metadata to top section
598 $doc_obj->add_metadata($thissection, "kea", $keyphrases);
599 $doc_obj->add_metadata($thissection, "stems", $stems);
600 }
601 } #end of kea
602
603 if ($self->{'first'}) {
604 my $thissection = $doc_obj->get_top_section();
605 while (defined $thissection) {
606 my $text = $doc_obj->get_text($thissection);
607 $self->extract_first_NNNN_characters (\$text, $doc_obj, $thissection) if $text =~ /./;
608 $thissection = $doc_obj->get_next_section ($thissection);
609 }
610 }
611
612 if ($self->{'extract_acronyms'}) {
613 my $thissection = $doc_obj->get_top_section();
614 while (defined $thissection) {
615 my $text = $doc_obj->get_text($thissection);
616 $self->extract_acronyms (\$text, $doc_obj, $thissection) if $text =~ /./;
617 $thissection = $doc_obj->get_next_section ($thissection);
618 }
619 }
620
621 if ($self->{'markup_acronyms'}) {
622 my $thissection = $doc_obj->get_top_section();
623 while (defined $thissection) {
624 my $text = $doc_obj->get_text($thissection);
625 $text = $self->markup_acronyms ($text, $doc_obj, $thissection);
626 $doc_obj->delete_text($thissection);
627 $doc_obj->add_text($thissection, $text);
628 $thissection = $doc_obj->get_next_section ($thissection);
629 }
630 }
631
632 if($self->{'date_extract'}) {
633 my $thissection = $doc_obj->get_top_section();
634 while (defined $thissection) {
635
636 my $text = $doc_obj->get_text($thissection);
637 &DateExtract::get_date_metadata($text, $doc_obj,
638 $thissection,
639 $self->{'no_biblio'},
640 $self->{'max_year'},
641 $self->{'max_century'});
642 $thissection = $doc_obj->get_next_section ($thissection);
643 }
644 }
645}
646
647# extract acronyms from a section in a document. progress is
648# reported to outhandle based on the verbosity. both the Acronym
649# and the AcronymKWIC metadata items are created.
650
651sub extract_acronyms {
652 my $self = shift (@_);
653 my ($textref, $doc_obj, $thissection) = @_;
654 my $outhandle = $self->{'outhandle'};
655
656 print $outhandle " extracting acronyms ...\n"
657 if ($self->{'verbosity'} > 2);
658
659 my $acro_array = &acronym::acronyms($textref);
660
661 foreach my $acro (@$acro_array) {
662
663 #check that this is the first time ...
664 my $seen_before = "false";
665 my $previous_data = $doc_obj->get_metadata($thissection, "Acronym");
666 foreach my $thisAcro (@$previous_data) {
667 if ($thisAcro eq $acro->to_string()) {
668 $seen_before = "true";
669 print $outhandle " already seen ". $acro->to_string() . "\n"
670 if ($self->{'verbosity'} >= 4);
671 }
672 }
673
674 if ($seen_before eq "false") {
675 #write it to the file ...
676 $acro->write_to_file();
677
678 #do the normal acronym
679 $doc_obj->add_utf8_metadata($thissection, "Acronym", $acro->to_string());
680 print $outhandle " adding ". $acro->to_string() . "\n"
681 if ($self->{'verbosity'} > 3);
682
683 }
684 }
685 print $outhandle " done extracting acronyms. \n"
686 if ($self->{'verbosity'} > 2);
687}
688
689sub markup_acronyms {
690 my $self = shift (@_);
691 my ($text, $doc_obj, $thissection) = @_;
692 my $outhandle = $self->{'outhandle'};
693
694 print $outhandle " marking up acronyms ...\n"
695 if ($self->{'verbosity'} > 2);
696
697 #self is passed in to check for verbosity ...
698 $text = &acronym::markup_acronyms($text, $self);
699
700 print $outhandle " done marking up acronyms. \n"
701 if ($self->{'verbosity'} > 2);
702
703 return $text;
704}
705
706sub compile_stats {
707 my $self = shift(@_);
708 my ($stats) = @_;
709
710 $stats->{'num_processed'} += $self->{'num_processed'};
711 $stats->{'num_not_processed'} += $self->{'num_not_processed'};
712 $stats->{'num_archives'} += $self->{'num_archives'};
713
714}
715
716sub associate_cover_image {
717 my $self = shift(@_);
718 my ($doc_obj, $filename) = @_;
719
720 $filename =~ s/\.[^\\\/\.]+$/\.jpg/;
721 if (-e $filename) {
722 $doc_obj->associate_file($filename, "cover.jpg", "image/jpeg");
723 }
724}
725
7261;
Note: See TracBrowser for help on using the repository browser.