[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 |
|
---|
| 28 | package classify;
|
---|
| 29 |
|
---|
| 30 | require util;
|
---|
| 31 |
|
---|
| 32 |
|
---|
[315] | 33 | $next_classify_num = 1;
|
---|
[214] | 34 |
|
---|
[811] | 35 | sub load_classifiers {
|
---|
[1839] | 36 | my ($classify_list, $build_dir, $outhandle) = @_;
|
---|
[811] | 37 | my @classify_objects = ();
|
---|
| 38 |
|
---|
| 39 | foreach $classifyoption (@$classify_list) {
|
---|
[214] | 40 |
|
---|
[811] | 41 | # get the classifier name
|
---|
| 42 | my $classname = shift @$classifyoption;
|
---|
| 43 | next unless defined $classname;
|
---|
[1839] | 44 |
|
---|
[811] | 45 | # find the classifier
|
---|
| 46 | my $colclassname = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},"perllib/classify",
|
---|
| 47 | "${classname}.pm");
|
---|
| 48 | my $mainclassname = &util::filename_cat($ENV{'GSDLHOME'},"perllib/classify",
|
---|
| 49 | "${classname}.pm");
|
---|
[214] | 50 |
|
---|
[811] | 51 | if (-e $colclassname) { require $colclassname; }
|
---|
| 52 | elsif (-e $mainclassname) { require $mainclassname; }
|
---|
| 53 | else { die "ERROR - couldn't find classifier \"$classname\"\n"; }
|
---|
[214] | 54 |
|
---|
[811] | 55 | # create the classify object
|
---|
| 56 | my ($classobj);
|
---|
[1839] | 57 |
|
---|
| 58 | # backwards compatability hack: if the classifier options are
|
---|
| 59 | # in "x=y" format, convert them to parsearg ("-x y") format.
|
---|
| 60 | my ($opt, $key, $value);
|
---|
| 61 | my @newoptions;
|
---|
| 62 | foreach $opt (@$classifyoption) {
|
---|
| 63 | if ($opt =~ /^(\w+)=(.*)$/) {
|
---|
| 64 | push @newoptions, "-$1", $2;
|
---|
| 65 | } else {
|
---|
| 66 | push @newoptions, $opt;
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
| 69 | push @newoptions, "-builddir", "$build_dir";
|
---|
| 70 | push @newoptions, "-outhandle", "$outhandle";
|
---|
| 71 | push @newoptions, "-verbosity", "2";
|
---|
| 72 |
|
---|
| 73 | map { $_ = "\"$_\""; } @newoptions;
|
---|
| 74 | my $options .= join (",", @newoptions);
|
---|
| 75 |
|
---|
| 76 | # print STDERR "$classname\noptions: $options\n";
|
---|
[1515] | 77 | eval ("\$classobj = new \$classname($options)");
|
---|
[811] | 78 | die "$@" if $@;
|
---|
[1839] | 79 |
|
---|
| 80 | # add this object to the list
|
---|
[811] | 81 | push (@classify_objects, $classobj);
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | return \@classify_objects;
|
---|
[214] | 85 | }
|
---|
| 86 |
|
---|
| 87 | # init_classifiers resets all the classifiers and readys them to process
|
---|
[315] | 88 | # the documents.
|
---|
[214] | 89 | sub init_classifiers {
|
---|
| 90 | my ($classifiers) = @_;
|
---|
| 91 |
|
---|
| 92 | foreach $classobj (@$classifiers) {
|
---|
| 93 | $classobj->init();
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | # classify_doc lets each of the classifiers classify a document
|
---|
| 98 | sub classify_doc {
|
---|
| 99 | my ($classifiers, $doc_obj) = @_;
|
---|
| 100 |
|
---|
| 101 | foreach $classobj (@$classifiers) {
|
---|
| 102 | $classobj->classify($doc_obj);
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | # output_classify_info outputs all the info needed for the classification
|
---|
| 107 | # to the gdbm
|
---|
| 108 | sub output_classify_info {
|
---|
[315] | 109 | my ($classifiers, $handle, $allclassifications) = @_;
|
---|
[214] | 110 | # $handle = "main::STDOUT";
|
---|
| 111 |
|
---|
[315] | 112 | # create a classification containing all the info
|
---|
| 113 | my $classifyinfo = {'classifyOID'=>'browse',
|
---|
| 114 | 'contains'=>[]};
|
---|
| 115 |
|
---|
| 116 | # get each of the classifications
|
---|
[214] | 117 | foreach $classobj (@$classifiers) {
|
---|
[315] | 118 | my $tempinfo = $classobj->get_classify_info();
|
---|
| 119 | $tempinfo->{'classifyOID'} = "CL$next_classify_num";
|
---|
| 120 | $next_classify_num++;
|
---|
| 121 | push (@{$classifyinfo->{'contains'}}, $tempinfo);
|
---|
[214] | 122 | }
|
---|
| 123 |
|
---|
[315] | 124 | &print_classify_info ($handle, $classifyinfo, "", $allclassifications);
|
---|
[214] | 125 | }
|
---|
| 126 |
|
---|
[315] | 127 | sub print_classify_info {
|
---|
| 128 | my ($handle, $classifyinfo, $OID, $allclassifications) = @_;
|
---|
[831] | 129 |
|
---|
[315] | 130 | $OID =~ s/^\.+//; # just for good luck
|
---|
[214] | 131 |
|
---|
[315] | 132 | # book information is printed elsewhere
|
---|
| 133 | return if (defined ($classifyinfo->{'OID'}));
|
---|
| 134 |
|
---|
| 135 | # don't want empty classifications
|
---|
[637] | 136 | if ($allclassifications || &check_contents ($classifyinfo) > 0) {
|
---|
[315] | 137 |
|
---|
| 138 | $OID = $classifyinfo->{'classifyOID'} if defined ($classifyinfo->{'classifyOID'});
|
---|
| 139 |
|
---|
| 140 | my $outputtext = "[$OID]\n";
|
---|
| 141 | $outputtext .= "<doctype>classify\n";
|
---|
| 142 | $outputtext .= "<hastxt>0\n";
|
---|
[652] | 143 | $outputtext .= "<childtype>$classifyinfo->{'childtype'}\n"
|
---|
| 144 | if defined $classifyinfo->{'childtype'};
|
---|
| 145 | $outputtext .= "<Title>$classifyinfo->{'Title'}\n"
|
---|
| 146 | if defined $classifyinfo->{'Title'};
|
---|
| 147 | $outputtext .= "<numleafdocs>$classifyinfo->{'numleafdocs'}\n"
|
---|
| 148 | if defined $classifyinfo->{'numleafdocs'};
|
---|
| 149 | $outputtext .= "<thistype>$classifyinfo->{'thistype'}\n"
|
---|
| 150 | if defined $classifyinfo->{'thistype'};
|
---|
| 151 |
|
---|
[315] | 152 |
|
---|
[831] | 153 | my $contains_text = "<contains>";
|
---|
| 154 | my $mdoffset_text = "<mdoffset>";
|
---|
[315] | 155 |
|
---|
| 156 | my $next_subOID = 1;
|
---|
| 157 | my $first = 1;
|
---|
| 158 | foreach $tempinfo (@{$classifyinfo->{'contains'}}) {
|
---|
| 159 | # empty contents were made undefined by clean_contents()
|
---|
| 160 | next unless defined $tempinfo;
|
---|
| 161 |
|
---|
[831] | 162 | $contains_text .= ";" unless $first;
|
---|
| 163 | $mdoffset_text .= ";" unless $first;
|
---|
[315] | 164 | $first = 0;
|
---|
| 165 |
|
---|
| 166 | if (defined ($tempinfo->{'classifyOID'})) {
|
---|
[831] | 167 | $contains_text .= $tempinfo->{'classifyOID'};
|
---|
[315] | 168 | &print_classify_info ($handle, $tempinfo, $tempinfo->{'classifyOID'},
|
---|
| 169 | $allclassifications);
|
---|
| 170 | } elsif (defined ($tempinfo->{'OID'})) {
|
---|
[831] | 171 | $contains_text .= $tempinfo->{'OID'};
|
---|
| 172 | $mdoffset_text .= $tempinfo->{'offset'}
|
---|
| 173 | if (defined ($tempinfo->{'offset'}))
|
---|
[315] | 174 | # note: we don't want to print the contents of the books
|
---|
| 175 | } else {
|
---|
[831] | 176 | $contains_text .= "\".$next_subOID";
|
---|
[315] | 177 | &print_classify_info ($handle, $tempinfo, "$OID.$next_subOID",
|
---|
| 178 | $allclassifications);
|
---|
| 179 | $next_subOID++;
|
---|
| 180 | }
|
---|
| 181 | }
|
---|
[831] | 182 |
|
---|
| 183 | $outputtext .= "$contains_text\n";
|
---|
| 184 | $outputtext .= "<mdtype>$classifyinfo->{'mdtype'}\n"
|
---|
| 185 | if defined $classifyinfo->{'mdtype'};
|
---|
| 186 | $outputtext .= "$mdoffset_text\n"
|
---|
| 187 | if ($mdoffset_text !~ m/^<mdoffset>;+$/);
|
---|
| 188 |
|
---|
[315] | 189 | $outputtext .= '-' x 70 . "\n";
|
---|
| 190 |
|
---|
| 191 | print $handle $outputtext;
|
---|
| 192 | }
|
---|
| 193 | }
|
---|
| 194 |
|
---|
[637] | 195 | sub check_contents {
|
---|
[315] | 196 | my ($classifyinfo) = @_;
|
---|
[637] | 197 | my $num_leaf_docs = 0;
|
---|
| 198 | my $sub_num_leaf_docs = 0;
|
---|
[315] | 199 |
|
---|
[637] | 200 | return $classifyinfo->{'numleafdocs'} if (defined $classifyinfo->{'numleafdocs'});
|
---|
| 201 |
|
---|
[315] | 202 | foreach $content (@{$classifyinfo->{'contains'}}) {
|
---|
| 203 | if (defined $content->{'OID'}) {
|
---|
| 204 | # found a book
|
---|
[637] | 205 | $num_leaf_docs ++;
|
---|
| 206 | } elsif (($sub_num_leaf_docs = &check_contents ($content)) > 0) {
|
---|
[315] | 207 | # there's a book somewhere below
|
---|
[637] | 208 | $num_leaf_docs += $sub_num_leaf_docs;
|
---|
[315] | 209 | } else {
|
---|
| 210 | # section contains no books so we want to remove
|
---|
| 211 | # it from its parents contents
|
---|
| 212 | $content = undef;
|
---|
| 213 | }
|
---|
| 214 | }
|
---|
[637] | 215 |
|
---|
| 216 | $classifyinfo->{'numleafdocs'} = $num_leaf_docs;
|
---|
| 217 | return $num_leaf_docs;
|
---|
[315] | 218 | }
|
---|
| 219 |
|
---|
[214] | 220 | 1;
|
---|