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

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

Removed the "oid_to_clids" crap that was allegedly added for incremental building, as it is never used. As well as wasting time during the classifier output, this can use up a substantial amount of memory if the collection is large or the classifiers are done at section level. With dynamic classifiers a key component of true incremental building there will be no requirement for this information tobe stored.

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