source: main/tags/2.00/gsdl/perllib/classify.pm@ 24239

Last change on this file since 24239 was 537, checked in by sjboddie, 25 years ago

added GPL headers

  • Property svn:keywords set to Author Date Id Revision
File size: 5.3 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 || &clean_contents ($classifyinfo)) {
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 .= "<classifytype>$classifyinfo->{'classifytype'}\n" if defined $classifyinfo->{'classifytype'};
121 $outputtext .= "<Title>$classifyinfo->{'Title'}\n" if defined $classifyinfo->{'Title'};
122
123 $outputtext .= "<contains>";
124
125 my $next_subOID = 1;
126 my $first = 1;
127 foreach $tempinfo (@{$classifyinfo->{'contains'}}) {
128 # empty contents were made undefined by clean_contents()
129 next unless defined $tempinfo;
130
131 $outputtext .= ";" unless $first;
132 $first = 0;
133
134 if (defined ($tempinfo->{'classifyOID'})) {
135 $outputtext .= $tempinfo->{'classifyOID'};
136 &print_classify_info ($handle, $tempinfo, $tempinfo->{'classifyOID'},
137 $allclassifications);
138 } elsif (defined ($tempinfo->{'OID'})) {
139 $outputtext .= $tempinfo->{'OID'};
140 # note: we don't want to print the contents of the books
141 } else {
142 $outputtext .= "\".$next_subOID";
143 &print_classify_info ($handle, $tempinfo, "$OID.$next_subOID",
144 $allclassifications);
145 $next_subOID++;
146 }
147 }
148 $outputtext .= "\n";
149 $outputtext .= '-' x 70 . "\n";
150
151 print $handle $outputtext;
152 }
153}
154
155sub clean_contents {
156 my ($classifyinfo) = @_;
157 my $has_content = 0;
158
159 foreach $content (@{$classifyinfo->{'contains'}}) {
160 if (defined $content->{'OID'}) {
161 # found a book
162 $has_content = 1;
163 } elsif (&clean_contents ($content)) {
164 # there's a book somewhere below
165 $has_content = 1;
166 } else {
167 # section contains no books so we want to remove
168 # it from its parents contents
169 $content = undef;
170 }
171 }
172 return $has_content;
173}
174
1751;
Note: See TracBrowser for help on using the repository browser.