source: tags/gsdl-2_51-distribution/gsdl/perllib/classify.pm@ 7622

Last change on this file since 7622 was 7346, checked in by davidb, 20 years ago

Collage specific code made more general.

  • 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;
41sub load_classifier_for_info {
42 my ($classifier) = shift @_;
43
44 # find the classifier
45 my $colclassname = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},
46 "perllib/classify",
47 "${classifier}.pm");
48 my $mainclassname = &util::filename_cat($ENV{'GSDLHOME'},
49 "perllib/classify",
50 "${classifier}.pm");
51
52 if (-e $colclassname) { require $colclassname; }
53 elsif (-e $mainclassname) { require $mainclassname; }
54 else {
55 &gsprintf(STDERR, "{classify.could_not_find_classifier}\n", $classifier) && die "\n";
56 }
57 my ($classobj);
58 my $options = "-gsdlinfo";
59
60 eval ("\$classobj = new \$classifier($options)");
61 die "$@" if $@;
62
63 return $classobj;
64}
65
66sub load_classifiers {
67 my ($classify_list, $build_dir, $outhandle) = @_;
68 my @classify_objects = ();
69
70 foreach $classifyoption (@$classify_list) {
71
72 # get the classifier name
73 my $classname = shift @$classifyoption;
74 next unless defined $classname;
75
76 # find the classifier
77 my $colclassname = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},"perllib/classify",
78 "${classname}.pm");
79 my $mainclassname = &util::filename_cat($ENV{'GSDLHOME'},"perllib/classify",
80 "${classname}.pm");
81
82 if (-e $colclassname) { require $colclassname; }
83 elsif (-e $mainclassname) { require $mainclassname; }
84 else { &gsprintf(STDERR, "{classify.could_not_find_classifier}\n", $classname) && die "\n";
85 # die "ERROR - couldn't find classifier \"$classname\"\n";
86 }
87
88 # create the classify object
89 my ($classobj);
90
91 my @newoptions;
92
93 # do these first so they can be overriden by user supplied options
94 push @newoptions, "-builddir", "$build_dir" if ($build_dir);
95 push @newoptions, "-outhandle", "$outhandle" if ($outhandle);
96 push @newoptions, "-verbosity", "2";
97
98 # backwards compatability hack: if the classifier options are
99 # in "x=y" format, convert them to parsearg ("-x y") format.
100 my ($opt, $key, $value);
101 foreach $opt (@$classifyoption) {
102 if ($opt =~ /^(\w+)=(.*)$/) {
103 push @newoptions, "-$1", $2;
104 } else {
105 push @newoptions, $opt;
106 }
107 }
108
109 map { $_ = "\"$_\""; } @newoptions;
110 my $options .= join (",", @newoptions);
111
112 eval ("\$classobj = new \$classname($options)");
113 die "$@" if $@;
114
115 # add this object to the list
116 push (@classify_objects, $classobj);
117 }
118
119 return \@classify_objects;
120}
121
122# init_classifiers resets all the classifiers and readys them to process
123# the documents.
124sub init_classifiers {
125 my ($classifiers) = @_;
126
127 foreach $classobj (@$classifiers) {
128 $classobj->init();
129 }
130}
131
132# classify_doc lets each of the classifiers classify a document
133sub classify_doc {
134 my ($classifiers, $doc_obj) = @_;
135
136 foreach $classobj (@$classifiers) {
137 $classobj->classify($doc_obj);
138 }
139}
140
141# output_classify_info outputs all the info needed for the classification
142# to the gdbm
143sub output_classify_info {
144 my ($classifiers, $handle, $allclassifications, $gli) = @_;
145# $handle = "main::STDOUT";
146
147 $gli = 0 unless defined $gli;
148
149 # create a classification containing all the info
150 my $classifyinfo = {'classifyOID'=>'browse',
151 'contains'=>[]};
152
153 # get each of the classifications
154 foreach $classobj (@$classifiers) {
155 my $tempinfo = $classobj->get_classify_info($gli);
156 $tempinfo->{'classifyOID'} = "CL$next_classify_num";
157 $next_classify_num++;
158 push (@{$classifyinfo->{'contains'}}, $tempinfo);
159 }
160
161 &print_classify_info ($handle, $classifyinfo, "", $allclassifications);
162}
163
164sub print_classify_info {
165 my ($handle, $classifyinfo, $OID, $allclassifications) = @_;
166
167 $OID =~ s/^\.+//; # just for good luck
168
169 # book information is printed elsewhere
170 return if (defined ($classifyinfo->{'OID'}));
171
172 # don't want empty classifications
173 if ($allclassifications || &check_contents ($classifyinfo) > 0) {
174
175 $OID = $classifyinfo->{'classifyOID'} if defined ($classifyinfo->{'classifyOID'});
176
177 my $outputtext = "[$OID]\n";
178 $outputtext .= "<doctype>classify\n";
179 $outputtext .= "<hastxt>0\n";
180 $outputtext .= "<childtype>$classifyinfo->{'childtype'}\n"
181 if defined $classifyinfo->{'childtype'};
182 $outputtext .= "<Title>$classifyinfo->{'Title'}\n"
183 if defined $classifyinfo->{'Title'};
184 $outputtext .= "<numleafdocs>$classifyinfo->{'numleafdocs'}\n"
185 if defined $classifyinfo->{'numleafdocs'};
186 $outputtext .= "<thistype>$classifyinfo->{'thistype'}\n"
187 if defined $classifyinfo->{'thistype'};
188 $outputtext .= "<parameters>$classifyinfo->{'parameters'}\n"
189 if defined $classifyinfo->{'parameters'};
190
191 my $contains_text = "<contains>";
192 my $mdoffset_text = "<mdoffset>";
193
194 my $next_subOID = 1;
195 my $first = 1;
196 foreach $tempinfo (@{$classifyinfo->{'contains'}}) {
197 # empty contents were made undefined by clean_contents()
198 next unless defined $tempinfo;
199
200 $contains_text .= ";" unless $first;
201 $mdoffset_text .= ";" unless $first;
202 $first = 0;
203
204 if (defined ($tempinfo->{'classifyOID'})) {
205 $contains_text .= $tempinfo->{'classifyOID'};
206 &print_classify_info ($handle, $tempinfo, $tempinfo->{'classifyOID'},
207 $allclassifications);
208 } elsif (defined ($tempinfo->{'OID'})) {
209 $contains_text .= $tempinfo->{'OID'};
210 $mdoffset_text .= $tempinfo->{'offset'}
211 if (defined ($tempinfo->{'offset'}))
212 # note: we don't want to print the contents of the books
213 } else {
214
215 # Supress having top-level node in Collage classifier
216 # so no bookshelf icon appears, top-level, along with the
217 # applet
218
219 if (!defined ($tempinfo->{'Title'}) || $tempinfo->{'Title'} ne "Collage") {
220 $contains_text .= "\".$next_subOID";
221 }
222
223 &print_classify_info ($handle, $tempinfo, "$OID.$next_subOID",
224 $allclassifications);
225 $next_subOID++;
226 }
227 }
228
229 $outputtext .= "$contains_text\n";
230 $outputtext .= "<mdtype>$classifyinfo->{'mdtype'}\n"
231 if defined $classifyinfo->{'mdtype'};
232 $outputtext .= "$mdoffset_text\n"
233 if ($mdoffset_text !~ m/^<mdoffset>;+$/);
234
235 $outputtext .= '-' x 70 . "\n";
236
237 print $handle $outputtext;
238 }
239}
240
241sub check_contents {
242 my ($classifyinfo) = @_;
243 my $num_leaf_docs = 0;
244 my $sub_num_leaf_docs = 0;
245
246 return $classifyinfo->{'numleafdocs'} if (defined $classifyinfo->{'numleafdocs'});
247
248 foreach $content (@{$classifyinfo->{'contains'}}) {
249 if (defined $content->{'OID'}) {
250 # found a book
251 $num_leaf_docs ++;
252 } elsif (($sub_num_leaf_docs = &check_contents ($content)) > 0) {
253 # there's a book somewhere below
254 $num_leaf_docs += $sub_num_leaf_docs;
255 } else {
256 # section contains no books so we want to remove
257 # it from its parents contents
258 $content = undef;
259 }
260 }
261
262 $classifyinfo->{'numleafdocs'} = $num_leaf_docs;
263 return $num_leaf_docs;
264}
265
2661;
Note: See TracBrowser for help on using the repository browser.