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