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

Last change on this file since 740 was 652, checked in by sjboddie, 25 years ago

redesigned browsing support

  • Property svn:keywords set to Author Date Id Revision
File size: 5.7 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;
31
32
33$next_classify_num = 1;
34
35# load_classifier will load one classifier. $classinfo is an
36# array containing information about the classifier to load. The
37# first element of the array is the name of the classifier, the
38# rest of the elements are classifier specific.
39sub load_classifier {
40 my ($classinfo) = @_;
41
42 # get the classifier name
43 my $classname = shift(@$classinfo);
44 return undef unless defined $classname;
45
46 # find the classifier
47 my $colclassname = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},"perllib/classify",
48 "${classname}.pm");
49 my $mainclassname = &util::filename_cat($ENV{'GSDLHOME'},"perllib/classify",
50 "${classname}.pm");
51 if (-e $colclassname) { require $colclassname; }
52 elsif (-e $mainclassname) { require $mainclassname; }
53 else { die "ERROR - couldn't find classifier \"$classname\"\n"; }
54
55 # create the classify object
56 my ($classobj);
57 map { $_ = "\"$_\""; } @$classinfo;
58 my $options = join (",", @$classinfo);
59 eval ("\$classobj = new $classname($options)");
60 die "$@" if $@;
61
62 return $classobj;
63}
64
65# init_classifiers resets all the classifiers and readys them to process
66# the documents.
67sub init_classifiers {
68 my ($classifiers) = @_;
69
70 foreach $classobj (@$classifiers) {
71 $classobj->init();
72 }
73}
74
75# classify_doc lets each of the classifiers classify a document
76sub classify_doc {
77 my ($classifiers, $doc_obj) = @_;
78
79 foreach $classobj (@$classifiers) {
80 $classobj->classify($doc_obj);
81 }
82}
83
84# output_classify_info outputs all the info needed for the classification
85# to the gdbm
86sub output_classify_info {
87 my ($classifiers, $handle, $allclassifications) = @_;
88# $handle = "main::STDOUT";
89
90 # create a classification containing all the info
91 my $classifyinfo = {'classifyOID'=>'browse',
92 'contains'=>[]};
93
94 # get each of the classifications
95 foreach $classobj (@$classifiers) {
96 my $tempinfo = $classobj->get_classify_info();
97 $tempinfo->{'classifyOID'} = "CL$next_classify_num";
98 $next_classify_num++;
99 push (@{$classifyinfo->{'contains'}}, $tempinfo);
100 }
101
102 &print_classify_info ($handle, $classifyinfo, "", $allclassifications);
103}
104
105sub print_classify_info {
106 my ($handle, $classifyinfo, $OID, $allclassifications) = @_;
107 $OID =~ s/^\.+//; # just for good luck
108
109 # book information is printed elsewhere
110 return if (defined ($classifyinfo->{'OID'}));
111
112 # don't want empty classifications
113 if ($allclassifications || &check_contents ($classifyinfo) > 0) {
114
115 $OID = $classifyinfo->{'classifyOID'} if defined ($classifyinfo->{'classifyOID'});
116
117 my $outputtext = "[$OID]\n";
118 $outputtext .= "<doctype>classify\n";
119 $outputtext .= "<hastxt>0\n";
120 $outputtext .= "<childtype>$classifyinfo->{'childtype'}\n"
121 if defined $classifyinfo->{'childtype'};
122 $outputtext .= "<Title>$classifyinfo->{'Title'}\n"
123 if defined $classifyinfo->{'Title'};
124 $outputtext .= "<numleafdocs>$classifyinfo->{'numleafdocs'}\n"
125 if defined $classifyinfo->{'numleafdocs'};
126 $outputtext .= "<thistype>$classifyinfo->{'thistype'}\n"
127 if defined $classifyinfo->{'thistype'};
128
129
130 $outputtext .= "<contains>";
131
132 my $next_subOID = 1;
133 my $first = 1;
134 foreach $tempinfo (@{$classifyinfo->{'contains'}}) {
135 # empty contents were made undefined by clean_contents()
136 next unless defined $tempinfo;
137
138 $outputtext .= ";" unless $first;
139 $first = 0;
140
141 if (defined ($tempinfo->{'classifyOID'})) {
142 $outputtext .= $tempinfo->{'classifyOID'};
143 &print_classify_info ($handle, $tempinfo, $tempinfo->{'classifyOID'},
144 $allclassifications);
145 } elsif (defined ($tempinfo->{'OID'})) {
146 $outputtext .= $tempinfo->{'OID'};
147 # note: we don't want to print the contents of the books
148 } else {
149 $outputtext .= "\".$next_subOID";
150 &print_classify_info ($handle, $tempinfo, "$OID.$next_subOID",
151 $allclassifications);
152 $next_subOID++;
153 }
154 }
155 $outputtext .= "\n";
156 $outputtext .= '-' x 70 . "\n";
157
158 print $handle $outputtext;
159 }
160}
161
162sub check_contents {
163 my ($classifyinfo) = @_;
164 my $num_leaf_docs = 0;
165 my $sub_num_leaf_docs = 0;
166
167 return $classifyinfo->{'numleafdocs'} if (defined $classifyinfo->{'numleafdocs'});
168
169 foreach $content (@{$classifyinfo->{'contains'}}) {
170 if (defined $content->{'OID'}) {
171 # found a book
172 $num_leaf_docs ++;
173 } elsif (($sub_num_leaf_docs = &check_contents ($content)) > 0) {
174 # there's a book somewhere below
175 $num_leaf_docs += $sub_num_leaf_docs;
176 } else {
177 # section contains no books so we want to remove
178 # it from its parents contents
179 $content = undef;
180 }
181 }
182
183 $classifyinfo->{'numleafdocs'} = $num_leaf_docs;
184 return $num_leaf_docs;
185}
186
1871;
Note: See TracBrowser for help on using the repository browser.