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 |
|
---|
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 |
|
---|
32 | use Kea;
|
---|
33 | use parsargv;
|
---|
34 | use multiread;
|
---|
35 | use encodings;
|
---|
36 | use cnseg;
|
---|
37 | use acronym;
|
---|
38 | use textcat;
|
---|
39 | use doc;
|
---|
40 | use diagnostics;
|
---|
41 | use DateExtract;
|
---|
42 | use ghtml;
|
---|
43 |
|
---|
44 | sub 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
|
---|
126 | sub print_usage {
|
---|
127 | print STDERR "\nThis plugin has no plugin specific options\n\n";
|
---|
128 | }
|
---|
129 |
|
---|
130 | sub 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()
|
---|
180 | sub 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 |
|
---|
208 | sub begin {
|
---|
209 | my $self = shift (@_);
|
---|
210 | my ($pluginfo, $base_dir, $processor, $maxdocs) = @_;
|
---|
211 | $self->initialise_extractors();
|
---|
212 | }
|
---|
213 |
|
---|
214 | sub end {
|
---|
215 | my ($self) = @_;
|
---|
216 | $self->finalise_extractors();
|
---|
217 | }
|
---|
218 |
|
---|
219 | # this function should be overridden to return 1
|
---|
220 | # in recursive plugins
|
---|
221 | sub is_recursive {
|
---|
222 | my $self = shift (@_);
|
---|
223 |
|
---|
224 | return 0;
|
---|
225 | }
|
---|
226 |
|
---|
227 | sub get_default_block_exp {
|
---|
228 | my $self = shift (@_);
|
---|
229 |
|
---|
230 | return "";
|
---|
231 | }
|
---|
232 |
|
---|
233 | sub 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 |
|
---|
253 | sub 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 | $file =~ s/^[\/\\]+//; # $file often begins with / so we'll tidy it up
|
---|
275 |
|
---|
276 | # Do encoding stuff
|
---|
277 | my ($language, $encoding) = $self->textcat_get_language_encoding ($filename);
|
---|
278 |
|
---|
279 | # create a new document
|
---|
280 | my $doc_obj = new doc ($filename, "indexed_doc");
|
---|
281 | $doc_obj->set_OIDtype ($processor->{'OIDtype'});
|
---|
282 | $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Language", $language);
|
---|
283 | $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Encoding", $encoding);
|
---|
284 | my ($filemeta) = $file =~ /([^\\\/]+)$/;
|
---|
285 | $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Source", &ghtml::dmsafe($filemeta));
|
---|
286 |
|
---|
287 | # read in file ($text will be in utf8)
|
---|
288 | my $text = "";
|
---|
289 | $self->read_file ($filename, $encoding, $language, \$text);
|
---|
290 |
|
---|
291 | if (!length ($text)) {
|
---|
292 | my $plugin_name = ref ($self);
|
---|
293 | print $outhandle "$plugin_name: ERROR: $file contains no text\n" if $self->{'verbosity'};
|
---|
294 |
|
---|
295 | my $failhandle = $self->{'failhandle'};
|
---|
296 | print $failhandle "$file: " . ref($self) . ": file contains no text\n";
|
---|
297 | $self->{'num_not_processed'} ++;
|
---|
298 |
|
---|
299 | return 0;
|
---|
300 | }
|
---|
301 |
|
---|
302 | # include any metadata passed in from previous plugins
|
---|
303 | # note that this metadata is associated with the top level section
|
---|
304 | $self->extra_metadata ($doc_obj, $doc_obj->get_top_section(), $metadata);
|
---|
305 |
|
---|
306 | # do plugin specific processing of doc_obj
|
---|
307 | return undef unless defined ($self->process (\$text, $pluginfo, $base_dir, $file, $metadata, $doc_obj));
|
---|
308 |
|
---|
309 | # do any automatic metadata extraction
|
---|
310 | $self->auto_extract_metadata ($doc_obj);
|
---|
311 |
|
---|
312 | # add an OID
|
---|
313 | $doc_obj->set_OID();
|
---|
314 |
|
---|
315 | # process the document
|
---|
316 | $processor->process($doc_obj);
|
---|
317 |
|
---|
318 | $self->{'num_processed'} ++;
|
---|
319 |
|
---|
320 | return 1; # processed the file
|
---|
321 | }
|
---|
322 |
|
---|
323 | # returns undef if file is rejected by the plugin
|
---|
324 | sub process {
|
---|
325 | my $self = shift (@_);
|
---|
326 | my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj) = @_;
|
---|
327 |
|
---|
328 | die "Basplug::process function must be implemented in sub-class\n";
|
---|
329 |
|
---|
330 | return undef; # never gets here
|
---|
331 | }
|
---|
332 |
|
---|
333 | # uses the multiread package to read in the entire file pointed to
|
---|
334 | # by filename and loads the resulting text into $$textref. Input text
|
---|
335 | # may be in any of the encodings handled by multiread, output text
|
---|
336 | # will be in utf8
|
---|
337 | sub read_file {
|
---|
338 | my $self = shift (@_);
|
---|
339 | my ($filename, $encoding, $language, $textref) = @_;
|
---|
340 |
|
---|
341 | if (!-r $filename)
|
---|
342 | {
|
---|
343 | my $outhandle = $self->{'outhandle'};
|
---|
344 | print $outhandle "Read permission denied for $filename\n" if $self->{'verbosity'};
|
---|
345 | return;
|
---|
346 | }
|
---|
347 |
|
---|
348 | $$textref = "";
|
---|
349 |
|
---|
350 | open (FILE, $filename) || die "BasPlug::read_file could not open $filename for reading ($!)\n";
|
---|
351 |
|
---|
352 | if ($encoding eq "ascii") {
|
---|
353 | undef $/;
|
---|
354 | $$textref = <FILE>;
|
---|
355 | $/ = "\n";
|
---|
356 | } else {
|
---|
357 | my $reader = new multiread();
|
---|
358 | $reader->set_handle ('BasPlug::FILE');
|
---|
359 | $reader->set_encoding ($encoding);
|
---|
360 | $reader->read_file ($textref);
|
---|
361 |
|
---|
362 | if ($language eq "zh") {
|
---|
363 | # segment the Chinese words
|
---|
364 | $$textref = &cnseg::segment($$textref);
|
---|
365 | }
|
---|
366 | }
|
---|
367 |
|
---|
368 | close FILE;
|
---|
369 | }
|
---|
370 |
|
---|
371 | sub textcat_get_language_encoding {
|
---|
372 | my $self = shift (@_);
|
---|
373 | my ($filename) = @_;
|
---|
374 |
|
---|
375 | my ($language, $encoding, $extracted_encoding);
|
---|
376 | if ($self->{'input_encoding'} eq "auto") {
|
---|
377 | # use textcat to automatically work out the input encoding and language
|
---|
378 | ($language, $encoding) = $self->get_language_encoding ($filename);
|
---|
379 | } elsif ($self->{'extract_language'}) {
|
---|
380 | # use textcat to get language metadata
|
---|
381 | ($language, $extracted_encoding) = $self->get_language_encoding ($filename);
|
---|
382 | $encoding = $self->{'input_encoding'};
|
---|
383 | if ($extracted_encoding ne $encoding && $self->{'verbosity'}) {
|
---|
384 | my $plugin_name = ref ($self);
|
---|
385 | my $outhandle = $self->{'outhandle'};
|
---|
386 | print $outhandle "$plugin_name: WARNING: $filename was read using $encoding encoding but ";
|
---|
387 | print $outhandle "appears to be encoded as $extracted_encoding.\n";
|
---|
388 | }
|
---|
389 | } else {
|
---|
390 | $language = $self->{'default_language'};
|
---|
391 | $encoding = $self->{'input_encoding'};
|
---|
392 | }
|
---|
393 | return ($language, $encoding);
|
---|
394 | }
|
---|
395 |
|
---|
396 | # Uses textcat to work out the encoding and language of the text in
|
---|
397 | # $filename. All html tags are removed before processing.
|
---|
398 | # returns an array containing "language" and "encoding"
|
---|
399 | sub get_language_encoding {
|
---|
400 | my $self = shift (@_);
|
---|
401 | my ($filename) = @_;
|
---|
402 | my $outhandle = $self->{'outhandle'};
|
---|
403 |
|
---|
404 | # read in file
|
---|
405 | open (FILE, $filename) || die "BasPlug::get_language_encoding could not open $filename for reading ($!)\n";
|
---|
406 | undef $/;
|
---|
407 | my $text = <FILE>;
|
---|
408 | $/ = "\n";
|
---|
409 | close FILE;
|
---|
410 |
|
---|
411 | # remove <title>stuff</title> -- as titles tend often to be in English
|
---|
412 | # for foreign language documents
|
---|
413 | $text =~ s/<title>.*?<\/title>//i;
|
---|
414 |
|
---|
415 | # remove all HTML tags
|
---|
416 | $text =~ s/<[^>]*>//sg;
|
---|
417 |
|
---|
418 | # get the language/encoding
|
---|
419 | my $results = $self->{'textcat'}->classify(\$text);
|
---|
420 |
|
---|
421 | # if textcat returns 3 or less possibilities we'll use the
|
---|
422 | # first one in the list - otherwise use the defaults
|
---|
423 | if (scalar @$results > 3) {
|
---|
424 |
|
---|
425 | if ($self->{'input_encoding'} ne 'auto') {
|
---|
426 | if ($self->{'extract_language'} && $self->{'verbosity'}) {
|
---|
427 | print $outhandle "BasPlug: WARNING: language could not be extracted from $filename - ";
|
---|
428 | print $outhandle "defaulting to $self->{'default_language'}\n";
|
---|
429 | }
|
---|
430 | return ($self->{'default_language'}, $self->{'input_encoding'});
|
---|
431 |
|
---|
432 | } else {
|
---|
433 | if ($self->{'verbosity'}) {
|
---|
434 | print $outhandle "BASPlug: WARNING: language/encoding could not be extracted from $filename - ";
|
---|
435 | print $outhandle "defaulting to $self->{'default_language'}/$self->{'default_encoding'}\n";
|
---|
436 | }
|
---|
437 | return ($self->{'default_language'}, $self->{'default_encoding'});
|
---|
438 | }
|
---|
439 | }
|
---|
440 |
|
---|
441 | # format language/encoding
|
---|
442 | my ($language, $encoding) = $results->[0] =~ /^([^-]*)(?:-(.*))?$/;
|
---|
443 | if (!defined $language) {
|
---|
444 | if ($self->{'verbosity'}) {
|
---|
445 | print $outhandle "BasPlug: WARNING: language could not be extracted from $filename - ";
|
---|
446 | print $outhandle "defaulting to $self->{'default_language'}\n";
|
---|
447 | }
|
---|
448 | $language = $self->{'default_language'};
|
---|
449 | }
|
---|
450 | if (!defined $encoding) {
|
---|
451 | if ($self->{'verbosity'}) {
|
---|
452 | print $outhandle "BasPlug: WARNING: encoding could not be extracted from $filename - ";
|
---|
453 | print $outhandle "defaulting to $self->{'default_encoding'}\n";
|
---|
454 | }
|
---|
455 | $encoding = $self->{'default_encoding'};
|
---|
456 | }
|
---|
457 |
|
---|
458 | if ($encoding !~ /^(ascii|utf8|unicode)$/ &&
|
---|
459 | !defined $encodings::encodings->{$encoding}) {
|
---|
460 | if ($self->{'verbosity'}) {
|
---|
461 | print $outhandle "BasPlug: WARNING: $filename appears to be encoded in an unsupported encoding ($encoding) - ";
|
---|
462 | print $outhandle "using $self->{'default_encoding'}\n";
|
---|
463 | }
|
---|
464 | $encoding = $self->{'default_encoding'};
|
---|
465 | }
|
---|
466 |
|
---|
467 | return ($language, $encoding);
|
---|
468 | }
|
---|
469 |
|
---|
470 | # add any extra metadata that's been passed around from one
|
---|
471 | # plugin to another.
|
---|
472 | # extra_metadata uses add_utf8_metadata so it expects metadata values
|
---|
473 | # to already be in utf8
|
---|
474 | sub extra_metadata {
|
---|
475 | my $self = shift (@_);
|
---|
476 | my ($doc_obj, $cursection, $metadata) = @_;
|
---|
477 |
|
---|
478 | foreach my $field (keys(%$metadata)) {
|
---|
479 | # $metadata->{$field} may be an array reference
|
---|
480 | if (ref ($metadata->{$field}) eq "ARRAY") {
|
---|
481 | map {
|
---|
482 | $doc_obj->add_utf8_metadata ($cursection, $field, $_);
|
---|
483 | } @{$metadata->{$field}};
|
---|
484 | } else {
|
---|
485 | $doc_obj->add_utf8_metadata ($cursection, $field, $metadata->{$field});
|
---|
486 | }
|
---|
487 | }
|
---|
488 | }
|
---|
489 |
|
---|
490 | # initialise metadata extractors
|
---|
491 | sub initialise_extractors {
|
---|
492 | my $self = shift (@_);
|
---|
493 |
|
---|
494 | if ($self->{'extract_acronyms'} || $self->{'markup_acronyms'}) {
|
---|
495 | &acronym::initialise_acronyms();
|
---|
496 | }
|
---|
497 | }
|
---|
498 |
|
---|
499 | # finalise metadata extractors
|
---|
500 | sub finalise_extractors {
|
---|
501 | my $self = shift (@_);
|
---|
502 |
|
---|
503 | if ($self->{'extract_acronyms'} || $self->{'markup_acronyms'}) {
|
---|
504 | &acronym::finalise_acronyms();
|
---|
505 | }
|
---|
506 | }
|
---|
507 |
|
---|
508 | # FIRSTNNN: extract the first NNN characters as metadata
|
---|
509 | sub extract_first_NNNN_characters {
|
---|
510 | my $self = shift (@_);
|
---|
511 | my ($textref, $doc_obj, $thissection) = @_;
|
---|
512 |
|
---|
513 | foreach my $size (split /,/, $self->{'first'}) {
|
---|
514 | my $tmptext = $$textref;
|
---|
515 | $tmptext =~ s/^\s+//;
|
---|
516 | $tmptext =~ s/\s+$//;
|
---|
517 | $tmptext =~ s/\s+/ /gs;
|
---|
518 | $tmptext = substr ($tmptext, 0, $size);
|
---|
519 | $tmptext =~ s/\s\S*$/…/;
|
---|
520 | $doc_obj->add_utf8_metadata ($thissection, "First$size", $tmptext);
|
---|
521 | }
|
---|
522 | }
|
---|
523 |
|
---|
524 | sub extract_email {
|
---|
525 | my $self = shift (@_);
|
---|
526 | my ($textref, $doc_obj, $thissection) = @_;
|
---|
527 | my $outhandle = $self->{'outhandle'};
|
---|
528 |
|
---|
529 | print $outhandle " extracting email addresses ...\n"
|
---|
530 | if ($self->{'verbosity'} > 2);
|
---|
531 |
|
---|
532 | my @email = ($$textref =~ m/([-a-z0-9\.@+_=]+@(?:[-a-z0-9]+\.)+(?:com|org|edu|mil|int|net|[a-z][a-z]))/g);
|
---|
533 | @email = sort @email;
|
---|
534 |
|
---|
535 | my @email2 = ();
|
---|
536 | foreach my $address (@email) {
|
---|
537 | if (!(join(" ",@email2) =~ m/$address/ )) {
|
---|
538 | push @email2, $address;
|
---|
539 | $doc_obj->add_utf8_metadata ($thissection, "emailAddress", $address);
|
---|
540 | print $outhandle " extracting $address\n"
|
---|
541 | if ($self->{'verbosity'} > 3);
|
---|
542 | }
|
---|
543 | }
|
---|
544 | print $outhandle " done extracting email addresses.\n"
|
---|
545 | if ($self->{'verbosity'} > 2);
|
---|
546 |
|
---|
547 | }
|
---|
548 |
|
---|
549 | # extract metadata
|
---|
550 | sub auto_extract_metadata {
|
---|
551 |
|
---|
552 |
|
---|
553 | my $self = shift (@_);
|
---|
554 | my ($doc_obj) = @_;
|
---|
555 |
|
---|
556 | if ($self->{'extract_email'}) {
|
---|
557 | my $thissection = $doc_obj->get_top_section();
|
---|
558 | while (defined $thissection) {
|
---|
559 | my $text = $doc_obj->get_text($thissection);
|
---|
560 | $self->extract_email (\$text, $doc_obj, $thissection) if $text =~ /./;
|
---|
561 | $thissection = $doc_obj->get_next_section ($thissection);
|
---|
562 | }
|
---|
563 | }
|
---|
564 |
|
---|
565 |
|
---|
566 | #adding kea keyphrases
|
---|
567 | if ($self->{'kea'}) {
|
---|
568 |
|
---|
569 | my $thissection = $doc_obj->get_top_section();
|
---|
570 | my $text = "";
|
---|
571 | my @list;
|
---|
572 |
|
---|
573 | while (defined $thissection) { #loop through sections to gather whole doc
|
---|
574 | my $sectiontext = $doc_obj->get_text($thissection);
|
---|
575 | $text = $text.$sectiontext;
|
---|
576 | $thissection = $doc_obj->get_next_section ($thissection);
|
---|
577 | }
|
---|
578 |
|
---|
579 | if($self->{'kea_options'}) { #if kea options flag is set, call Kea with specified options
|
---|
580 | @list = &Kea::extract_KeyPhrases ($text, $self->{'kea_options'});
|
---|
581 | } else { #otherwise call Kea with no options
|
---|
582 | @list = &Kea::extract_KeyPhrases ($text);
|
---|
583 | }
|
---|
584 |
|
---|
585 | if(@list){ #if a list of kea keyphrases was returned (ie not empty)
|
---|
586 | my $keyphrases = $list[0]; #first arg is keyphrase list
|
---|
587 | my $stems = $list[1]; #second arg is stemmed keyphrase list
|
---|
588 | print STDERR "keyphrases: $keyphrases\n";
|
---|
589 | print STDERR "stems: $stems\n";
|
---|
590 | $thissection = $doc_obj->get_top_section(); #add metadata to top section
|
---|
591 | $doc_obj->add_metadata($thissection, "kea", $keyphrases);
|
---|
592 | $doc_obj->add_metadata($thissection, "stems", $stems);
|
---|
593 | }
|
---|
594 | } #end of kea
|
---|
595 |
|
---|
596 | if ($self->{'first'}) {
|
---|
597 | my $thissection = $doc_obj->get_top_section();
|
---|
598 | while (defined $thissection) {
|
---|
599 | my $text = $doc_obj->get_text($thissection);
|
---|
600 | $self->extract_first_NNNN_characters (\$text, $doc_obj, $thissection) if $text =~ /./;
|
---|
601 | $thissection = $doc_obj->get_next_section ($thissection);
|
---|
602 | }
|
---|
603 | }
|
---|
604 |
|
---|
605 | if ($self->{'extract_acronyms'}) {
|
---|
606 | my $thissection = $doc_obj->get_top_section();
|
---|
607 | while (defined $thissection) {
|
---|
608 | my $text = $doc_obj->get_text($thissection);
|
---|
609 | $self->extract_acronyms (\$text, $doc_obj, $thissection) if $text =~ /./;
|
---|
610 | $thissection = $doc_obj->get_next_section ($thissection);
|
---|
611 | }
|
---|
612 | }
|
---|
613 |
|
---|
614 | if ($self->{'markup_acronyms'}) {
|
---|
615 | my $thissection = $doc_obj->get_top_section();
|
---|
616 | while (defined $thissection) {
|
---|
617 | my $text = $doc_obj->get_text($thissection);
|
---|
618 | $text = $self->markup_acronyms ($text, $doc_obj, $thissection);
|
---|
619 | $doc_obj->delete_text($thissection);
|
---|
620 | $doc_obj->add_text($thissection, $text);
|
---|
621 | $thissection = $doc_obj->get_next_section ($thissection);
|
---|
622 | }
|
---|
623 | }
|
---|
624 |
|
---|
625 | if($self->{'date_extract'}) {
|
---|
626 | my $thissection = $doc_obj->get_top_section();
|
---|
627 | while (defined $thissection) {
|
---|
628 |
|
---|
629 | my $text = $doc_obj->get_text($thissection);
|
---|
630 | &DateExtract::get_date_metadata($text, $doc_obj,
|
---|
631 | $thissection,
|
---|
632 | $self->{'no_biblio'},
|
---|
633 | $self->{'max_year'},
|
---|
634 | $self->{'max_century'});
|
---|
635 | $thissection = $doc_obj->get_next_section ($thissection);
|
---|
636 | }
|
---|
637 | }
|
---|
638 | }
|
---|
639 |
|
---|
640 | # extract acronyms from a section in a document. progress is
|
---|
641 | # reported to outhandle based on the verbosity. both the Acronym
|
---|
642 | # and the AcronymKWIC metadata items are created.
|
---|
643 |
|
---|
644 | sub extract_acronyms {
|
---|
645 | my $self = shift (@_);
|
---|
646 | my ($textref, $doc_obj, $thissection) = @_;
|
---|
647 | my $outhandle = $self->{'outhandle'};
|
---|
648 |
|
---|
649 | print $outhandle " extracting acronyms ...\n"
|
---|
650 | if ($self->{'verbosity'} > 2);
|
---|
651 |
|
---|
652 | my $acro_array = &acronym::acronyms($textref);
|
---|
653 |
|
---|
654 | foreach my $acro (@$acro_array) {
|
---|
655 |
|
---|
656 | #check that this is the first time ...
|
---|
657 | my $seen_before = "false";
|
---|
658 | my $previous_data = $doc_obj->get_metadata($thissection, "Acronym");
|
---|
659 | foreach my $thisAcro (@$previous_data) {
|
---|
660 | if ($thisAcro eq $acro->to_string()) {
|
---|
661 | $seen_before = "true";
|
---|
662 | print $outhandle " already seen ". $acro->to_string() . "\n"
|
---|
663 | if ($self->{'verbosity'} >= 4);
|
---|
664 | }
|
---|
665 | }
|
---|
666 |
|
---|
667 | if ($seen_before eq "false") {
|
---|
668 | #write it to the file ...
|
---|
669 | $acro->write_to_file();
|
---|
670 |
|
---|
671 | #do the normal acronym
|
---|
672 | $doc_obj->add_utf8_metadata($thissection, "Acronym", $acro->to_string());
|
---|
673 | print $outhandle " adding ". $acro->to_string() . "\n"
|
---|
674 | if ($self->{'verbosity'} > 3);
|
---|
675 |
|
---|
676 | }
|
---|
677 | }
|
---|
678 | print $outhandle " done extracting acronyms. \n"
|
---|
679 | if ($self->{'verbosity'} > 2);
|
---|
680 | }
|
---|
681 |
|
---|
682 | sub markup_acronyms {
|
---|
683 | my $self = shift (@_);
|
---|
684 | my ($text, $doc_obj, $thissection) = @_;
|
---|
685 | my $outhandle = $self->{'outhandle'};
|
---|
686 |
|
---|
687 | print $outhandle " marking up acronyms ...\n"
|
---|
688 | if ($self->{'verbosity'} > 2);
|
---|
689 |
|
---|
690 | #self is passed in to check for verbosity ...
|
---|
691 | $text = &acronym::markup_acronyms($text, $self);
|
---|
692 |
|
---|
693 | print $outhandle " done marking up acronyms. \n"
|
---|
694 | if ($self->{'verbosity'} > 2);
|
---|
695 |
|
---|
696 | return $text;
|
---|
697 | }
|
---|
698 |
|
---|
699 | sub compile_stats {
|
---|
700 | my $self = shift(@_);
|
---|
701 | my ($stats) = @_;
|
---|
702 |
|
---|
703 | $stats->{'num_processed'} += $self->{'num_processed'};
|
---|
704 | $stats->{'num_not_processed'} += $self->{'num_not_processed'};
|
---|
705 | $stats->{'num_archives'} += $self->{'num_archives'};
|
---|
706 |
|
---|
707 | }
|
---|
708 |
|
---|
709 | 1;
|
---|