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