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

Last change on this file since 11681 was 11681, checked in by kjdon, 18 years ago

print_xml_usage and print_xml_header now take arguments

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