[537] | 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 | ###########################################################################
|
---|
[4] | 25 |
|
---|
| 26 | package BasPlug;
|
---|
[1954] | 27 | use Kea;
|
---|
[1219] | 28 | use parsargv;
|
---|
| 29 | use multiread;
|
---|
[1870] | 30 | use encodings;
|
---|
[1219] | 31 | use cnseg;
|
---|
[1242] | 32 | use acronym;
|
---|
[1317] | 33 | use textcat;
|
---|
[1242] | 34 | use doc;
|
---|
[1360] | 35 | use diagnostics;
|
---|
[1411] | 36 | use DateExtract;
|
---|
[4] | 37 |
|
---|
[1244] | 38 | sub print_general_usage {
|
---|
[1242] | 39 | my ($plugin_name) = @_;
|
---|
| 40 |
|
---|
| 41 | print STDERR "\n usage: plugin $plugin_name [options]\n\n";
|
---|
[1844] | 42 |
|
---|
[1242] | 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";
|
---|
[1844] | 47 | print STDERR " .htm or .html (case-insensitive).\n\n";
|
---|
| 48 |
|
---|
[1242] | 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";
|
---|
[1686] | 54 | print STDERR " any files with .gif, .jpg, .jpeg, .png, .rtf or .css\n";
|
---|
[1844] | 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 |
|
---|
[1870] | 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 | }
|
---|
[1844] | 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 |
|
---|
[1868] | 97 | print STDERR " -extract_acronyms Extract acronyms from within text and set as metadata\n";
|
---|
[1844] | 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 |
|
---|
[1602] | 104 | print STDERR " -extract_email Extract email addresses as metadata\n\n";
|
---|
[1844] | 105 |
|
---|
[1868] | 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";
|
---|
[1219] | 112 | }
|
---|
| 113 |
|
---|
[1244] | 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 |
|
---|
[4] | 120 | sub new {
|
---|
[1219] | 121 | my $class = shift (@_);
|
---|
[1242] | 122 | my $plugin_name = shift (@_);
|
---|
[1219] | 123 | my $self = {};
|
---|
[1844] | 124 |
|
---|
| 125 | my $enc = "^(";
|
---|
[1874] | 126 | map {$enc .= "$_|";} keys %$encodings::encodings;
|
---|
[1870] | 127 | my $denc = $enc . "ascii|utf8|unicode)\$";
|
---|
| 128 | $enc .= "ascii|utf8|unicode|auto)\$";
|
---|
[1844] | 129 |
|
---|
[1424] | 130 | $self->{'outhandle'} = STDERR;
|
---|
[1411] | 131 | my $year = (localtime)[5]+1900;
|
---|
[1954] | 132 |
|
---|
| 133 |
|
---|
[1219] | 134 | # general options available to all plugins
|
---|
[1242] | 135 | if (!parsargv::parse(\@_,
|
---|
| 136 | q^process_exp/.*/^, \$self->{'process_exp'},
|
---|
| 137 | q^block_exp/.*/^, \$self->{'block_exp'},
|
---|
[1954] | 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
|
---|
[1844] | 141 | qq^input_encoding/$enc/auto^, \$self->{'input_encoding'},
|
---|
| 142 | qq^default_encoding/$denc/iso_8859_1^, \$self->{'default_encoding'},
|
---|
[1602] | 143 | q^extract_email^, \$self->{'extract_email'},
|
---|
[1393] | 144 | q^markup_acronyms^, \$self->{'markup_acronyms'},
|
---|
[1844] | 145 | q^default_language/.{2}/en^, \$self->{'default_language'},
|
---|
[1605] | 146 | q^first/.*/^, \$self->{'first'},
|
---|
[1718] | 147 | q^extract_date^, \$self->{'date_extract'},
|
---|
[1844] | 148 | qq^maximum_date/\\d{4}/$year^, \$self->{'max_year'},
|
---|
[1411] | 149 | q^no_bibliography^, \$self->{'no_biblio'},
|
---|
[1844] | 150 | qq^maximum_century/-?\\d{1,2}( ?B\\.C\\.E\\.)?/-1^, \$self->{'max_century'},
|
---|
[1219] | 151 | "allow_extra_options")) {
|
---|
[1411] | 152 |
|
---|
[1244] | 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);
|
---|
[1219] | 156 | die "\n";
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | return bless $self, $class;
|
---|
[4] | 160 | }
|
---|
| 161 |
|
---|
[1242] | 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 (@_);
|
---|
[1424] | 166 | my ($verbosity, $outhandle) = @_;
|
---|
[1242] | 167 |
|
---|
| 168 | # verbosity is passed through from the processor
|
---|
| 169 | $self->{'verbosity'} = $verbosity;
|
---|
| 170 |
|
---|
[1424] | 171 | # as is the outhandle ...
|
---|
| 172 | $self->{'outhandle'} = $outhandle if defined $outhandle;
|
---|
| 173 |
|
---|
[1242] | 174 | # set process_exp and block_exp to defaults unless they were
|
---|
| 175 | # explicitly set
|
---|
[1244] | 176 |
|
---|
| 177 | if ((!$self->is_recursive()) and
|
---|
[1242] | 178 | (!defined $self->{'process_exp'}) || ($self->{'process_exp'} eq "")) {
|
---|
[1244] | 179 |
|
---|
[1242] | 180 | $self->{'process_exp'} = $self->get_default_process_exp ();
|
---|
| 181 | if ($self->{'process_exp'} eq "") {
|
---|
[1244] | 182 | warn ref($self) . " Warning: Non-recursive plugin has no process_exp\n";
|
---|
[1242] | 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 |
|
---|
[839] | 191 | sub begin {
|
---|
| 192 | my $self = shift (@_);
|
---|
| 193 | my ($pluginfo, $base_dir, $processor, $maxdocs) = @_;
|
---|
[1396] | 194 | $self->initialise_extractors();
|
---|
[839] | 195 | }
|
---|
| 196 |
|
---|
| 197 | sub end {
|
---|
| 198 | my ($self) = @_;
|
---|
[1396] | 199 | $self->finalise_extractors();
|
---|
[839] | 200 | }
|
---|
| 201 |
|
---|
[1242] | 202 | # this function should be overridden to return 1
|
---|
| 203 | # in recursive plugins
|
---|
[4] | 204 | sub is_recursive {
|
---|
| 205 | my $self = shift (@_);
|
---|
| 206 |
|
---|
[1242] | 207 | return 0;
|
---|
[4] | 208 | }
|
---|
| 209 |
|
---|
[1242] | 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 | #
|
---|
[1244] | 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 | #
|
---|
[1242] | 232 | # Return number of files processed, undef if can't process
|
---|
[4] | 233 | # Note that $base_dir might be "" and that $file might
|
---|
| 234 | # include directories
|
---|
[1242] | 235 |
|
---|
[4] | 236 | sub read {
|
---|
[1954] | 237 | my $self = shift (@_);
|
---|
| 238 |
|
---|
[317] | 239 | my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs) = @_;
|
---|
[4] | 240 |
|
---|
[1242] | 241 | if ($self->is_recursive()) {
|
---|
| 242 | die "BasPlug::read function must be implemented in sub-class for recursive plugins\n";
|
---|
| 243 | }
|
---|
[4] | 244 |
|
---|
[1844] | 245 | my $outhandle = $self->{'outhandle'};
|
---|
| 246 |
|
---|
[1242] | 247 | my $filename = &util::filename_cat($base_dir, $file);
|
---|
[1244] | 248 | return 0 if $self->{'block_exp'} ne "" && $filename =~ /$self->{'block_exp'}/;
|
---|
[1242] | 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
|
---|
[1844] | 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 |
|
---|
[1845] | 265 | if ($extracted_encoding ne $encoding && $self->{'verbosity'}) {
|
---|
[1844] | 266 | print $outhandle "$plugin_name: WARNING: $file was read using $encoding encoding but ";
|
---|
[1855] | 267 | print $outhandle "appears to be encoded as $extracted_encoding.\n";
|
---|
[1844] | 268 | }
|
---|
| 269 |
|
---|
| 270 | } else {
|
---|
| 271 | $language = $self->{'default_language'};
|
---|
| 272 | $encoding = $self->{'input_encoding'};
|
---|
| 273 | }
|
---|
| 274 |
|
---|
[1242] | 275 | # create a new document
|
---|
[1379] | 276 | my $doc_obj = new doc ($filename, "indexed_doc");
|
---|
[1844] | 277 | $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Language", $language);
|
---|
[1868] | 278 | $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Encoding", $encoding);
|
---|
[1242] | 279 |
|
---|
| 280 | # read in file ($text will be in utf8)
|
---|
| 281 | my $text = "";
|
---|
[1844] | 282 | $self->read_file ($filename, $encoding, \$text);
|
---|
[1242] | 283 |
|
---|
[1844] | 284 | if (!length ($text)) {
|
---|
[1424] | 285 | print $outhandle "$plugin_name: ERROR: $file contains no text\n" if $self->{'verbosity'};
|
---|
[1242] | 286 | return 0;
|
---|
| 287 | }
|
---|
[1954] | 288 |
|
---|
[1242] | 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
|
---|
[1244] | 294 | return undef unless defined ($self->process (\$text, $pluginfo, $base_dir, $file, $metadata, $doc_obj));
|
---|
[1954] | 295 |
|
---|
[1242] | 296 | # do any automatic metadata extraction
|
---|
| 297 | $self->auto_extract_metadata ($doc_obj);
|
---|
[1954] | 298 |
|
---|
[1242] | 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
|
---|
[4] | 306 | }
|
---|
| 307 |
|
---|
[1244] | 308 | # returns undef if file is rejected by the plugin
|
---|
[1242] | 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";
|
---|
[1244] | 314 |
|
---|
| 315 | return undef; # never gets here
|
---|
[1242] | 316 | }
|
---|
| 317 |
|
---|
[1219] | 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 (@_);
|
---|
[1844] | 324 | my ($filename, $encoding, $textref) = @_;
|
---|
[4] | 325 |
|
---|
[1756] | 326 | if (!-r $filename)
|
---|
| 327 | {
|
---|
[1844] | 328 | my $outhandle = $self->{'outhandle'};
|
---|
| 329 | print $outhandle "Read permission denied for $filename\n" if $self->{'verbosity'};
|
---|
[1756] | 330 | return;
|
---|
| 331 | }
|
---|
| 332 |
|
---|
[1219] | 333 | $$textref = "";
|
---|
| 334 |
|
---|
| 335 | open (FILE, $filename) || die "BasPlug::read_file could not open $filename for reading ($!)\n";
|
---|
| 336 |
|
---|
[1844] | 337 | if ($encoding eq "ascii") {
|
---|
[1219] | 338 | undef $/;
|
---|
| 339 | $$textref = <FILE>;
|
---|
| 340 | $/ = "\n";
|
---|
| 341 | } else {
|
---|
| 342 | my $reader = new multiread();
|
---|
| 343 | $reader->set_handle ('BasPlug::FILE');
|
---|
[1844] | 344 | $reader->set_encoding ($encoding);
|
---|
[1219] | 345 | $reader->read_file ($textref);
|
---|
| 346 |
|
---|
[1844] | 347 | if ($encoding eq "gb") {
|
---|
[1219] | 348 | # segment the Chinese words
|
---|
| 349 | $$textref = &cnseg::segment($$textref);
|
---|
| 350 | }
|
---|
| 351 | }
|
---|
| 352 |
|
---|
| 353 | close FILE;
|
---|
| 354 | }
|
---|
| 355 |
|
---|
[1844] | 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 |
|
---|
[1903] | 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) {
|
---|
[1868] | 380 |
|
---|
[1844] | 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] =~ /^([^-]*)(?:-(.*))?$/;
|
---|
[1870] | 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 | }
|
---|
[1844] | 406 | if (!defined $encoding) {
|
---|
[1870] | 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'};
|
---|
[1844] | 412 | }
|
---|
| 413 |
|
---|
[1870] | 414 | if ($encoding !~ /^(ascii|utf8|unicode)$/ &&
|
---|
| 415 | !defined $encodings::encodings->{$encoding}) {
|
---|
[1844] | 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 |
|
---|
[1219] | 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)) {
|
---|
[839] | 435 | # $metadata->{$field} may be an array reference
|
---|
| 436 | if (ref ($metadata->{$field}) eq "ARRAY") {
|
---|
| 437 | map {
|
---|
[1219] | 438 | $doc_obj->add_utf8_metadata ($cursection, $field, $_);
|
---|
[839] | 439 | } @{$metadata->{$field}};
|
---|
| 440 | } else {
|
---|
[1219] | 441 | $doc_obj->add_utf8_metadata ($cursection, $field, $metadata->{$field});
|
---|
[839] | 442 | }
|
---|
| 443 | }
|
---|
| 444 | }
|
---|
| 445 |
|
---|
[1396] | 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 |
|
---|
[1602] | 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"
|
---|
[1844] | 486 | if ($self->{'verbosity'} > 2);
|
---|
[1602] | 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"
|
---|
[1844] | 497 | if ($self->{'verbosity'} > 3);
|
---|
[1602] | 498 | }
|
---|
| 499 | }
|
---|
| 500 | print $outhandle " done extracting email addresses.\n"
|
---|
[1844] | 501 | if ($self->{'verbosity'} > 2);
|
---|
[1602] | 502 |
|
---|
| 503 | }
|
---|
| 504 |
|
---|
| 505 | # extract metadata
|
---|
[1954] | 506 | sub auto_extract_metadata {
|
---|
| 507 |
|
---|
| 508 |
|
---|
[1242] | 509 | my $self = shift (@_);
|
---|
| 510 | my ($doc_obj) = @_;
|
---|
[1602] | 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 | }
|
---|
[1954] | 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 |
|
---|
[1602] | 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 |
|
---|
[1242] | 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 | }
|
---|
[1602] | 569 |
|
---|
[1393] | 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 |
|
---|
[1846] | 581 | if($self->{'date_extract'}) {
|
---|
[1317] | 582 | my $thissection = $doc_obj->get_top_section();
|
---|
| 583 | while (defined $thissection) {
|
---|
[1846] | 584 |
|
---|
[1317] | 585 | my $text = $doc_obj->get_text($thissection);
|
---|
[1846] | 586 | &DateExtract::get_date_metadata($text, $doc_obj,
|
---|
| 587 | $thissection,
|
---|
| 588 | $self->{'no_biblio'},
|
---|
| 589 | $self->{'max_year'},
|
---|
| 590 | $self->{'max_century'});
|
---|
[1317] | 591 | $thissection = $doc_obj->get_next_section ($thissection);
|
---|
| 592 | }
|
---|
| 593 | }
|
---|
[1242] | 594 | }
|
---|
| 595 |
|
---|
[1335] | 596 | # extract acronyms from a section in a document. progress is
|
---|
[1424] | 597 | # reported to outhandle based on the verbosity. both the Acronym
|
---|
[1335] | 598 | # and the AcronymKWIC metadata items are created.
|
---|
| 599 |
|
---|
[1242] | 600 | sub extract_acronyms {
|
---|
| 601 | my $self = shift (@_);
|
---|
| 602 | my ($textref, $doc_obj, $thissection) = @_;
|
---|
[1424] | 603 | my $outhandle = $self->{'outhandle'};
|
---|
[1242] | 604 |
|
---|
[1424] | 605 | print $outhandle " extracting acronyms ...\n"
|
---|
[1844] | 606 | if ($self->{'verbosity'} > 2);
|
---|
[1335] | 607 |
|
---|
[1242] | 608 | my $acro_array = &acronym::acronyms($textref);
|
---|
[1360] | 609 |
|
---|
[1242] | 610 | foreach my $acro (@$acro_array) {
|
---|
| 611 |
|
---|
[1335] | 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) {
|
---|
[1602] | 616 | if ($thisAcro eq $acro->to_string()) {
|
---|
[1335] | 617 | $seen_before = "true";
|
---|
[1424] | 618 | print $outhandle " already seen ". $acro->to_string() . "\n"
|
---|
[1602] | 619 | if ($self->{'verbosity'} >= 4);
|
---|
[1335] | 620 | }
|
---|
[1242] | 621 | }
|
---|
[1335] | 622 |
|
---|
[1602] | 623 | if ($seen_before eq "false") {
|
---|
[1393] | 624 | #write it to the file ...
|
---|
| 625 | $acro->write_to_file();
|
---|
| 626 |
|
---|
[1335] | 627 | #do the normal acronym
|
---|
| 628 | $doc_obj->add_utf8_metadata($thissection, "Acronym", $acro->to_string());
|
---|
[1602] | 629 | print $outhandle " adding ". $acro->to_string() . "\n"
|
---|
[1844] | 630 | if ($self->{'verbosity'} > 3);
|
---|
[1335] | 631 |
|
---|
| 632 | }
|
---|
[1242] | 633 | }
|
---|
[1424] | 634 | print $outhandle " done extracting acronyms. \n"
|
---|
[1844] | 635 | if ($self->{'verbosity'} > 2);
|
---|
[1242] | 636 | }
|
---|
| 637 |
|
---|
[1393] | 638 | sub markup_acronyms {
|
---|
| 639 | my $self = shift (@_);
|
---|
| 640 | my ($text, $doc_obj, $thissection) = @_;
|
---|
[1424] | 641 | my $outhandle = $self->{'outhandle'};
|
---|
[1393] | 642 |
|
---|
[1424] | 643 | print $outhandle " marking up acronyms ...\n"
|
---|
[1844] | 644 | if ($self->{'verbosity'} > 2);
|
---|
[1393] | 645 |
|
---|
| 646 | #self is passed in to check for verbosity ...
|
---|
| 647 | $text = &acronym::markup_acronyms($text, $self);
|
---|
| 648 |
|
---|
[1424] | 649 | print $outhandle " done marking up acronyms. \n"
|
---|
[1844] | 650 | if ($self->{'verbosity'} > 2);
|
---|
[1393] | 651 |
|
---|
| 652 | return $text;
|
---|
| 653 | }
|
---|
| 654 |
|
---|
[4] | 655 | 1;
|
---|