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

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

Jeffrey's new parsing modifications, committed approx 6 July, 15.16

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