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

Last change on this file since 28706 was 27675, checked in by ak19, 11 years ago

Tidying up my previous 2 commits on this file.

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