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

Last change on this file since 33477 was 33477, checked in by kjdon, 5 years ago

need to call setup_custom_sort to allow for collection's customsorttools.pm

  • Property svn:keywords set to Author Date Id Revision
File size: 11.1 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' => "no_metadata_formatting",
74 'desc' => "{BasClas.no_metadata_formatting}",
75 'type' => "flag" },
76 { 'name' => "builddir",
77 'desc' => "{BasClas.builddir}",
78 'type' => "string",
79 'deft' => "" },
80 { 'name' => "outhandle",
81 'desc' => "{BasClas.outhandle}",
82 'type' => "string",
83 'deft' => "STDERR" },
84 { 'name' => "verbosity",
85 'desc' => "{BasClas.verbosity}",
86# 'type' => "enum",
87 'type' => "int",
88 'deft' => "2",
89 'reqd' => "no" }
90
91# { 'name' => "ignore_namespace",
92# 'desc' => "{BasClas.ignore_namespace}",
93# 'type' => "flag"}
94 ];
95
96my $options = { 'name' => "BaseClassifier",
97 'desc' => "{BasClas.desc}",
98 'abstract' => "yes",
99 'inherits' => "no",
100 'args' => $arguments };
101
102
103sub gsprintf
104{
105 return &gsprintf::gsprintf(@_);
106}
107
108
109sub print_xml_usage
110{
111 my $self = shift(@_);
112 my $header = shift(@_);
113 my $high_level_information_only = shift(@_);
114
115 # XML output is always in UTF-8
116 &gsprintf::output_strings_in_UTF8;
117
118 if ($header) {
119 &PrintUsage::print_xml_header("classify");
120 }
121 $self->print_xml($high_level_information_only);
122}
123
124
125sub print_xml
126{
127 my $self = shift(@_);
128 my $high_level_information_only = shift(@_);
129
130 my $optionlistref = $self->{'option_list'};
131 my @optionlist = @$optionlistref;
132 my $classifieroptions = shift(@$optionlistref);
133 return if (!defined($classifieroptions));
134
135 &gsprintf(STDERR, "<ClassInfo>\n");
136 &gsprintf(STDERR, " <Name>$classifieroptions->{'name'}</Name>\n");
137 my $desc = &gsprintf::lookup_string($classifieroptions->{'desc'});
138 $desc =~ s/</&amp;lt;/g; # doubly escaped
139 $desc =~ s/>/&amp;gt;/g;
140 &gsprintf(STDERR, " <Desc>$desc</Desc>\n");
141 &gsprintf(STDERR, " <Abstract>$classifieroptions->{'abstract'}</Abstract>\n");
142 &gsprintf(STDERR, " <Inherits>$classifieroptions->{'inherits'}</Inherits>\n");
143 unless (defined($high_level_information_only)) {
144 &gsprintf(STDERR, " <Arguments>\n");
145 if (defined($classifieroptions->{'args'})) {
146 &PrintUsage::print_options_xml($classifieroptions->{'args'});
147 }
148 &gsprintf(STDERR, " </Arguments>\n");
149
150 # Recurse up the classifier hierarchy
151 $self->print_xml();
152 }
153 &gsprintf(STDERR, "</ClassInfo>\n");
154}
155
156
157sub print_txt_usage
158{
159 my $self = shift(@_);
160
161 # Print the usage message for a classifier (recursively)
162 my $descoffset = $self->determine_description_offset(0);
163 $self->print_classifier_usage($descoffset, 1);
164}
165
166
167sub determine_description_offset
168{
169 my $self = shift(@_);
170 my $maxoffset = shift(@_);
171
172 my $optionlistref = $self->{'option_list'};
173 my @optionlist = @$optionlistref;
174 my $classifieroptions = pop(@$optionlistref);
175 return $maxoffset if (!defined($classifieroptions));
176
177 # Find the length of the longest option string of this classifier
178 my $classifierargs = $classifieroptions->{'args'};
179 if (defined($classifierargs)) {
180 my $longest = &PrintUsage::find_longest_option_string($classifierargs);
181 if ($longest > $maxoffset) {
182 $maxoffset = $longest;
183 }
184 }
185
186 # Recurse up the classifier hierarchy
187 $maxoffset = $self->determine_description_offset($maxoffset);
188 $self->{'option_list'} = \@optionlist;
189 return $maxoffset;
190}
191
192
193sub print_classifier_usage
194{
195 my $self = shift(@_);
196 my $descoffset = shift(@_);
197 my $isleafclass = shift(@_);
198
199 my $optionlistref = $self->{'option_list'};
200 my @optionlist = @$optionlistref;
201 my $classifieroptions = shift(@$optionlistref);
202 return if (!defined($classifieroptions));
203
204 my $classifiername = $classifieroptions->{'name'};
205 my $classifierargs = $classifieroptions->{'args'};
206 my $classifierdesc = $classifieroptions->{'desc'};
207 # Produce the usage information using the data structure above
208 if ($isleafclass) {
209 if (defined($classifierdesc)) {
210 &gsprintf(STDERR, "$classifierdesc\n\n");
211 }
212 &gsprintf(STDERR, " {common.usage}: classify $classifiername [{common.options}]\n\n");
213
214 }
215
216 # Display the classifier options, if there are some
217 if (defined($classifierargs)) {
218 # Calculate the column offset of the option descriptions
219 my $optiondescoffset = $descoffset + 2; # 2 spaces between options & descriptions
220
221 if ($isleafclass) {
222 &gsprintf(STDERR, " {common.specific_options}:\n");
223 }
224 else {
225 &gsprintf(STDERR, " {common.general_options}:\n", $classifiername);
226 }
227
228 # Display the classifier options
229 &PrintUsage::print_options_txt($classifierargs, $optiondescoffset);
230 }
231
232 # Recurse up the classifier hierarchy
233 $self->print_classifier_usage($descoffset, 0);
234 $self->{'option_list'} = \@optionlist;
235}
236
237
238sub new {
239 my ($class) = shift (@_);
240 my ($classifierslist,$args,$hashArgOptLists) = @_;
241 push(@$classifierslist, $class);
242 my $classifier_name = (defined $classifierslist->[0]) ? $classifierslist->[0] : $class;
243
244 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
245 push(@{$hashArgOptLists->{"OptList"}},$options);
246
247
248 # Manually set $self parameters.
249 my $self = {};
250 $self->{'outhandle'} = STDERR;
251 $self->{'idnum'} = -1;
252 $self->{'option_list'} = $hashArgOptLists->{"OptList"};
253 $self->{"info_only"} = 0;
254
255 # Check if gsdlinfo is in the argument list or not - if it is, don't parse
256 # the args, just return the object.
257 foreach my $strArg (@{$args})
258 {
259 if($strArg eq "-gsdlinfo")
260 {
261 $self->{"info_only"} = 1;
262 return bless $self, $class;
263 }
264 }
265
266 # general options available to all classifiers
267 if(parse2::parse($args,$hashArgOptLists->{"ArgList"},$self) == -1)
268 {
269 #print out the text usage of this classifier.
270 my $classTempClass = bless $self, $class;
271 print STDERR "<BadClassifier c=$classifier_name>\n";
272
273 &gsprintf(STDERR, "\n{BasClas.bad_general_option}\n", $classifier_name);
274 $classTempClass->print_txt_usage(""); # Use default resource bundle
275 die "\n";
276 }
277
278 delete $self->{"info_only"};
279
280 &sorttools::setup_custom_sort();
281# We now ensure that when text files (and even colcfg) are read in,
282# they are straightaway made to be Unicode aware strings in Perl
283
284 return bless $self, $class;
285}
286
287sub init {
288 my $self = shift (@_);
289
290 $self->{'supportsmemberof'} = &supports_memberof();
291}
292
293sub set_number {
294 my $self = shift (@_);
295 my ($id) = @_;
296 $self->{'idnum'} = $id;
297}
298
299sub get_number {
300 my $self = shift (@_);
301 return $self->{'idnum'};
302}
303
304sub oid_array_delete
305{
306 my $self = shift (@_);
307 my ($delete_oid,$field) = @_;
308
309 my $outhandle = $self->{'outhandle'};
310
311 my @filtered_list = ();
312 foreach my $existing_oid (@{$self->{$field}}) {
313 if ($existing_oid eq $delete_oid) {
314 print $outhandle " Deleting old $delete_oid for ", ref $self, "\n";
315 }
316 else {
317 push(@filtered_list,$existing_oid);
318 }
319 }
320 $self->{$field} = \@filtered_list;
321}
322
323sub oid_hash_delete
324{
325 my $self = shift (@_);
326 my ($delete_oid,$field) = @_;
327
328 my $outhandle = $self->{'outhandle'};
329
330 print $outhandle " Deleting old $delete_oid for ", ref $self, "\n";
331 delete $self->{$field}->{$delete_oid};
332}
333
334sub classify {
335 my $self = shift (@_);
336 my ($doc_obj) = @_;
337
338 my $outhandle = $self->{'outhandle'};
339 &gsprintf($outhandle, "BaseClassifier::classify {common.must_be_implemented}\n");
340}
341
342sub get_classify_info {
343 my $self = shift (@_);
344
345 my $outhandle = $self->{'outhandle'};
346 &gsprintf($outhandle, "BaseClassifier::get_classify_info {common.must_be_implemented}\n");
347}
348
349sub supports_memberof {
350 my $self = shift(@_);
351
352 return "false";
353}
354
355# previously, if a buttonname wasn't specified, we just use the metadata value,
356# but with a list of metadata, we want to do something a bit nicer so that
357# eg -metadata dc.Title,Title will end up with Title as the buttonname
358
359# current algorithm - use the first element, but strip its namespace
360sub generate_title_from_metadata {
361
362 my $self = shift (@_);
363 my $metadata = shift (@_);
364
365 return "" unless defined $metadata && $metadata =~ /\S/;
366
367 my @metalist = split(/,|;/, $metadata);
368 my $firstmeta = $metalist[0];
369 if ($firstmeta =~ /\./) {
370 $firstmeta =~ s/^\w+\.//;
371 }
372 return $firstmeta;
373}
374
375
376# ex. can be at front, or it may be a list of metadata, separated by ,/;
377sub strip_ex_from_metadata {
378 my $self = shift (@_);
379 my $metadata = shift (@_);
380
381 return $metadata unless defined $metadata && $metadata =~ /\S/;
382
383 # only remove ex. metadata prefix if there are no other prefixes after it
384 $metadata =~ s/(,|;|:|\/)/$1 /g; # insert a space separator so meta names like flex.Image don't become fl.Image
385 $metadata =~ s/(^| )ex\.([^.,;:\/]+)(,|;|:|\/|$)/$1$2$3/g;
386 $metadata =~ s/(,|;|:|\/) /$1/g;
387
388 return $metadata;
389}
390
391
3921;
Note: See TracBrowser for help on using the repository browser.