source: main/trunk/greenstone2/perllib/classify/BaseClassifier.pm@ 33901

Last change on this file since 33901 was 33901, checked in by kjdon, 4 years ago

new casefold_metadata_for_formatting and accentfold_metadata_for_formatting options

  • Property svn:keywords set to Author Date Id Revision
File size: 11.7 KB
Line 
1###########################################################################
2#
3# BaseClassifier.pm -- base class for all classifiers
4#
5# A component of the Greenstone digital library software
6# from the New Zealand Digital Library Project at the
7# University of Waikato, New Zealand.
8#
9# Copyright (C) 2000 New Zealand Digital Library Project
10#
11# This program is free software; you can redistribute it and/or modify
12# it under the terms of the GNU General Public License as published by
13# the Free Software Foundation; either version 2 of the License, or
14# (at your option) any later version.
15#
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19# GNU General Public License for more details.
20#
21# You should have received a copy of the GNU General Public License
22# along with this program; if not, write to the Free Software
23# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24#
25###########################################################################
26
27package BaseClassifier;
28
29# How a classifier works.
30#
31# For each classifier requested in the collect.cfg file, buildcol.pl creates
32# a new classifier object (a subclass of BaseClassifier). Later, it passes each
33# document object to each classifier in turn for classification.
34#
35# Four primary functions are used:
36#
37# 1. "new" is called before the documents are processed to set up the
38# classifier.
39#
40# 2. "init" is called after buildcol.pl has created the indexes etc but
41# before the documents are classified in order that the classifier might
42# set any variables it requires, etc.
43#
44# 3. "classify" is called once for each document object. The classifier
45# "classifies" each document and updates its local data accordingly.
46#
47# 4. "get_classify_info" is called after every document has been
48# classified. It collates the information about the documents and
49# stores a reference to the classifier so that Greenstone can later
50# display it.
51
52
53use gsprintf;
54use printusage;
55use parse2;
56use sorttools;
57
58# suppress the annoying "subroutine redefined" warning that various
59# classifiers cause under perl 5.6
60$SIG{__WARN__} = sub {warn($_[0]) unless ($_[0] =~ /Subroutine\s+\S+\sredefined/)};
61
62use strict;
63no strict 'subs'; # allow barewords (eg STDERR) as function arguments
64no strict 'refs'; # allow filehandles to be variables and viceversa
65
66my $arguments =
67 [
68 { 'name' => "buttonname",
69 'desc' => "{BasClas.buttonname}",
70 'type' => "string",
71 'deft' => "",
72 'reqd' => "no" },
73 { 'name' => "casefold_metadata_for_sorting",
74 'desc' => "{BasClas.casefold}",
75 'type' => "enum",
76 'list' => [{'name' => "true", 'desc' => "{common.true}"},
77 {'name' => "false", 'desc' => "{common.false}"}],
78 'deft' => "true",
79 'reqd' => "no" },
80 { 'name' => "accentfold_metadata_for_sorting",
81 'desc' => "{BasClas.accentfold}",
82 'type' => "enum",
83 'list' => [{'name' => "true", 'desc' => "{common.true}"},
84 {'name' => "false", 'desc' => "{common.false}"}],
85 'deft' => "false",
86 'reqd' => "no" },
87 { 'name' => "no_metadata_formatting",
88 'desc' => "{BasClas.no_metadata_formatting}",
89 'type' => "flag" },
90 { 'name' => "builddir",
91 'desc' => "{BasClas.builddir}",
92 'type' => "string",
93 'deft' => "" },
94 { 'name' => "outhandle",
95 'desc' => "{BasClas.outhandle}",
96 'type' => "string",
97 'deft' => "STDERR" },
98 { 'name' => "verbosity",
99 'desc' => "{BasClas.verbosity}",
100# 'type' => "enum",
101 'type' => "int",
102 'deft' => "2",
103 'reqd' => "no" }
104
105# { 'name' => "ignore_namespace",
106# 'desc' => "{BasClas.ignore_namespace}",
107# 'type' => "flag"}
108 ];
109
110my $options = { 'name' => "BaseClassifier",
111 'desc' => "{BasClas.desc}",
112 'abstract' => "yes",
113 'inherits' => "no",
114 'args' => $arguments };
115
116
117sub gsprintf
118{
119 return &gsprintf::gsprintf(@_);
120}
121
122
123sub print_xml_usage
124{
125 my $self = shift(@_);
126 my $header = shift(@_);
127 my $high_level_information_only = shift(@_);
128
129 # XML output is always in UTF-8
130 &gsprintf::output_strings_in_UTF8;
131
132 if ($header) {
133 &PrintUsage::print_xml_header("classify");
134 }
135 $self->print_xml($high_level_information_only);
136}
137
138
139sub print_xml
140{
141 my $self = shift(@_);
142 my $high_level_information_only = shift(@_);
143
144 my $optionlistref = $self->{'option_list'};
145 my @optionlist = @$optionlistref;
146 my $classifieroptions = shift(@$optionlistref);
147 return if (!defined($classifieroptions));
148
149 &gsprintf(STDERR, "<ClassInfo>\n");
150 &gsprintf(STDERR, " <Name>$classifieroptions->{'name'}</Name>\n");
151 my $desc = &gsprintf::lookup_string($classifieroptions->{'desc'});
152 $desc =~ s/</&amp;lt;/g; # doubly escaped
153 $desc =~ s/>/&amp;gt;/g;
154 &gsprintf(STDERR, " <Desc>$desc</Desc>\n");
155 &gsprintf(STDERR, " <Abstract>$classifieroptions->{'abstract'}</Abstract>\n");
156 &gsprintf(STDERR, " <Inherits>$classifieroptions->{'inherits'}</Inherits>\n");
157 unless (defined($high_level_information_only)) {
158 &gsprintf(STDERR, " <Arguments>\n");
159 if (defined($classifieroptions->{'args'})) {
160 &PrintUsage::print_options_xml($classifieroptions->{'args'});
161 }
162 &gsprintf(STDERR, " </Arguments>\n");
163
164 # Recurse up the classifier hierarchy
165 $self->print_xml();
166 }
167 &gsprintf(STDERR, "</ClassInfo>\n");
168}
169
170
171sub print_txt_usage
172{
173 my $self = shift(@_);
174
175 # Print the usage message for a classifier (recursively)
176 my $descoffset = $self->determine_description_offset(0);
177 $self->print_classifier_usage($descoffset, 1);
178}
179
180
181sub determine_description_offset
182{
183 my $self = shift(@_);
184 my $maxoffset = shift(@_);
185
186 my $optionlistref = $self->{'option_list'};
187 my @optionlist = @$optionlistref;
188 my $classifieroptions = pop(@$optionlistref);
189 return $maxoffset if (!defined($classifieroptions));
190
191 # Find the length of the longest option string of this classifier
192 my $classifierargs = $classifieroptions->{'args'};
193 if (defined($classifierargs)) {
194 my $longest = &PrintUsage::find_longest_option_string($classifierargs);
195 if ($longest > $maxoffset) {
196 $maxoffset = $longest;
197 }
198 }
199
200 # Recurse up the classifier hierarchy
201 $maxoffset = $self->determine_description_offset($maxoffset);
202 $self->{'option_list'} = \@optionlist;
203 return $maxoffset;
204}
205
206
207sub print_classifier_usage
208{
209 my $self = shift(@_);
210 my $descoffset = shift(@_);
211 my $isleafclass = shift(@_);
212
213 my $optionlistref = $self->{'option_list'};
214 my @optionlist = @$optionlistref;
215 my $classifieroptions = shift(@$optionlistref);
216 return if (!defined($classifieroptions));
217
218 my $classifiername = $classifieroptions->{'name'};
219 my $classifierargs = $classifieroptions->{'args'};
220 my $classifierdesc = $classifieroptions->{'desc'};
221 # Produce the usage information using the data structure above
222 if ($isleafclass) {
223 if (defined($classifierdesc)) {
224 &gsprintf(STDERR, "$classifierdesc\n\n");
225 }
226 &gsprintf(STDERR, " {common.usage}: classify $classifiername [{common.options}]\n\n");
227
228 }
229
230 # Display the classifier options, if there are some
231 if (defined($classifierargs)) {
232 # Calculate the column offset of the option descriptions
233 my $optiondescoffset = $descoffset + 2; # 2 spaces between options & descriptions
234
235 if ($isleafclass) {
236 &gsprintf(STDERR, " {common.specific_options}:\n");
237 }
238 else {
239 &gsprintf(STDERR, " {common.general_options}:\n", $classifiername);
240 }
241
242 # Display the classifier options
243 &PrintUsage::print_options_txt($classifierargs, $optiondescoffset);
244 }
245
246 # Recurse up the classifier hierarchy
247 $self->print_classifier_usage($descoffset, 0);
248 $self->{'option_list'} = \@optionlist;
249}
250
251
252sub new {
253 my ($class) = shift (@_);
254 my ($classifierslist,$args,$hashArgOptLists) = @_;
255 push(@$classifierslist, $class);
256 my $classifier_name = (defined $classifierslist->[0]) ? $classifierslist->[0] : $class;
257
258 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
259 push(@{$hashArgOptLists->{"OptList"}},$options);
260
261
262 # Manually set $self parameters.
263 my $self = {};
264 $self->{'outhandle'} = STDERR;
265 $self->{'idnum'} = -1;
266 $self->{'option_list'} = $hashArgOptLists->{"OptList"};
267 $self->{"info_only"} = 0;
268
269 # Check if gsdlinfo is in the argument list or not - if it is, don't parse
270 # the args, just return the object.
271 foreach my $strArg (@{$args})
272 {
273 if($strArg eq "-gsdlinfo")
274 {
275 $self->{"info_only"} = 1;
276 return bless $self, $class;
277 }
278 }
279
280 # general options available to all classifiers
281 if(parse2::parse($args,$hashArgOptLists->{"ArgList"},$self) == -1)
282 {
283 #print out the text usage of this classifier.
284 my $classTempClass = bless $self, $class;
285 print STDERR "<BadClassifier c=$classifier_name>\n";
286
287 &gsprintf(STDERR, "\n{BasClas.bad_general_option}\n", $classifier_name);
288 $classTempClass->print_txt_usage(""); # Use default resource bundle
289 die "\n";
290 }
291
292 delete $self->{"info_only"};
293
294 &sorttools::setup_custom_sort();
295# We now ensure that when text files (and even colcfg) are read in,
296# they are straightaway made to be Unicode aware strings in Perl
297
298 return bless $self, $class;
299}
300
301sub init {
302 my $self = shift (@_);
303
304 $self->{'supportsmemberof'} = &supports_memberof();
305}
306
307sub set_number {
308 my $self = shift (@_);
309 my ($id) = @_;
310 $self->{'idnum'} = $id;
311}
312
313sub get_number {
314 my $self = shift (@_);
315 return $self->{'idnum'};
316}
317
318sub oid_array_delete
319{
320 my $self = shift (@_);
321 my ($delete_oid,$field) = @_;
322
323 my $outhandle = $self->{'outhandle'};
324
325 my @filtered_list = ();
326 foreach my $existing_oid (@{$self->{$field}}) {
327 if ($existing_oid eq $delete_oid) {
328 print $outhandle " Deleting old $delete_oid for ", ref $self, "\n";
329 }
330 else {
331 push(@filtered_list,$existing_oid);
332 }
333 }
334 $self->{$field} = \@filtered_list;
335}
336
337sub oid_hash_delete
338{
339 my $self = shift (@_);
340 my ($delete_oid,$field) = @_;
341
342 my $outhandle = $self->{'outhandle'};
343
344 print $outhandle " Deleting old $delete_oid for ", ref $self, "\n";
345 delete $self->{$field}->{$delete_oid};
346}
347
348sub classify {
349 my $self = shift (@_);
350 my ($doc_obj) = @_;
351
352 my $outhandle = $self->{'outhandle'};
353 &gsprintf($outhandle, "BaseClassifier::classify {common.must_be_implemented}\n");
354}
355
356sub get_classify_info {
357 my $self = shift (@_);
358
359 my $outhandle = $self->{'outhandle'};
360 &gsprintf($outhandle, "BaseClassifier::get_classify_info {common.must_be_implemented}\n");
361}
362
363sub supports_memberof {
364 my $self = shift(@_);
365
366 return "false";
367}
368
369# previously, if a buttonname wasn't specified, we just use the metadata value,
370# but with a list of metadata, we want to do something a bit nicer so that
371# eg -metadata dc.Title,Title will end up with Title as the buttonname
372
373# current algorithm - use the first element, but strip its namespace
374sub generate_title_from_metadata {
375
376 my $self = shift (@_);
377 my $metadata = shift (@_);
378
379 return "" unless defined $metadata && $metadata =~ /\S/;
380
381 my @metalist = split(/,|;/, $metadata);
382 my $firstmeta = $metalist[0];
383 if ($firstmeta =~ /\./) {
384 $firstmeta =~ s/^\w+\.//;
385 }
386 return $firstmeta;
387}
388
389
390# ex. can be at front, or it may be a list of metadata, separated by ,/;
391sub strip_ex_from_metadata {
392 my $self = shift (@_);
393 my $metadata = shift (@_);
394
395 return $metadata unless defined $metadata && $metadata =~ /\S/;
396
397 # only remove ex. metadata prefix if there are no other prefixes after it
398 $metadata =~ s/(,|;|:|\/)/$1 /g; # insert a space separator so meta names like flex.Image don't become fl.Image
399 $metadata =~ s/(^| )ex\.([^.,;:\/]+)(,|;|:|\/|$)/$1$2$3/g;
400 $metadata =~ s/(,|;|:|\/) /$1/g;
401
402 return $metadata;
403}
404
405
4061;
Note: See TracBrowser for help on using the repository browser.