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

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

(Adding new DB support) Added $infodb_type as first argument to all the dbutil functions.

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