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

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

* empty log message *

  • Property svn:keywords set to Author Date Id Revision
File size: 24.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\n";
122}
123
124# print_usage should be overridden for any sub-classes having
125# their own plugin specific options
126sub print_usage {
127 print STDERR "\nThis plugin has no plugin specific options\n\n";
128}
129
130sub new {
131 my $class = shift (@_);
132 my $plugin_name = shift (@_);
133 my $self = {};
134
135 my $enc = "^(";
136 map {$enc .= "$_|";} keys %$encodings::encodings;
137 my $denc = $enc . "ascii|utf8|unicode)\$";
138 $enc .= "ascii|utf8|unicode|auto)\$";
139
140 $self->{'outhandle'} = STDERR;
141 my $year = (localtime)[5]+1900;
142
143 $self->{'textcat'} = new textcat();
144
145 $self->{'num_processed'} = 0;
146 $self->{'num_not_processed'} = 0;
147 $self->{'num_blocked'} = 0;
148 $self->{'num_archives'} = 0;
149
150 # general options available to all plugins
151 if (!parsargv::parse(\@_,
152 q^process_exp/.*/^, \$self->{'process_exp'},
153 q^block_exp/.*/^, \$self->{'block_exp'},
154 q^extract_acronyms^, \$self->{'extract_acronyms'},
155 q^extract_keyphrases^, \$self->{'kea'}, #with extra options
156 q^extract_keyphrase_options/.*/^, \$self->{'kea_options'}, #no extra options
157 qq^input_encoding/$enc/auto^, \$self->{'input_encoding'},
158 qq^default_encoding/$denc/iso_8859_1^, \$self->{'default_encoding'},
159 q^extract_email^, \$self->{'extract_email'},
160 q^markup_acronyms^, \$self->{'markup_acronyms'},
161 q^default_language/.{2}/en^, \$self->{'default_language'},
162 q^first/.*/^, \$self->{'first'},
163 q^extract_date^, \$self->{'date_extract'},
164 qq^maximum_date/\\d{4}/$year^, \$self->{'max_year'},
165 q^no_bibliography^, \$self->{'no_biblio'},
166 qq^maximum_century/-?\\d{1,2}( ?B\\.C\\.E\\.)?/-1^, \$self->{'max_century'},
167 "allow_extra_options")) {
168
169 print STDERR "\nThe $plugin_name plugin uses an incorrect general option (general options are those\n";
170 print STDERR "available to all plugins). Check your collect.cfg configuration file.\n";
171 &print_general_usage($plugin_name);
172 die "\n";
173 }
174
175 return bless $self, $class;
176}
177
178# initialize BasPlug options
179# if init() is overridden in a sub-class, remember to call BasPlug::init()
180sub init {
181 my $self = shift (@_);
182 my ($verbosity, $outhandle, $failhandle) = @_;
183
184 # verbosity is passed through from the processor
185 $self->{'verbosity'} = $verbosity;
186
187 # as are the outhandle and failhandle
188 $self->{'outhandle'} = $outhandle if defined $outhandle;
189 $self->{'failhandle'} = $failhandle;
190
191 # set process_exp and block_exp to defaults unless they were
192 # explicitly set
193
194 if ((!$self->is_recursive()) and
195 (!defined $self->{'process_exp'}) || ($self->{'process_exp'} eq "")) {
196
197 $self->{'process_exp'} = $self->get_default_process_exp ();
198 if ($self->{'process_exp'} eq "") {
199 warn ref($self) . " Warning: Non-recursive plugin has no process_exp\n";
200 }
201 }
202
203 if ((!defined $self->{'block_exp'}) || ($self->{'block_exp'} eq "")) {
204 $self->{'block_exp'} = $self->get_default_block_exp ();
205 }
206}
207
208sub begin {
209 my $self = shift (@_);
210 my ($pluginfo, $base_dir, $processor, $maxdocs) = @_;
211 $self->initialise_extractors();
212}
213
214sub end {
215 my ($self) = @_;
216 $self->finalise_extractors();
217}
218
219# this function should be overridden to return 1
220# in recursive plugins
221sub is_recursive {
222 my $self = shift (@_);
223
224 return 0;
225}
226
227sub get_default_block_exp {
228 my $self = shift (@_);
229
230 return "";
231}
232
233sub get_default_process_exp {
234 my $self = shift (@_);
235
236 return "";
237}
238
239# The BasPlug read() function. This function does all the right things
240# to make general options work for a given plugin. It calls the process()
241# function which does all the work specific to a plugin (like the old
242# read functions used to do). Most plugins should define their own
243# process() function and let this read() function keep control.
244#
245# recursive plugins (e.g. RecPlug) and specialized plugins like those
246# capable of processing many documents within a single file (e.g.
247# GMLPlug) should normally implement their own version of read()
248#
249# Return number of files processed, undef if can't process
250# Note that $base_dir might be "" and that $file might
251# include directories
252
253sub read {
254 my $self = shift (@_);
255
256 my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs) = @_;
257
258 if ($self->is_recursive()) {
259 die "BasPlug::read function must be implemented in sub-class for recursive plugins\n";
260 }
261
262 my $outhandle = $self->{'outhandle'};
263
264 my $filename = $file;
265 $filename = &util::filename_cat ($base_dir, $file) if $base_dir =~ /\w/;
266
267 if ($self->{'block_exp'} ne "" && $filename =~ /$self->{'block_exp'}/) {
268 $self->{'num_blocked'} ++;
269 return 0;
270 }
271 if ($filename !~ /$self->{'process_exp'}/ || !-f $filename) {
272 return undef;
273 }
274 my $plugin_name = ref ($self);
275 $file =~ s/^[\/\\]+//; # $file often begins with / so we'll tidy it up
276
277 my ($language, $encoding);
278 if ($self->{'input_encoding'} eq "auto") {
279 # use textcat to automatically work out the input encoding and language
280 ($language, $encoding) = $self->get_language_encoding ($filename);
281
282 } elsif ($self->{'extract_language'}) {
283 # use textcat to get language metadata
284 ($language, $extracted_encoding) = $self->get_language_encoding ($filename);
285 $encoding = $self->{'input_encoding'};
286
287 if ($extracted_encoding ne $encoding && $self->{'verbosity'}) {
288 print $outhandle "$plugin_name: WARNING: $file was read using $encoding encoding but ";
289 print $outhandle "appears to be encoded as $extracted_encoding.\n";
290 }
291
292 } else {
293 $language = $self->{'default_language'};
294 $encoding = $self->{'input_encoding'};
295 }
296
297 # create a new document
298 my $doc_obj = new doc ($filename, "indexed_doc");
299 $doc_obj->set_OIDtype ($processor->{'OIDtype'});
300 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Language", $language);
301 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Encoding", $encoding);
302 my ($filemeta) = $file =~ /([^\\\/]+)$/;
303 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Source", &ghtml::dmsafe($filemeta));
304
305 # read in file ($text will be in utf8)
306 my $text = "";
307 $self->read_file ($filename, $encoding, $language, \$text);
308
309 if (!length ($text)) {
310 print $outhandle "$plugin_name: ERROR: $file contains no text\n" if $self->{'verbosity'};
311
312 my $failhandle = $self->{'failhandle'};
313 print $failhandle "$file: " . ref($self) . ": file contains no text\n";
314 $self->{'num_not_processed'} ++;
315
316 return 0;
317 }
318
319 # include any metadata passed in from previous plugins
320 # note that this metadata is associated with the top level section
321 $self->extra_metadata ($doc_obj, $doc_obj->get_top_section(), $metadata);
322
323 # do plugin specific processing of doc_obj
324 return undef unless defined ($self->process (\$text, $pluginfo, $base_dir, $file, $metadata, $doc_obj));
325
326 # do any automatic metadata extraction
327 $self->auto_extract_metadata ($doc_obj);
328
329 # add an OID
330 $doc_obj->set_OID();
331
332 # process the document
333 $processor->process($doc_obj);
334
335 $self->{'num_processed'} ++;
336
337 return 1; # processed the file
338}
339
340# returns undef if file is rejected by the plugin
341sub process {
342 my $self = shift (@_);
343 my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj) = @_;
344
345 die "Basplug::process function must be implemented in sub-class\n";
346
347 return undef; # never gets here
348}
349
350# uses the multiread package to read in the entire file pointed to
351# by filename and loads the resulting text into $$textref. Input text
352# may be in any of the encodings handled by multiread, output text
353# will be in utf8
354sub read_file {
355 my $self = shift (@_);
356 my ($filename, $encoding, $language, $textref) = @_;
357
358 if (!-r $filename)
359 {
360 my $outhandle = $self->{'outhandle'};
361 print $outhandle "Read permission denied for $filename\n" if $self->{'verbosity'};
362 return;
363 }
364
365 $$textref = "";
366
367 open (FILE, $filename) || die "BasPlug::read_file could not open $filename for reading ($!)\n";
368
369 if ($encoding eq "ascii") {
370 undef $/;
371 $$textref = <FILE>;
372 $/ = "\n";
373 } else {
374 my $reader = new multiread();
375 $reader->set_handle ('BasPlug::FILE');
376 $reader->set_encoding ($encoding);
377 $reader->read_file ($textref);
378
379 if ($language eq "zh") {
380 # segment the Chinese words
381 $$textref = &cnseg::segment($$textref);
382 }
383 }
384
385 close FILE;
386}
387
388# Uses textcat to work out the encoding and language of the text in
389# $filename. All html tags are removed before processing.
390# returns an array containing "language" and "encoding"
391sub get_language_encoding {
392 my $self = shift (@_);
393 my ($filename) = @_;
394 my $outhandle = $self->{'outhandle'};
395
396 # read in file
397 open (FILE, $filename) || die "BasPlug::get_language_encoding could not open $filename for reading ($!)\n";
398 undef $/;
399 my $text = <FILE>;
400 $/ = "\n";
401 close FILE;
402
403 # remove <title>stuff</title> -- as titles tend often to be in English
404 # for foreign language documents
405 $text =~ s/<title>.*?<\/title>//i;
406
407 # remove all HTML tags
408 $text =~ s/<[^>]*>//sg;
409
410 # get the language/encoding
411 my $results = $self->{'textcat'}->classify(\$text);
412
413 # if textcat returns 3 or less possibilities we'll use the
414 # first one in the list - otherwise use the defaults
415 if (scalar @$results > 3) {
416
417 if ($self->{'input_encoding'} ne 'auto') {
418 if ($self->{'extract_language'} && $self->{'verbosity'}) {
419 print $outhandle "BasPlug: WARNING: language could not be extracted from $filename - ";
420 print $outhandle "defaulting to $self->{'default_language'}\n";
421 }
422 return ($self->{'default_language'}, $self->{'input_encoding'});
423
424 } else {
425 if ($self->{'verbosity'}) {
426 print $outhandle "BASPlug: WARNING: language/encoding could not be extracted from $filename - ";
427 print $outhandle "defaulting to $self->{'default_language'}/$self->{'default_encoding'}\n";
428 }
429 return ($self->{'default_language'}, $self->{'default_encoding'});
430 }
431 }
432
433 # format language/encoding
434 my ($language, $encoding) = $results->[0] =~ /^([^-]*)(?:-(.*))?$/;
435 if (!defined $language) {
436 if ($self->{'verbosity'}) {
437 print $outhandle "BasPlug: WARNING: language could not be extracted from $filename - ";
438 print $outhandle "defaulting to $self->{'default_language'}\n";
439 }
440 $language = $self->{'default_language'};
441 }
442 if (!defined $encoding) {
443 if ($self->{'verbosity'}) {
444 print $outhandle "BasPlug: WARNING: encoding could not be extracted from $filename - ";
445 print $outhandle "defaulting to $self->{'default_encoding'}\n";
446 }
447 $encoding = $self->{'default_encoding'};
448 }
449
450 if ($encoding !~ /^(ascii|utf8|unicode)$/ &&
451 !defined $encodings::encodings->{$encoding}) {
452 if ($self->{'verbosity'}) {
453 print $outhandle "BasPlug: WARNING: $filename appears to be encoded in an unsupported encoding ($encoding) - ";
454 print $outhandle "using $self->{'default_encoding'}\n";
455 }
456 $encoding = $self->{'default_encoding'};
457 }
458
459 return ($language, $encoding);
460}
461
462# add any extra metadata that's been passed around from one
463# plugin to another.
464# extra_metadata uses add_utf8_metadata so it expects metadata values
465# to already be in utf8
466sub extra_metadata {
467 my $self = shift (@_);
468 my ($doc_obj, $cursection, $metadata) = @_;
469
470 foreach my $field (keys(%$metadata)) {
471 # $metadata->{$field} may be an array reference
472 if (ref ($metadata->{$field}) eq "ARRAY") {
473 map {
474 $doc_obj->add_utf8_metadata ($cursection, $field, $_);
475 } @{$metadata->{$field}};
476 } else {
477 $doc_obj->add_utf8_metadata ($cursection, $field, $metadata->{$field});
478 }
479 }
480}
481
482# initialise metadata extractors
483sub initialise_extractors {
484 my $self = shift (@_);
485
486 if ($self->{'extract_acronyms'} || $self->{'markup_acronyms'}) {
487 &acronym::initialise_acronyms();
488 }
489}
490
491# finalise metadata extractors
492sub finalise_extractors {
493 my $self = shift (@_);
494
495 if ($self->{'extract_acronyms'} || $self->{'markup_acronyms'}) {
496 &acronym::finalise_acronyms();
497 }
498}
499
500# FIRSTNNN: extract the first NNN characters as metadata
501sub extract_first_NNNN_characters {
502 my $self = shift (@_);
503 my ($textref, $doc_obj, $thissection) = @_;
504
505 foreach my $size (split /,/, $self->{'first'}) {
506 my $tmptext = $$textref;
507 $tmptext =~ s/^\s+//;
508 $tmptext =~ s/\s+$//;
509 $tmptext =~ s/\s+/ /gs;
510 $tmptext = substr ($tmptext, 0, $size);
511 $tmptext =~ s/\s\S*$/&#8230;/;
512 $doc_obj->add_utf8_metadata ($thissection, "First$size", $tmptext);
513 }
514}
515
516sub extract_email {
517 my $self = shift (@_);
518 my ($textref, $doc_obj, $thissection) = @_;
519 my $outhandle = $self->{'outhandle'};
520
521 print $outhandle " extracting email addresses ...\n"
522 if ($self->{'verbosity'} > 2);
523
524 my @email = ($$textref =~ m/([-a-z0-9\.@+_=]+@(?:[-a-z0-9]+\.)+(?:com|org|edu|mil|int|net|[a-z][a-z]))/g);
525 @email = sort @email;
526
527 my @email2 = ();
528 foreach my $address (@email) {
529 if (!(join(" ",@email2) =~ m/$address/ )) {
530 push @email2, $address;
531 $doc_obj->add_utf8_metadata ($thissection, "emailAddress", $address);
532 print $outhandle " extracting $address\n"
533 if ($self->{'verbosity'} > 3);
534 }
535 }
536 print $outhandle " done extracting email addresses.\n"
537 if ($self->{'verbosity'} > 2);
538
539}
540
541# extract metadata
542sub auto_extract_metadata {
543
544
545 my $self = shift (@_);
546 my ($doc_obj) = @_;
547
548 if ($self->{'extract_email'}) {
549 my $thissection = $doc_obj->get_top_section();
550 while (defined $thissection) {
551 my $text = $doc_obj->get_text($thissection);
552 $self->extract_email (\$text, $doc_obj, $thissection) if $text =~ /./;
553 $thissection = $doc_obj->get_next_section ($thissection);
554 }
555 }
556
557
558#adding kea keyphrases
559 if ($self->{'kea'}) {
560
561 my $thissection = $doc_obj->get_top_section();
562 my $text = "";
563 my @list;
564
565 while (defined $thissection) { #loop through sections to gather whole doc
566 my $sectiontext = $doc_obj->get_text($thissection);
567 $text = $text.$sectiontext;
568 $thissection = $doc_obj->get_next_section ($thissection);
569 }
570
571 if($self->{'kea_options'}) { #if kea options flag is set, call Kea with specified options
572 @list = &Kea::extract_KeyPhrases ($text, $self->{'kea_options'});
573 } else { #otherwise call Kea with no options
574 @list = &Kea::extract_KeyPhrases ($text);
575 }
576
577 if(@list){ #if a list of kea keyphrases was returned (ie not empty)
578 my $keyphrases = $list[0]; #first arg is keyphrase list
579 my $stems = $list[1]; #second arg is stemmed keyphrase list
580 print STDERR "keyphrases: $keyphrases\n";
581 print STDERR "stems: $stems\n";
582 $thissection = $doc_obj->get_top_section(); #add metadata to top section
583 $doc_obj->add_metadata($thissection, "kea", $keyphrases);
584 $doc_obj->add_metadata($thissection, "stems", $stems);
585 }
586 } #end of kea
587
588 if ($self->{'first'}) {
589 my $thissection = $doc_obj->get_top_section();
590 while (defined $thissection) {
591 my $text = $doc_obj->get_text($thissection);
592 $self->extract_first_NNNN_characters (\$text, $doc_obj, $thissection) if $text =~ /./;
593 $thissection = $doc_obj->get_next_section ($thissection);
594 }
595 }
596
597 if ($self->{'extract_acronyms'}) {
598 my $thissection = $doc_obj->get_top_section();
599 while (defined $thissection) {
600 my $text = $doc_obj->get_text($thissection);
601 $self->extract_acronyms (\$text, $doc_obj, $thissection) if $text =~ /./;
602 $thissection = $doc_obj->get_next_section ($thissection);
603 }
604 }
605
606 if ($self->{'markup_acronyms'}) {
607 my $thissection = $doc_obj->get_top_section();
608 while (defined $thissection) {
609 my $text = $doc_obj->get_text($thissection);
610 $text = $self->markup_acronyms ($text, $doc_obj, $thissection);
611 $doc_obj->delete_text($thissection);
612 $doc_obj->add_text($thissection, $text);
613 $thissection = $doc_obj->get_next_section ($thissection);
614 }
615 }
616
617 if($self->{'date_extract'}) {
618 my $thissection = $doc_obj->get_top_section();
619 while (defined $thissection) {
620
621 my $text = $doc_obj->get_text($thissection);
622 &DateExtract::get_date_metadata($text, $doc_obj,
623 $thissection,
624 $self->{'no_biblio'},
625 $self->{'max_year'},
626 $self->{'max_century'});
627 $thissection = $doc_obj->get_next_section ($thissection);
628 }
629 }
630}
631
632# extract acronyms from a section in a document. progress is
633# reported to outhandle based on the verbosity. both the Acronym
634# and the AcronymKWIC metadata items are created.
635
636sub extract_acronyms {
637 my $self = shift (@_);
638 my ($textref, $doc_obj, $thissection) = @_;
639 my $outhandle = $self->{'outhandle'};
640
641 print $outhandle " extracting acronyms ...\n"
642 if ($self->{'verbosity'} > 2);
643
644 my $acro_array = &acronym::acronyms($textref);
645
646 foreach my $acro (@$acro_array) {
647
648 #check that this is the first time ...
649 my $seen_before = "false";
650 my $previous_data = $doc_obj->get_metadata($thissection, "Acronym");
651 foreach my $thisAcro (@$previous_data) {
652 if ($thisAcro eq $acro->to_string()) {
653 $seen_before = "true";
654 print $outhandle " already seen ". $acro->to_string() . "\n"
655 if ($self->{'verbosity'} >= 4);
656 }
657 }
658
659 if ($seen_before eq "false") {
660 #write it to the file ...
661 $acro->write_to_file();
662
663 #do the normal acronym
664 $doc_obj->add_utf8_metadata($thissection, "Acronym", $acro->to_string());
665 print $outhandle " adding ". $acro->to_string() . "\n"
666 if ($self->{'verbosity'} > 3);
667
668 }
669 }
670 print $outhandle " done extracting acronyms. \n"
671 if ($self->{'verbosity'} > 2);
672}
673
674sub markup_acronyms {
675 my $self = shift (@_);
676 my ($text, $doc_obj, $thissection) = @_;
677 my $outhandle = $self->{'outhandle'};
678
679 print $outhandle " marking up acronyms ...\n"
680 if ($self->{'verbosity'} > 2);
681
682 #self is passed in to check for verbosity ...
683 $text = &acronym::markup_acronyms($text, $self);
684
685 print $outhandle " done marking up acronyms. \n"
686 if ($self->{'verbosity'} > 2);
687
688 return $text;
689}
690
691sub compile_stats {
692 my $self = shift(@_);
693 my ($stats) = @_;
694
695 $stats->{'num_processed'} += $self->{'num_processed'};
696 $stats->{'num_not_processed'} += $self->{'num_not_processed'};
697 $stats->{'num_archives'} += $self->{'num_archives'};
698
699}
700
7011;
Note: See TracBrowser for help on using the repository browser.