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

Last change on this file since 6761 was 6332, checked in by jmt12, 20 years ago

When -gli argument is provided to calling script these modules will now output gli specific, non-language nor verbosity specific, messages

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