source: main/trunk/greenstone2/perllib/classify.pm@ 27334

Last change on this file since 27334 was 27334, checked in by kjdon, 11 years ago

2 bugfixes that broke the building process.

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