source: trunk/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
RevLine 
[537]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
[214]26# functions to handle classifiers
27
28package classify;
29
30require util;
[5682]31use gsprintf;
[214]32
33
[5682]34sub gsprintf
35{
36 return &gsprintf::gsprintf(@_);
37}
38
39
[315]40$next_classify_num = 1;
[6967]41sub load_classifier_for_info {
42 my ($classifier) = shift @_;
[214]43
[6967]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
[811]66sub load_classifiers {
[1839]67 my ($classify_list, $build_dir, $outhandle) = @_;
[811]68 my @classify_objects = ();
69
70 foreach $classifyoption (@$classify_list) {
[214]71
[811]72 # get the classifier name
73 my $classname = shift @$classifyoption;
74 next unless defined $classname;
[1839]75
[811]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");
[214]81
[811]82 if (-e $colclassname) { require $colclassname; }
83 elsif (-e $mainclassname) { require $mainclassname; }
[5682]84 else { &gsprintf(STDERR, "{classify.could_not_find_classifier}\n", $classname) && die "\n";
85 # die "ERROR - couldn't find classifier \"$classname\"\n";
86 }
[214]87
[811]88 # create the classify object
89 my ($classobj);
[1839]90
91 my @newoptions;
[6964]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
[6967]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);
[1839]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
[1515]112 eval ("\$classobj = new \$classname($options)");
[811]113 die "$@" if $@;
[1839]114
115 # add this object to the list
[811]116 push (@classify_objects, $classobj);
117 }
118
119 return \@classify_objects;
[214]120}
121
122# init_classifiers resets all the classifiers and readys them to process
[315]123# the documents.
[214]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 {
[6332]144 my ($classifiers, $handle, $allclassifications, $gli) = @_;
[214]145# $handle = "main::STDOUT";
146
[6332]147 $gli = 0 unless defined $gli;
148
[315]149 # create a classification containing all the info
150 my $classifyinfo = {'classifyOID'=>'browse',
151 'contains'=>[]};
152
153 # get each of the classifications
[214]154 foreach $classobj (@$classifiers) {
[6332]155 my $tempinfo = $classobj->get_classify_info($gli);
[315]156 $tempinfo->{'classifyOID'} = "CL$next_classify_num";
157 $next_classify_num++;
158 push (@{$classifyinfo->{'contains'}}, $tempinfo);
[214]159 }
160
[315]161 &print_classify_info ($handle, $classifyinfo, "", $allclassifications);
[214]162}
163
[315]164sub print_classify_info {
165 my ($handle, $classifyinfo, $OID, $allclassifications) = @_;
[831]166
[315]167 $OID =~ s/^\.+//; # just for good luck
[214]168
[315]169 # book information is printed elsewhere
170 return if (defined ($classifyinfo->{'OID'}));
171
172 # don't want empty classifications
[637]173 if ($allclassifications || &check_contents ($classifyinfo) > 0) {
[315]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";
[652]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'};
[2024]188 $outputtext .= "<parameters>$classifyinfo->{'parameters'}\n"
189 if defined $classifyinfo->{'parameters'};
[315]190
[831]191 my $contains_text = "<contains>";
192 my $mdoffset_text = "<mdoffset>";
[315]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
[831]200 $contains_text .= ";" unless $first;
201 $mdoffset_text .= ";" unless $first;
[315]202 $first = 0;
203
204 if (defined ($tempinfo->{'classifyOID'})) {
[831]205 $contains_text .= $tempinfo->{'classifyOID'};
[315]206 &print_classify_info ($handle, $tempinfo, $tempinfo->{'classifyOID'},
207 $allclassifications);
208 } elsif (defined ($tempinfo->{'OID'})) {
[831]209 $contains_text .= $tempinfo->{'OID'};
210 $mdoffset_text .= $tempinfo->{'offset'}
211 if (defined ($tempinfo->{'offset'}))
[315]212 # note: we don't want to print the contents of the books
213 } else {
[7346]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
[315]223 &print_classify_info ($handle, $tempinfo, "$OID.$next_subOID",
224 $allclassifications);
225 $next_subOID++;
226 }
227 }
[831]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
[315]235 $outputtext .= '-' x 70 . "\n";
236
237 print $handle $outputtext;
238 }
239}
240
[637]241sub check_contents {
[315]242 my ($classifyinfo) = @_;
[637]243 my $num_leaf_docs = 0;
244 my $sub_num_leaf_docs = 0;
[315]245
[637]246 return $classifyinfo->{'numleafdocs'} if (defined $classifyinfo->{'numleafdocs'});
247
[315]248 foreach $content (@{$classifyinfo->{'contains'}}) {
249 if (defined $content->{'OID'}) {
250 # found a book
[637]251 $num_leaf_docs ++;
252 } elsif (($sub_num_leaf_docs = &check_contents ($content)) > 0) {
[315]253 # there's a book somewhere below
[637]254 $num_leaf_docs += $sub_num_leaf_docs;
[315]255 } else {
256 # section contains no books so we want to remove
257 # it from its parents contents
258 $content = undef;
259 }
260 }
[637]261
262 $classifyinfo->{'numleafdocs'} = $num_leaf_docs;
263 return $num_leaf_docs;
[315]264}
265
[214]2661;
Note: See TracBrowser for help on using the repository browser.