source: trunk/gsdl/perllib/classify.pm@ 6983

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

added a new load_classifier_for_info method which is used by classinfo.pl - this one adds a new arg into the classifier options (-gsdlinfo), so that the arguments are not parsed - some classifiers die if their required args are not set, but don't want this to happen for classinfo.pl

  • Property svn:keywords set to Author Date Id Revision
File size: 7.8 KB
Line 
1###########################################################################
2#
3# classify.pm --
4# A component of the Greenstone digital library software
5# from the New Zealand Digital Library Project at the
6# University of Waikato, New Zealand.
7#
8# Copyright (C) 1999 New Zealand Digital Library Project
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
25
26# functions to handle classifiers
27
28package classify;
29
30require util;
31use gsprintf;
32
33
34sub gsprintf
35{
36 return &gsprintf::gsprintf(@_);
37}
38
39
40$next_classify_num = 1;
41$collage = 0;
42sub load_classifier_for_info {
43 my ($classifier) = shift @_;
44
45 # find the classifier
46 my $colclassname = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},
47 "perllib/classify",
48 "${classifier}.pm");
49 my $mainclassname = &util::filename_cat($ENV{'GSDLHOME'},
50 "perllib/classify",
51 "${classifier}.pm");
52
53 if (-e $colclassname) { require $colclassname; }
54 elsif (-e $mainclassname) { require $mainclassname; }
55 else {
56 &gsprintf(STDERR, "{classify.could_not_find_classifier}\n", $classifier) && die "\n";
57 }
58 my ($classobj);
59 my $options = "-gsdlinfo";
60
61 eval ("\$classobj = new \$classifier($options)");
62 die "$@" if $@;
63
64 return $classobj;
65}
66
67sub load_classifiers {
68 my ($classify_list, $build_dir, $outhandle) = @_;
69 my @classify_objects = ();
70
71 foreach $classifyoption (@$classify_list) {
72
73 # get the classifier name
74 my $classname = shift @$classifyoption;
75 next unless defined $classname;
76
77 # find the classifier
78 my $colclassname = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},"perllib/classify",
79 "${classname}.pm");
80 my $mainclassname = &util::filename_cat($ENV{'GSDLHOME'},"perllib/classify",
81 "${classname}.pm");
82
83 if (-e $colclassname) { require $colclassname; }
84 elsif (-e $mainclassname) { require $mainclassname; }
85 else { &gsprintf(STDERR, "{classify.could_not_find_classifier}\n", $classname) && die "\n";
86 # die "ERROR - couldn't find classifier \"$classname\"\n";
87 }
88
89 # create the classify object
90 my ($classobj);
91
92 my @newoptions;
93
94 # do these first so they can be overriden by user supplied options
95 push @newoptions, "-builddir", "$build_dir" if ($build_dir);
96 push @newoptions, "-outhandle", "$outhandle" if ($outhandle);
97 push @newoptions, "-verbosity", "2";
98
99 # backwards compatability hack: if the classifier options are
100 # in "x=y" format, convert them to parsearg ("-x y") format.
101 my ($opt, $key, $value);
102 foreach $opt (@$classifyoption) {
103 if ($opt =~ /^(\w+)=(.*)$/) {
104 push @newoptions, "-$1", $2;
105 } else {
106 push @newoptions, $opt;
107 }
108 }
109
110 map { $_ = "\"$_\""; } @newoptions;
111 my $options .= join (",", @newoptions);
112
113 eval ("\$classobj = new \$classname($options)");
114 die "$@" if $@;
115
116 # add this object to the list
117 push (@classify_objects, $classobj);
118 }
119
120 return \@classify_objects;
121}
122
123# init_classifiers resets all the classifiers and readys them to process
124# the documents.
125sub init_classifiers {
126 my ($classifiers) = @_;
127
128 foreach $classobj (@$classifiers) {
129 $classobj->init();
130 }
131}
132
133# classify_doc lets each of the classifiers classify a document
134sub classify_doc {
135 my ($classifiers, $doc_obj) = @_;
136
137 foreach $classobj (@$classifiers) {
138 $classobj->classify($doc_obj);
139 }
140}
141
142# output_classify_info outputs all the info needed for the classification
143# to the gdbm
144sub output_classify_info {
145 my ($classifiers, $handle, $allclassifications, $gli) = @_;
146# $handle = "main::STDOUT";
147
148 $gli = 0 unless defined $gli;
149
150 # create a classification containing all the info
151 my $classifyinfo = {'classifyOID'=>'browse',
152 'contains'=>[]};
153
154 # get each of the classifications
155 foreach $classobj (@$classifiers) {
156 my $tempinfo = $classobj->get_classify_info($gli);
157 $tempinfo->{'classifyOID'} = "CL$next_classify_num";
158 $next_classify_num++;
159 push (@{$classifyinfo->{'contains'}}, $tempinfo);
160 }
161
162 &print_classify_info ($handle, $classifyinfo, "", $allclassifications);
163}
164
165sub print_classify_info {
166 my ($handle, $classifyinfo, $OID, $allclassifications) = @_;
167
168 $OID =~ s/^\.+//; # just for good luck
169
170 # book information is printed elsewhere
171 return if (defined ($classifyinfo->{'OID'}));
172
173 # don't want empty classifications
174 if ($allclassifications || &check_contents ($classifyinfo) > 0) {
175
176 $OID = $classifyinfo->{'classifyOID'} if defined ($classifyinfo->{'classifyOID'});
177
178 my $outputtext = "[$OID]\n";
179 $outputtext .= "<doctype>classify\n";
180 $outputtext .= "<hastxt>0\n";
181 $outputtext .= "<childtype>$classifyinfo->{'childtype'}\n"
182 if defined $classifyinfo->{'childtype'};
183 $outputtext .= "<Title>$classifyinfo->{'Title'}\n"
184 if defined $classifyinfo->{'Title'};
185 $outputtext .= "<numleafdocs>$classifyinfo->{'numleafdocs'}\n"
186 if defined $classifyinfo->{'numleafdocs'};
187 $outputtext .= "<thistype>$classifyinfo->{'thistype'}\n"
188 if defined $classifyinfo->{'thistype'};
189 $outputtext .= "<parameters>$classifyinfo->{'parameters'}\n"
190 if defined $classifyinfo->{'parameters'};
191
192 my $contains_text = "<contains>";
193 my $mdoffset_text = "<mdoffset>";
194
195 my $next_subOID = 1;
196 my $first = 1;
197 foreach $tempinfo (@{$classifyinfo->{'contains'}}) {
198 # empty contents were made undefined by clean_contents()
199 next unless defined $tempinfo;
200
201 $contains_text .= ";" unless $first;
202 $mdoffset_text .= ";" unless $first;
203 $first = 0;
204
205 if (defined ($tempinfo->{'classifyOID'})) {
206 # the third one is where we want to cut if it's a collage
207 if (defined ($tempinfo->{'Title'}) && $tempinfo->{'Title'} eq "Collage") {
208 $collage = 1;
209 }
210 $contains_text .= $tempinfo->{'classifyOID'};
211 &print_classify_info ($handle, $tempinfo, $tempinfo->{'classifyOID'},
212 $allclassifications);
213 $collage = 0;
214 } elsif (defined ($tempinfo->{'OID'})) {
215 $contains_text .= $tempinfo->{'OID'};
216 $mdoffset_text .= $tempinfo->{'offset'}
217 if (defined ($tempinfo->{'offset'}))
218 # note: we don't want to print the contents of the books
219 } else {
220 if (! $collage) { $contains_text .= "\".$next_subOID"; }
221 else { push (@{$tempinfo->{'childtype'}}, ""); $containstext = "";}
222 &print_classify_info ($handle, $tempinfo, "$OID.$next_subOID",
223 $allclassifications);
224 $next_subOID++;
225 }
226 }
227
228 $outputtext .= "$contains_text\n";
229 $outputtext .= "<mdtype>$classifyinfo->{'mdtype'}\n"
230 if defined $classifyinfo->{'mdtype'};
231 $outputtext .= "$mdoffset_text\n"
232 if ($mdoffset_text !~ m/^<mdoffset>;+$/);
233
234 $outputtext .= '-' x 70 . "\n";
235
236 print $handle $outputtext;
237 }
238}
239
240sub check_contents {
241 my ($classifyinfo) = @_;
242 my $num_leaf_docs = 0;
243 my $sub_num_leaf_docs = 0;
244
245 return $classifyinfo->{'numleafdocs'} if (defined $classifyinfo->{'numleafdocs'});
246
247 foreach $content (@{$classifyinfo->{'contains'}}) {
248 if (defined $content->{'OID'}) {
249 # found a book
250 $num_leaf_docs ++;
251 } elsif (($sub_num_leaf_docs = &check_contents ($content)) > 0) {
252 # there's a book somewhere below
253 $num_leaf_docs += $sub_num_leaf_docs;
254 } else {
255 # section contains no books so we want to remove
256 # it from its parents contents
257 $content = undef;
258 }
259 }
260
261 $classifyinfo->{'numleafdocs'} = $num_leaf_docs;
262 return $num_leaf_docs;
263}
264
2651;
Note: See TracBrowser for help on using the repository browser.