source: trunk/gsdl/perllib/classify/BasClas.pm@ 11293

Last change on this file since 11293 was 10630, checked in by kjdon, 19 years ago

added -no_metadata_formatting option to avoid the format_metadata_for_sorting call

  • Property svn:keywords set to Author Date Id Revision
File size: 9.5 KB
Line 
1###########################################################################
2#
3# BasClas.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 BasClas;
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 BasClas). 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# 09/05/02 Added usage datastructure - John Thompson
53# 28/11/03 Commented out verbosity argument - John Thompson
54
55use gsprintf;
56use printusage;
57use parse2;
58
59# suppress the annoying "subroutine redefined" warning that various
60# classifiers cause under perl 5.6
61$SIG{__WARN__} = sub {warn($_[0]) unless ($_[0] =~ /Subroutine\s+\S+\sredefined/)};
62
63use strict;
64no strict 'subs'; # allow barewords (eg STDERR) as function arguments
65no strict 'refs'; # allow filehandles to be variables and viceversa
66
67my $arguments =
68 [
69 { 'name' => "no_metadata_formatting",
70 'desc' => "{BasClas.no_metadata_formatting}",
71 'type' => "flag" },
72 { 'name' => "builddir",
73 'desc' => "{BasClas.builddir}",
74 'type' => "string",
75 'deft' => "" },
76 { 'name' => "outhandle",
77 'desc' => "{BasClas.outhandle}",
78 'type' => "string",
79 'deft' => "STDERR" },
80 { 'name' => "verbosity",
81 'desc' => "{BasClas.verbosity}",
82# 'type' => "enum",
83 'type' => "int",
84 'deft' => "2",
85 'reqd' => "no" }
86
87# { 'name' => "ignore_namespace",
88# 'desc' => "{BasClas.ignore_namespace}",
89# 'type' => "flag"}
90 ];
91
92my $options = { 'name' => "BasClas",
93 'desc' => "{BasClas.desc}",
94 'abstract' => "yes",
95 'inherits' => "no",
96 'args' => $arguments };
97
98
99sub gsprintf
100{
101 return &gsprintf::gsprintf(@_);
102}
103
104
105sub print_xml_usage
106{
107 my $self = shift(@_);
108
109 # XML output is always in UTF-8
110 &gsprintf::output_strings_in_UTF8;
111
112 &PrintUsage::print_xml_header();
113 $self->print_xml();
114}
115
116
117sub print_xml
118{
119 my $self = shift(@_);
120
121 my $optionlistref = $self->{'option_list'};
122 my @optionlist = @$optionlistref;
123 my $classifieroptions = shift(@$optionlistref);
124 return if (!defined($classifieroptions));
125
126 &gsprintf(STDERR, "<ClassInfo>\n");
127 &gsprintf(STDERR, " <Name>$classifieroptions->{'name'}</Name>\n");
128 my $desc = &gsprintf::lookup_string($classifieroptions->{'desc'});
129 $desc =~ s/</&amp;lt;/g; # doubly escaped
130 $desc =~ s/>/&amp;gt;/g;
131 &gsprintf(STDERR, " <Desc>$desc</Desc>\n");
132 &gsprintf(STDERR, " <Abstract>$classifieroptions->{'abstract'}</Abstract>\n");
133 &gsprintf(STDERR, " <Inherits>$classifieroptions->{'inherits'}</Inherits>\n");
134 &gsprintf(STDERR, " <Arguments>\n");
135 if (defined($classifieroptions->{'args'})) {
136 &PrintUsage::print_options_xml($classifieroptions->{'args'});
137 }
138
139 # Recurse up the classifier hierarchy
140 $self->print_xml();
141
142 &gsprintf(STDERR, " </Arguments>\n");
143 &gsprintf(STDERR, "</ClassInfo>\n");
144}
145
146
147sub print_txt_usage
148{
149 my $self = shift(@_);
150
151 # Print the usage message for a classifier (recursively)
152 my $descoffset = $self->determine_description_offset(0);
153 $self->print_classifier_usage($descoffset, 1);
154}
155
156
157sub determine_description_offset
158{
159 my $self = shift(@_);
160 my $maxoffset = shift(@_);
161
162 my $optionlistref = $self->{'option_list'};
163 my @optionlist = @$optionlistref;
164 my $classifieroptions = pop(@$optionlistref);
165 return $maxoffset if (!defined($classifieroptions));
166
167 # Find the length of the longest option string of this classifier
168 my $classifierargs = $classifieroptions->{'args'};
169 if (defined($classifierargs)) {
170 my $longest = &PrintUsage::find_longest_option_string($classifierargs);
171 if ($longest > $maxoffset) {
172 $maxoffset = $longest;
173 }
174 }
175
176 # Recurse up the classifier hierarchy
177 $maxoffset = $self->determine_description_offset($maxoffset);
178 $self->{'option_list'} = \@optionlist;
179 return $maxoffset;
180}
181
182
183sub print_classifier_usage
184{
185 my $self = shift(@_);
186 my $descoffset = shift(@_);
187 my $isleafclass = shift(@_);
188
189 my $optionlistref = $self->{'option_list'};
190 my @optionlist = @$optionlistref;
191 my $classifieroptions = shift(@$optionlistref);
192 return if (!defined($classifieroptions));
193
194 my $classifiername = $classifieroptions->{'name'};
195 my $classifierargs = $classifieroptions->{'args'};
196 my $classifierdesc = $classifieroptions->{'desc'};
197 # Produce the usage information using the data structure above
198 if ($isleafclass) {
199 if (defined($classifierdesc)) {
200 &gsprintf(STDERR, "$classifierdesc\n\n");
201 }
202 &gsprintf(STDERR, " {common.usage}: classify $classifiername [{common.options}]\n\n");
203
204 }
205
206 # Display the classifier options, if there are some
207 if (defined($classifierargs)) {
208 # Calculate the column offset of the option descriptions
209 my $optiondescoffset = $descoffset + 2; # 2 spaces between options & descriptions
210
211 if ($isleafclass) {
212 &gsprintf(STDERR, " {common.specific_options}:\n");
213 }
214 else {
215 &gsprintf(STDERR, " {common.general_options}:\n", $classifiername);
216 }
217
218 # Display the classifier options
219 &PrintUsage::print_options_txt($classifierargs, $optiondescoffset);
220 }
221
222 # Recurse up the classifier hierarchy
223 $self->print_classifier_usage($descoffset, 0);
224 $self->{'option_list'} = \@optionlist;
225}
226
227
228sub new {
229 my ($class) = shift (@_);
230 my ($classifierslist,$args,$hashArgOptLists) = @_;
231 push(@$classifierslist, $class);
232 my $classifier_name = (defined $classifierslist->[0]) ? $classifierslist->[0] : $class;
233
234 if(defined $arguments){ push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});}
235 if(defined $options) { push(@{$hashArgOptLists->{"OptList"}},$options)};
236
237
238 # Manually set $self parameters.
239 my $self = {};
240 $self->{'outhandle'} = STDERR;
241 $self->{'idnum'} = -1;
242 $self->{'option_list'} = $hashArgOptLists->{"OptList"};
243 $self->{"info_only"} = 0;
244
245 # Check if gsdlinfo is in the argument list or not - if it is, don't parse
246 # the args, just return the object.
247 foreach my $strArg (@{$args})
248 {
249 if($strArg eq "-gsdlinfo")
250 {
251 $self->{"info_only"} = 1;
252 return bless $self, $class;
253 }
254 }
255
256 # general options available to all classifiers
257 if(!parse2::parse($args,$hashArgOptLists->{"ArgList"},$self))
258 {
259 #print out the text usage of this classifier.
260 my $classTempClass = bless $self, $class;
261 print STDERR "<BadClassifier c=$classifier_name>\n";
262
263 &gsprintf(STDERR, "\n{BasClas.bad_general_option}\n", $classifier_name);
264 $classTempClass->print_txt_usage(""); # Use default resource bundle
265 die "\n";
266 }
267
268 delete $self->{"info_only"};
269 return bless $self, $class;
270}
271
272sub init {
273 my $self = shift (@_);
274
275 $self->{'supportsmemberof'} = &supports_memberof();
276}
277
278sub set_number {
279 my $self = shift (@_);
280 my ($id) = @_;
281 $self->{'idnum'} = $id;
282}
283
284sub get_number {
285 my $self = shift (@_);
286 return $self->{'idnum'};
287}
288
289sub classify {
290 my $self = shift (@_);
291 my ($doc_obj) = @_;
292
293 my $outhandle = $self->{'outhandle'};
294 &gsprintf($outhandle, "BasClass::classify {common.must_be_implemented}\n");
295}
296
297sub get_classify_info {
298 my $self = shift (@_);
299
300 my $outhandle = $self->{'outhandle'};
301 &gsprintf($outhandle, "BasClass::get_classify_info {common.must_be_implemented}\n");
302}
303
304sub supports_memberof {
305 my $self = shift(@_);
306
307 return "false";
308}
309
310# previously, if a buttonname wasn't specified, we just use the metadata value,
311# but with a list of metadata, we want to do something a bit nicer so that
312# eg -metadata dc.Title,Title will end up with Title as the buttonname
313
314# current algorithm - use the first element, but strip its namespace
315sub generate_title_from_metadata {
316
317 my $self = shift (@_);
318 my $metadata = shift (@_);
319
320 return "" unless defined $metadata && $metadata =~ /\S/;
321
322 my @metalist = split(/,/, $metadata);
323 my $firstmeta = $metalist[0];
324 if ($firstmeta =~ /\./) {
325 $firstmeta =~ s/^\w+\.//;
326 }
327 return $firstmeta;
328}
329
3301;
Note: See TracBrowser for help on using the repository browser.