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

Last change on this file since 6956 was 6956, checked in by kjdon, 20 years ago

removed some commented out stuff, added a new subroutine: format_metadata_for_sorting. all classifiers do this stuff, and they all do it a bit differently - put it here so they can share it and keep it consistent

  • Property svn:keywords set to Author Date Id Revision
File size: 7.9 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;
58
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# 'deft' => "2",
73# 'reqd' => "no" } ];
74# { 'name' => "ignore_namespace",
75# 'desc' => "{BasClas.ignore_namespace}",
76# 'type' => "flag"}
77 ];
78
79my $options = { 'name' => "BasClas",
80 'desc' => "{BasClas.desc}",
81 'abstract' => "yes",
82 'inherits' => "no",
83 'args' => $arguments };
84
85
86sub gsprintf
87{
88 return &gsprintf::gsprintf(@_);
89}
90
91
92sub print_xml_usage
93{
94 local $self = shift(@_);
95
96 # XML output is always in UTF-8
97 &gsprintf::output_strings_in_UTF8;
98
99 &PrintUsage::print_xml_header();
100 $self->print_xml();
101}
102
103
104sub print_xml
105{
106 local $self = shift(@_);
107
108 local $optionlistref = $self->{'option_list'};
109 local @optionlist = @$optionlistref;
110 local $classifieroptions = pop(@$optionlistref);
111 return if (!defined($classifieroptions));
112
113 print STDERR "<ClassInfo>\n";
114 print STDERR " <Name>$classifieroptions->{'name'}</Name>\n";
115 print STDERR " <Desc>$classifieroptions->{'desc'}</Desc>\n";
116 print STDERR " <Abstract>$classifieroptions->{'abstract'}</Abstract>\n";
117 print STDERR " <Inherits>$classifieroptions->{'inherits'}</Inherits>\n";
118 print STDERR " <Arguments>\n";
119 if (defined($classifieroptions->{'args'})) {
120 &PrintUsage::print_options_xml($classifieroptions->{'args'});
121 }
122
123 # Recurse up the classifier hierarchy
124 $self->print_xml();
125
126 print STDERR " </Arguments>\n";
127 print STDERR "</ClassInfo>\n";
128}
129
130
131sub print_txt_usage
132{
133 local $self = shift(@_);
134
135 # Print the usage message for a classifier (recursively)
136 local $descoffset = $self->determine_description_offset(0);
137 $self->print_classifier_usage($descoffset, 1);
138}
139
140
141sub determine_description_offset
142{
143 local $self = shift(@_);
144 local $maxoffset = shift(@_);
145
146 local $optionlistref = $self->{'option_list'};
147 local @optionlist = @$optionlistref;
148 local $classifieroptions = pop(@$optionlistref);
149 return $maxoffset if (!defined($classifieroptions));
150
151 # Find the length of the longest option string of this classifier
152 local $classifierargs = $classifieroptions->{'args'};
153 if (defined($classifierargs)) {
154 local $longest = &PrintUsage::find_longest_option_string($classifierargs);
155 if ($longest > $maxoffset) {
156 $maxoffset = $longest;
157 }
158 }
159
160 # Recurse up the classifier hierarchy
161 $maxoffset = $self->determine_description_offset($maxoffset);
162 $self->{'option_list'} = \@optionlist;
163 return $maxoffset;
164}
165
166
167sub print_classifier_usage
168{
169 local $self = shift(@_);
170 local $descoffset = shift(@_);
171 local $isleafclass = shift(@_);
172
173 local $optionlistref = $self->{'option_list'};
174 local @optionlist = @$optionlistref;
175 local $classifieroptions = pop(@$optionlistref);
176 return if (!defined($classifieroptions));
177
178 local $classifiername = $classifieroptions->{'name'};
179 local $classifierargs = $classifieroptions->{'args'};
180 local $classifierdesc = $classifieroptions->{'desc'};
181 # Produce the usage information using the data structure above
182 if ($isleafclass) {
183 if (defined($classifierdesc)) {
184 &gsprintf(STDERR, "$classifierdesc\n\n");
185 }
186 &gsprintf(STDERR, " {common.usage}: classify $classifiername [{common.options}]\n\n");
187
188 }
189
190 # Display the classifier options, if there are some
191 if (defined($classifierargs)) {
192 # Calculate the column offset of the option descriptions
193 local $optiondescoffset = $descoffset + 2; # 2 spaces between options & descriptions
194
195 if ($isleafclass) {
196 &gsprintf(STDERR, " {common.specific_options}:\n");
197 }
198 else {
199 &gsprintf(STDERR, " {common.general_options}:\n", $classifiername);
200 }
201
202 # Display the classifier options
203 &PrintUsage::print_options_txt($classifierargs, $optiondescoffset);
204 }
205
206 # Recurse up the classifier hierarchy
207 $self->print_classifier_usage($descoffset, 0);
208 $self->{'option_list'} = \@optionlist;
209}
210
211
212sub new {
213 my $class = shift (@_);
214 my $name = shift (@_);
215
216 my $self = {};
217
218 $self->{'outhandle'} = STDERR;
219
220 $self->{'option_list'} = [ $options ];
221
222 # general options available to all classifiers
223 if (!parsargv::parse(\@_,
224 q^builddir/.*/^, \$self->{'builddir'},
225 q^outhandle/.*/STDERR^, \$self->{'outhandle'},
226 q^verbosity/\d/2^, \$self->{'verbosity'},
227 q^ignore_namespace^, \$self->{'ignore_namespace'},
228 "allow_extra_options")) {
229
230 &gsprintf(STDERR, "\n{BasClas.bad_general_option}\n", $name);
231 $self->print_txt_usage(""); # Use default resource bundle
232 die "\n";
233 }
234
235 return bless $self, $class;
236}
237
238sub init {
239 my $self = shift (@_);
240}
241
242sub classify {
243 my $self = shift (@_);
244 my ($doc_obj) = @_;
245
246 my $outhandle = $self->{'outhandle'};
247 &gsprintf($outhandle, "BasClass::classify {common.must_be_implemented}\n");
248}
249
250sub get_classify_info {
251 my $self = shift (@_);
252
253 my $outhandle = $self->{'outhandle'};
254 &gsprintf($outhandle, "BasClass::get_classify_info {common.must_be_implemented}\n");
255}
256
257# all classifiers do something like this, but all slightly different
258# so put it here so they can all share it.
259sub format_metadata_for_sorting {
260
261 my $self = shift (@_);
262
263 my ($metaname, $metavalue, $doc_obj) = @_;
264
265 if ($metaname eq "Language") {
266 $metavalue = $iso639::fromiso639{$metavalue};
267 return $metavalue;
268 }
269
270 my $lang = $doc_obj->get_metadata_element ($doc_obj->get_top_section(), 'Language');
271 $lang = 'en' unless defined $lang;
272 if ($lang eq 'en') {
273 if ($metaname eq "Creator") {
274 &sorttools::format_string_name_english (\$metavalue);
275 } else {
276 &sorttools::format_string_english (\$metavalue);
277 }
278 }
279 return $metavalue;
280}
281
282
2831;
Note: See TracBrowser for help on using the repository browser.