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

Last change on this file since 15702 was 15702, checked in by mdewsnip, 16 years ago

(Adding new DB support) Tidying up output_classify_info() and changing print_classify_info() to use dbutil::write_infodb_entry() instead of having GDBM-specific code.

  • Property svn:keywords set to Author Date Id Revision
File size: 15.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;
31require AllList;
32use gsprintf;
33
34#use GDBM_File;
35use unbuildutil;
36
37
38sub gsprintf
39{
40 return &gsprintf::gsprintf(@_);
41}
42
43
44$oid_to_clids = {};
45
46sub load_classifier_for_info {
47 my ($classifier) = shift @_;
48
49 # find the classifier
50 my $customclassname;
51 if (defined($ENV{'GSDLCOLLECTION'}))
52 {
53 $customclassname = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "custom", $ENV{'GSDLCOLLECTION'},
54 "perllib", "classify", "${classifier}.pm");
55 }
56 my $colclassname = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "perllib", "classify", "${classifier}.pm");
57 my $mainclassname = &util::filename_cat($ENV{'GSDLHOME'}, "perllib", "classify", "${classifier}.pm");
58
59 if (defined($customclassname) && -e $customclassname) { require $customclassname; }
60 elsif (-e $colclassname) { require $colclassname; }
61 elsif (-e $mainclassname) { require $mainclassname; }
62 else {
63 &gsprintf(STDERR, "{classify.could_not_find_classifier}\n", $classifier) && die "\n";
64 }
65 my ($classobj);
66 my $options = "-gsdlinfo";
67 eval ("\$classobj = new \$classifier([],[$options])");
68 die "$@" if $@;
69
70 return $classobj;
71}
72
73sub load_classifiers {
74 my ($classify_list, $build_dir, $outhandle) = @_;
75 my @classify_objects = ();
76 my $classify_number = 1;
77
78 my $colclassdir = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},"perllib/classify");
79 unshift (@INC, $colclassdir);
80
81 foreach $classifyoption (@$classify_list) {
82
83 # get the classifier name
84 my $classname = shift @$classifyoption;
85 next unless defined $classname;
86
87 # find the classifier
88 my $customclassname = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "custom", $ENV{'GSDLCOLLECTION'},
89 "perllib", "classify", "${classname}.pm");
90 my $colclassname = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "perllib", "classify", "${classname}.pm");
91 my $mainclassname = &util::filename_cat($ENV{'GSDLHOME'}, "perllib", "classify", "${classname}.pm");
92
93 if (-e $customclassname) { require $customclassname; }
94 elsif (-e $colclassname) { require $colclassname; }
95 elsif (-e $mainclassname) { require $mainclassname; }
96 else { &gsprintf(STDERR, "{classify.could_not_find_classifier}\n", $classname) && die "\n";
97 # die "ERROR - couldn't find classifier \"$classname\"\n";
98 }
99
100 # create the classify object
101 my ($classobj);
102
103 my @newoptions;
104
105 # do these first so they can be overriden by user supplied options
106 push @newoptions, "-builddir", "$build_dir" if ($build_dir);
107 push @newoptions, "-outhandle", "$outhandle" if ($outhandle);
108 push @newoptions, "-verbosity", "2";
109
110 # backwards compatability hack: if the classifier options are
111 # in "x=y" format, convert them to parsearg ("-x y") format.
112 my ($opt, $key, $value);
113 foreach $opt (@$classifyoption) {
114 # if ($opt =~ /^(\w+)=(.*)$/) {
115 # push @newoptions, "-$1", $2;
116 # } else {
117 push @newoptions, $opt;
118 #}
119 }
120
121 map { $_ = "\"$_\""; } @newoptions;
122 my $options .= join (",", @newoptions);
123
124
125 eval ("\$classobj = new \$classname([],[$options])");
126 die "$@" if $@;
127
128 $classobj->set_number($classify_number);
129 $classify_number ++;
130
131 # add this object to the list
132 push (@classify_objects, $classobj);
133 }
134
135 my ($classobj);
136 eval ("\$classobj = new AllList()");
137 die "$@" if $@;
138 push (@classify_objects, $classobj);
139
140 return \@classify_objects;
141}
142
143# init_classifiers resets all the classifiers and readys them to process
144# the documents.
145sub init_classifiers {
146 my ($classifiers) = @_;
147
148 foreach $classobj (@$classifiers) {
149 $classobj->init();
150 }
151}
152
153
154
155# takes a hashref containing the metadata for a gdbmfile entry, and extracts
156# the childrens numbers (from the 'contains' entry).
157# assumes format is ".1;".2;".3
158sub get_children {
159 my ($doc_db_hash) = @_;
160
161 my $children = undef;
162
163 $childs = $doc_db_hash->{'contains'};
164 if (defined ($childs)) {
165 $childs =~ s/\@$//; #remove trailing @
166 $childs =~ s/^\"\.//; #remove initial ".
167 @$children = split /\;\"\./, $childs;
168
169 }
170
171 return $children;
172}
173
174
175sub recurse_sections {
176 my ($doc_obj, $children, $parentoid, $parentsection, $gdbm_recs) = @_;
177
178 return if (!defined $children);
179
180 foreach my $child (sort { $a <=> $b} @$children) {
181 $doc_obj->create_named_section("$parentsection.$child");
182 my $doc_db_rec = $gdbm_recs->{"$parentoid.$child"};
183 my $doc_db_hash = db_rec_to_hash($doc_db_rec);
184
185 # get child's children
186 my $newchildren = &get_children($doc_db_hash);
187
188 # add content for current section
189 add_section_content($doc_obj, "$parentsection.$child", $doc_db_hash);
190
191 # process all the children if there are any
192 if (defined ($newchildren))
193 {
194 recurse_sections($doc_obj, $newchildren, "$parentoid.$child",
195 "$parentsection.$child", $gdbm_recs);
196 }
197 }
198}
199
200
201sub add_section_content {
202 my ($doc_obj, $cursection, $doc_db_hash) = @_;
203
204 foreach $key (keys %$doc_db_hash) {
205 #don't need to store these metadata
206 next if $key =~ /(thistype|childtype|contains|docnum|doctype|classifytype)/i;
207 # but do want things like hastxt and archivedir
208 my @items = split /@/, $doc_db_hash->{$key};
209 map {$doc_obj->add_metadata ($cursection, $key, $_); } @items;
210
211 }
212}
213
214
215# gets all the metadata from a gdbm file entry, and puts it into a hashref
216sub db_rec_to_hash {
217
218 my ($gdb_str_ref) = @_;
219
220 my $hashref = {};
221
222 my @entries = split(/\n/, $gdb_str_ref);
223 foreach $entry (@entries) {
224 my($key, $value) = ($entry =~ /^<([^>]*)>(.*?)$/ );
225 $hashref->{$key} .= '@' if defined $hashref->{$key};
226 $hashref->{$key} .= $value;
227
228 }
229
230 return $hashref;
231}
232
233
234sub reconstruct_doc_objs_metadata
235{
236 my ($fulldbname) = @_;
237
238# tie %gdbm_recs, 'GDBM_File', $fulldbname, &GDBM_WRCREAT, 0640;
239
240 my %gdbm_recs;
241 &unbuildutil::read_gdbm($fulldbname,\%gdbm_recs);
242
243
244 # dig out top level doc sections
245 my %top_sections = ();
246 my %top_docnums = ();
247 foreach my $key ( keys %gdbm_recs )
248 {
249 my $md_rec = $gdbm_recs{$key};
250 my $md_hash = db_rec_to_hash($md_rec);
251
252 if ((defined $md_hash->{'doctype'}) && ($md_hash->{'doctype'} eq "doc")) {
253 next if ($key =~ m/\./);
254 $top_sections{$key} = $md_hash;
255 $top_docnums{$key} = $md_hash->{'docnum'};
256 }
257 }
258
259 # for greenstone document objects based on metadata in gdbm file
260 my @all_docs = ();
261 # we need to make sure the documents were processed in the same order as
262 # before, so sort based on their docnums
263 foreach my $oid ( sort { $top_docnums{$a} <=> $top_docnums{$b} } keys %top_sections )
264 {
265 my $doc_db_hash = $top_sections{$oid};
266
267 my $doc_obj = new doc();
268 $doc_obj->set_OID($oid);
269 my $top = $doc_obj->get_top_section();
270 add_section_content ($doc_obj, $top, $doc_db_hash);
271 my $children = &get_children($doc_db_hash);
272 recurse_sections($doc_obj, $children, $oid, $top, \%gdbm_recs);
273
274 push(@all_docs,$doc_obj);
275 }
276
277# untie %gdbm_recs;
278
279 return \@all_docs;
280}
281
282
283
284
285
286# classify_doc lets each of the classifiers classify a document
287sub classify_doc {
288 my ($classifiers, $doc_obj) = @_;
289
290 foreach $classobj (@$classifiers) {
291 my $title = $classobj->{'title'};
292 $classobj->classify($doc_obj);
293 }
294}
295
296
297# output_classify_info outputs all the info needed for the classification
298# to the database
299sub output_classify_info
300{
301 my ($classifiers, $infodb_handle, $remove_empty_classifications, $gli) = @_;
302
303 $gli = 0 unless defined $gli;
304
305 # create a classification containing all the info
306 my $classifyinfo = { 'classifyOID'=> 'browse',
307 'contains' => [] };
308
309 # get each of the classifications
310 my $next_classify_num = 1;
311 foreach $classifier (@$classifiers)
312 {
313 my $classifier_info = $classifier->get_classify_info($gli);
314 $classifier_info->{'classifyOID'} = "CL$next_classify_num" unless defined($classifier_info->{'classifyOID'});
315 print STDERR "*** outputting information for classifier: $classifier_info->{'classifyOID'}\n";
316
317 push(@{$classifyinfo->{'contains'}}, $classifier_info);
318 $next_classify_num++;
319 }
320
321 &print_classify_info ($infodb_handle, $classifyinfo, "", $remove_empty_classifications);
322}
323
324
325sub print_classify_info
326{
327 my ($infodb_handle, $classifyinfo, $OID, $remove_empty_classifications) = @_;
328
329 $OID =~ s/^\.+//; # just for good luck
330
331 # book information is printed elsewhere
332 return if (defined ($classifyinfo->{'OID'}));
333
334 # don't want empty classifications
335 return if (&check_contents ($classifyinfo, $remove_empty_classifications) == 0 && $remove_empty_classifications);
336
337 $OID = $classifyinfo->{'classifyOID'} if defined ($classifyinfo->{'classifyOID'});
338
339 my %classify_infodb = ();
340 $classify_infodb{"doctype"} = [ "classify" ];
341 $classify_infodb{"hastxt"} = [ "0" ];
342 $classify_infodb{"childtype"} = [ $classifyinfo->{'childtype'} ]
343 if defined $classifyinfo->{'childtype'};
344 $classify_infodb{"Title"} = [ $classifyinfo->{'Title'} ]
345 if defined $classifyinfo->{'Title'};
346 $classify_infodb{"numleafdocs"} = [ $classifyinfo->{'numleafdocs'} ]
347 if defined $classifyinfo->{'numleafdocs'};
348 $classify_infodb{"thistype"} = [ $classifyinfo->{'thistype'} ]
349 if defined $classifyinfo->{'thistype'};
350 $classify_infodb{"parameters"} = [ $classifyinfo->{'parameters'} ]
351 if defined $classifyinfo->{'parameters'};
352 $classify_infodb{"supportsmemberof"} = [ $classifyinfo->{'supportsmemberof'} ]
353 if defined $classifyinfo->{'supportsmemberof'};
354
355 my $contains_text = "";
356 my $mdoffset_text = "";
357
358 my $next_subOID = 1;
359 my $first = 1;
360 foreach my $tempinfo (@{$classifyinfo->{'contains'}}) {
361 # empty contents were made undefined by clean_contents()
362 next unless defined $tempinfo;
363
364 if (!defined ($tempinfo->{'classifyOID'}) ||
365 $tempinfo->{'classifyOID'} ne "oai") {
366 $contains_text .= ";" unless $first;
367 }
368 $mdoffset_text .= ";" unless $first;
369 $first = 0;
370
371 if (defined ($tempinfo->{'classifyOID'})) {
372 if ($tempinfo->{'classifyOID'} ne "oai") {
373 $contains_text .= $tempinfo->{'classifyOID'};
374 }
375
376 # Extra code for incremental building.
377 # We need to store a listing of the classifiers each DOI is in
378 my $clids = [];
379 #rint STDERR "==1. Recording reverse lookup for " . $tempinfo->{'classifyOID'} . "==\n";
380 if(defined($oid_to_clids->{$tempinfo->{'classifyOID'}})) {
381 #rint STDERR "Found existing array!\n";
382 $clids = $oid_to_clids->{$tempinfo->{'classifyOID'}};
383 }
384 #rint STDERR "Appended $OID to \"" . join(";", @{$clids}) . "\"\n";
385 push(@{$clids}, $OID);
386 $oid_to_clids->{$tempinfo->{'classifyOID'}} = $clids;
387 #rint STDERR "Result: \"" . join(";", @{$clids}) . "\"\n";
388
389 &print_classify_info ($infodb_handle, $tempinfo, $tempinfo->{'classifyOID'},
390 $remove_empty_classifications);
391 } elsif (defined ($tempinfo->{'OID'})) {
392 $contains_text .= $tempinfo->{'OID'};
393 $mdoffset_text .= $tempinfo->{'offset'} if (defined ($tempinfo->{'offset'}));
394
395
396 # note: we don't want to print the contents of the books
397 # Extra code for incremental building.
398 # We need to store a listing of the classifiers each DOI is in
399 my $clids = [];
400 #rint STDERR "==2. Recording reverse lookup for " . $tempinfo->{'OID'} . "==\n";
401 if(defined($oid_to_clids->{$tempinfo->{'OID'}})) {
402 #rint STDERR "Found existing array!\n";
403 $clids = $oid_to_clids->{$tempinfo->{'OID'}};
404 }
405 #rint STDERR "Appended $OID to \"" . join(";", @{$clids}) . "\"\n";
406 push(@{$clids}, $OID);
407 $oid_to_clids->{$tempinfo->{'OID'}} = $clids;
408 #rint STDERR "Result: \"" . join(";", @{$clids}) . "\"\n";
409
410
411 } else {
412
413 # Supress having top-level node in Collage classifier
414 # so no bookshelf icon appears, top-level, along with the
415 # applet
416
417 if (!defined ($tempinfo->{'Title'}) || $tempinfo->{'Title'} ne "Collage") {
418 $contains_text .= "\".$next_subOID";
419 }
420
421 # Extra code for incremental building.
422 # We need to store a listing of the classifiers each DOI is in
423 my $clids = [];
424 #rint STDERR "==3. Recording reverse lookup for $OID.$next_subOID==\n";
425 if(defined($oid_to_clids->{$OID . "." . $next_subOID})) {
426 #rint STDERR "Found existing array!\n";
427 $clids = $oid_to_clids->{$OID . "." . $next_subOID};
428 }
429 #rint STDERR "Appended $OID to \"" . join(";", @{$clids}) . "\"\n";
430 push(@{$clids}, $OID);
431 $oid_to_clids->{$OID . "." . $next_subOID} = $clids;
432 #rint STDERR "Result: \"" . join(";", @{$clids}) . "\"\n";
433
434 &print_classify_info ($infodb_handle, $tempinfo, "$OID.$next_subOID",
435 $remove_empty_classifications);
436 $next_subOID++;
437 }
438 }
439
440 $classify_infodb{"contains"} = [ $contains_text ];
441 $classify_infodb{"mdtype"} = [ $classifyinfo->{'mdtype'} ]
442 if defined $classifyinfo->{'mdtype'};
443 $classify_infodb{"mdoffset"} = [ $mdoffset_text ]
444 if ($mdoffset_text !~ m/^;+$/);
445
446 &dbutil::write_infodb_entry($infodb_handle, $OID, \%classify_infodb);
447}
448
449
450sub check_contents {
451 my ($classifyinfo,$remove_empty_classifications) = @_;
452 $remove_empty_classifications = 0 unless ($remove_empty_classifications);
453 my $num_leaf_docs = 0;
454 my $sub_num_leaf_docs = 0;
455
456 return $classifyinfo->{'numleafdocs'} if (defined $classifyinfo->{'numleafdocs'});
457
458 foreach $content (@{$classifyinfo->{'contains'}}) {
459 if (defined $content->{'OID'}) {
460 # found a book
461 $num_leaf_docs ++;
462 } elsif (($sub_num_leaf_docs = &check_contents ($content,$remove_empty_classifications)) > 0) {
463 # there's a book somewhere below
464 $num_leaf_docs += $sub_num_leaf_docs;
465 } else {
466 if ($remove_empty_classifications){
467 # section contains no books so we want to remove
468 # it from its parents contents
469 $content = undef;
470 }
471 }
472 }
473
474 $classifyinfo->{'numleafdocs'} = $num_leaf_docs;
475 return $num_leaf_docs;
476}
477
4781;
Note: See TracBrowser for help on using the repository browser.