source: gs2-extensions/parallel-building/trunk/src/perllib/classify.pm@ 24626

Last change on this file since 24626 was 24626, checked in by jmt12, 13 years ago

An (almost) complete copy of the perllib directory from a (circa SEP2011) head checkout from Greenstone 2 trunk - in order to try and make merging in this extension a little easier later on (as there have been some major changes to buildcol.pl commited in the main trunk but not in the x64 branch)

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