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