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

Last change on this file since 6911 was 6809, checked in by mdewsnip, 20 years ago

Additions for the GsdlCollageApplet: a classifier that displays a collage of the images in a collection. By Katrina Edgar (kde2).

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