source: trunk/gsdl/perllib/classify/GenericList.pm@ 12892

Last change on this file since 12892 was 12892, checked in by mdewsnip, 18 years ago

Changed the ".use_hlist" entries to ".list_type", for simplicity.

  • Property svn:keywords set to Author Date Id Revision
File size: 15.9 KB
Line 
1###########################################################################
2#
3# GenericList.pm -- A general and flexible list classifier with most of
4# the abilities of AZCompactList, and better Unicode,
5# metadata and sorting capabilities.
6#
7# A component of the Greenstone digital library software
8# from the New Zealand Digital Library Project at the
9# University of Waikato, New Zealand.
10#
11# Author: Michael Dewsnip, NZDL Project, University of Waikato, NZ
12#
13# Copyright (C) 2005 New Zealand Digital Library Project
14#
15# This program is free software; you can redistribute it and/or modify
16# it under the terms of the GNU General Public License as published by
17# the Free Software Foundation; either version 2 of the License, or
18# (at your option) any later version.
19#
20# This program is distributed in the hope that it will be useful,
21# but WITHOUT ANY WARRANTY; without even the implied warranty of
22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23# GNU General Public License for more details.
24#
25# You should have received a copy of the GNU General Public License
26# along with this program; if not, write to the Free Software
27# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28#
29# TO DO: Remove punctuation from metadata values before sorting.
30#
31###########################################################################
32
33package GenericList;
34
35
36use BasClas;
37
38use strict;
39
40
41sub BEGIN {
42 @GenericList::ISA = ('BasClas');
43}
44
45
46my $arguments =
47 [ { 'name' => "metadata",
48 'desc' => "{GenericList.metadata}",
49 'type' => "metadata",
50 'reqd' => "yes" },
51
52 # The interesting options
53 { 'name' => "always_bookshelf_last_level",
54 'desc' => "{GenericList.always_bookshelf_last_level}",
55 'type' => "flag" },
56 { 'name' => "classify_sections",
57 'desc' => "{GenericList.classify_sections}",
58 'type' => "flag" },
59 { 'name' => "partition_type_within_level",
60 'desc' => "{GenericList.partition_type_within_level}",
61 'type' => "string",
62 'deft' => "none" },
63 { 'name' => "partition_size_within_level",
64 'desc' => "{GenericList.partition_size_within_level}",
65 'type' => "string" },
66 { 'name' => "sort_leaf_nodes_using",
67 'desc' => "{GenericList.sort_leaf_nodes_using}",
68 'type' => "metadata",
69 'deft' => "Title" },
70 { 'name' => "use_hlist_for",
71 'desc' => "{GenericList.use_hlist_for}",
72 'type' => "string" } ];
73
74my $options = { 'name' => "GenericList",
75 'desc' => "{GenericList.desc}",
76 'abstract' => "no",
77 'inherits' => "Yes",
78 'args' => $arguments };
79
80
81sub new
82{
83 my ($class) = shift(@_);
84 my ($classifierslist, $inputargs, $hashArgOptLists) = @_;
85 push(@$classifierslist, $class);
86
87 if (defined $arguments) { push(@{$hashArgOptLists->{"ArgList"}}, @{$arguments}); }
88 if (defined $options) { push(@{$hashArgOptLists->{"OptList"}}, $options); }
89
90 my $self = new BasClas($classifierslist, $inputargs, $hashArgOptLists);
91 $self->{'OIDs'} = [];
92
93 if ($self->{'info_only'}) {
94 # don't worry about any options etc
95 return bless $self, $class;
96 }
97
98 my $metadata = $self->{'metadata'};
99 my $buttonname = $self->{'buttonname'};
100 my $partition_type_within_level = $self->{'partition_type_within_level'};
101 my $partition_size_within_level = $self->{'partition_size_within_level'};
102 my $sort_leaf_nodes_using = $self->{'sort_leaf_nodes_using'};
103
104 # The metadata elements to use (required)
105 # print STDERR "Creating new GenericList for $metadata...\n";
106 if (!$metadata) {
107 die "Error: No metadata fields specified for GenericList.\n";
108 }
109 my @metadata_groups = split(/\//, $metadata);
110 $self->{'metadata_groups'} = \@metadata_groups;
111
112 # The classifier button name
113 if (!$buttonname) {
114 # Default: the first metadata element specified
115 my $firstmetagroupfirstelem = (split(/\;/, $metadata_groups[0]))[0];
116 $buttonname = $self->generate_title_from_metadata($firstmetagroupfirstelem);
117 }
118 $self->{'title'} = $buttonname;
119
120 # Whether to group single items into a bookshelf (must be true for all metadata fields except the last)
121 foreach my $metadata_group (@metadata_groups) {
122 $self->{$metadata_group . ".always_bookshelf"} = "t";
123 }
124 if (!$self->{'always_bookshelf_last_level'}) {
125 # Default: leave leafnodes ungrouped (equivalent to AZCompactList -mingroup 2)
126 my $last_metadata_group = $metadata_groups[$#metadata_groups];
127 $self->{$last_metadata_group . ".always_bookshelf"} = "f";
128 }
129
130 # Whether to use an hlist or a vlist for each level in the hierarchy (default: vlist)
131 foreach my $metadata_group (@metadata_groups) {
132 $self->{$metadata_group . ".list_type"} = "VList";
133 }
134 foreach my $metadata_group (split(/\,/, $self->{'use_hlist_for'})) {
135 $self->{$metadata_group . ".list_type"} = "HList";
136 }
137
138 # How the items are grouped into partitions
139 if (!$partition_type_within_level) {
140 # Default: none
141 $partition_type_within_level = "none";
142 }
143 $self->{"partition_type_within_level"} = $partition_type_within_level;
144
145 # The number of items in each partition
146 if (!$partition_size_within_level) {
147 # Default: 20
148 foreach my $metadata_group (@metadata_groups) {
149 $self->{$metadata_group . ".partition_size_within_level"} = 20;
150 }
151 }
152 else {
153 my @partition_size_within_levellist = split(/\//, $partition_size_within_level);
154
155 # Assign values based on the partition_size_within_level parameter
156 foreach my $metadata_group (@metadata_groups) {
157 my $partition_size_within_levelelem = shift(@partition_size_within_levellist);
158 if (defined($partition_size_within_levelelem)) {
159 $self->{$metadata_group . ".partition_size_within_level"} = $partition_size_within_levelelem;
160 }
161 else {
162 $self->{$metadata_group . ".partition_size_within_level"} = $self->{$metadata_groups[0] . ".partition_size_within_level"};
163 }
164 }
165 }
166
167 # The metadata elements to use to sort the leaf nodes
168 my @sort_leaf_nodes_usingmetadata_groups = ( "Title" );
169 if ($sort_leaf_nodes_using) {
170 @sort_leaf_nodes_usingmetadata_groups = split(/\|/, $sort_leaf_nodes_using);
171 }
172 $self->{'sort_leaf_nodes_usingmetadata_groups'} = \@sort_leaf_nodes_usingmetadata_groups;
173
174 return bless $self, $class;
175}
176
177
178sub init
179{
180 # Nothing to do...
181}
182
183
184sub classify
185{
186 my $self = shift(@_);
187 my $doc_obj = shift(@_);
188
189 my $doc_OID = $doc_obj->get_OID();
190
191 if ($self->{'classify_sections'}) {
192 my $section = $doc_obj->get_next_section($doc_obj->get_top_section());
193 while (defined $section) {
194 $self->classify_section($doc_obj, $doc_OID . ".$section", $section);
195 $section = $doc_obj->get_next_section($section);
196 }
197 }
198 else {
199 $self->classify_section($doc_obj, $doc_OID, $doc_obj->get_top_section());
200 }
201}
202
203
204sub classify_section
205{
206 my $self = shift(@_);
207 my $doc_obj = shift(@_);
208 my $doc_OID = shift(@_);
209 my $section = shift(@_);
210
211 my @metadata_groups = @{$self->{'metadata_groups'}};
212
213 # Only classify the document if it has a value for one of the metadata elements in the first group
214 foreach my $firstmetagroupelem (split(/\;/, $metadata_groups[0])) {
215 my $firstmetagroupelemvalue = $doc_obj->get_metadata_element($section, $firstmetagroupelem);
216 if (defined($firstmetagroupelemvalue) && $firstmetagroupelemvalue ne "") {
217 push(@{$self->{'OIDs'}}, $doc_OID);
218
219 # Create a hash for the metadata values of each metadata element we're interested in
220 my %metagroupsdone = ();
221 foreach my $metadata_group (@metadata_groups, @{$self->{'sort_leaf_nodes_usingmetadata_groups'}}) {
222 # Take care not to do a metadata group more than once
223 unless ($metagroupsdone{$metadata_group}) {
224 foreach my $metaelem (split(/\;/, $metadata_group)) {
225 my @metavalues = @{$doc_obj->get_metadata($section, $metaelem)};
226 foreach my $metavalue (@metavalues) {
227 # Strip leading and trailing whitespace
228 $metavalue =~ s/^\s*//;
229 $metavalue =~ s/\s*$//;
230 push(@{$self->{$metadata_group . ".list"}->{$doc_OID}}, $metavalue);
231 }
232 last if (@metavalues > 0);
233 }
234
235 $metagroupsdone{$metadata_group} = 1;
236 }
237 }
238
239 last;
240 }
241 }
242}
243
244
245sub get_classify_info
246{
247 my $self = shift(@_);
248
249 # The metadata elements to classify by
250 my @metadata_groups = @{$self->{'metadata_groups'}};
251 my $first_metadata_group = $metadata_groups[0];
252
253 # The OID values of the documents to include in the classification
254 my @OIDs = @{$self->{'OIDs'}};
255
256 # The root node of the classification hierarchy
257 my $childtype = $self->{$first_metadata_group . ".list_type"};
258 my %classifyinfo = ( 'thistype' => "Invisible",
259 'childtype' => $childtype,
260 'Title' => $self->{'title'},
261 'contains' => [] );
262
263 # Recursively create the classification hierarchy, one level for each metadata element
264 &add_az_list($self, \@metadata_groups, \@OIDs, \%classifyinfo);
265 return \%classifyinfo;
266}
267
268
269sub add_az_list
270{
271 my $self = shift(@_);
272 my @metadata_groups = @{shift(@_)};
273 my @OIDs = @{shift(@_)};
274 my $classifyinfo = shift(@_);
275 # print STDERR "\nAdding AZ list for " . $classifyinfo->{'Title'} . "\n";
276
277 my $metadata_group = $metadata_groups[0];
278 # print STDERR "Processing metadata group: " . $metadata_group . "\n";
279 # print STDERR "Number of OID values: " . @OIDs . "\n";
280
281 my %OIDtometavaluehash = %{$self->{$metadata_group . ".list"}};
282
283 # Create a mapping from metadata value to OID
284 my %metavaluetoOIDhash = ();
285 foreach my $OID (@OIDs) {
286 if ($OIDtometavaluehash{$OID}) {
287 my @metavalues = @{$OIDtometavaluehash{$OID}};
288 foreach my $metavalue (@metavalues) {
289 push(@{$metavaluetoOIDhash{$metavalue}}, $OID);
290 }
291 }
292 }
293 # print STDERR "Number of distinct values: " . scalar(keys %metavaluetoOIDhash) . "\n";
294
295 # Partition the values (if necessary)
296 my $partition_type_within_level = $self->{"partition_type_within_level"};
297 if ($partition_type_within_level =~ /^per_letter$/i) {
298 # Generate one hlist for each letter
299 my @sortedmetavalues = sort(keys %metavaluetoOIDhash);
300 my %metavaluetoOIDsubhash = ();
301
302 my $lastpartition = &unicode::substr($sortedmetavalues[0], 0, 1);
303 foreach my $metavalue (@sortedmetavalues) {
304 my $metavaluepartition = &unicode::substr($metavalue, 0, 1);
305
306 # Is this the start of a new partition?
307 if ($metavaluepartition ne $lastpartition) {
308 &add_hlist_partition($self, \@metadata_groups, $classifyinfo, $lastpartition, \%metavaluetoOIDsubhash);
309 %metavaluetoOIDsubhash = ();
310 $lastpartition = $metavaluepartition;
311 }
312
313 $metavaluetoOIDsubhash{$metavalue} = $metavaluetoOIDhash{$metavalue};
314 }
315
316 # Don't forget to add the last partition
317 &add_hlist_partition($self, \@metadata_groups, $classifyinfo, $lastpartition, \%metavaluetoOIDsubhash);
318
319 # The partitions are stored in an HList
320 $classifyinfo->{'childtype'} = "HList";
321 }
322
323 else {
324 # Generate hlists of a certain size
325 my $partition_size_within_level = $self->{$metadata_group . ".partition_size_within_level"};
326 if ($partition_type_within_level =~ /^constant_size$/i && scalar(keys %metavaluetoOIDhash) > $partition_size_within_level) {
327 my @sortedmetavalues = sort(keys %metavaluetoOIDhash);
328 my $itemsdone = 0;
329 my %metavaluetoOIDsubhash = ();
330 my $lastpartitionend = "";
331 my $partitionstart;
332 foreach my $metavalue (@sortedmetavalues) {
333 $metavaluetoOIDsubhash{$metavalue} = $metavaluetoOIDhash{$metavalue};
334 $itemsdone++;
335 my $itemsinpartition = scalar(keys %metavaluetoOIDsubhash);
336
337 # Is this the start of a new partition?
338 if ($itemsinpartition == 1) {
339 $partitionstart = &generate_partition_start($metavalue, $lastpartitionend);
340 }
341
342 # Is this the end of the partition?
343 if ($itemsinpartition == $partition_size_within_level || $itemsdone == @sortedmetavalues) {
344 my $partitionend = &generate_partition_end($metavalue, $partitionstart);
345 my $partitionname = $partitionstart;
346 if ($partitionend ne $partitionstart) {
347 $partitionname = $partitionname . "-" . $partitionend;
348 }
349
350 &add_hlist_partition($self, \@metadata_groups, $classifyinfo, $partitionname, \%metavaluetoOIDsubhash);
351 %metavaluetoOIDsubhash = ();
352 $lastpartitionend = $partitionend;
353 }
354 }
355
356 # The partitions are stored in an HList
357 $classifyinfo->{'childtype'} = "HList";
358 }
359
360 # Otherwise just add all the values to a VList
361 else {
362 &add_vlist($self, \@metadata_groups, $classifyinfo, \%metavaluetoOIDhash);
363 }
364 }
365}
366
367
368sub unicode_length
369{
370 my $utf8string = shift(@_);
371
372 my @unicodestring = @{&unicode::utf82unicode($utf8string)};
373 return scalar(@unicodestring);
374}
375
376
377sub generate_partition_start
378{
379 my $metavalue = shift(@_);
380 my $lastpartitionend = shift(@_);
381
382 my $partitionstart = &unicode::substr($metavalue, 0, 1);
383 if ($partitionstart le $lastpartitionend) {
384 $partitionstart = &unicode::substr($metavalue, 0, 2);
385 # Give up after three characters
386 if ($partitionstart le $lastpartitionend) {
387 $partitionstart = &unicode::substr($metavalue, 0, 3);
388 }
389 }
390
391 return $partitionstart;
392}
393
394
395sub generate_partition_end
396{
397 my $metavalue = shift(@_);
398 my $partitionstart = shift(@_);
399
400 my $partitionend = &unicode::substr($metavalue, 0, &unicode_length($partitionstart));
401 if ($partitionend gt $partitionstart) {
402 $partitionend = &unicode::substr($metavalue, 0, 1);
403 if ($partitionend le $partitionstart) {
404 $partitionend = &unicode::substr($metavalue, 0, 2);
405 # Give up after three characters
406 if ($partitionend le $partitionstart) {
407 $partitionend = &unicode::substr($metavalue, 0, 3);
408 }
409 }
410 }
411
412 return $partitionend;
413}
414
415
416sub add_hlist_partition
417{
418 my $self = shift(@_);
419 my @metadata_groups = @{shift(@_)};
420 my $classifyinfo = shift(@_);
421 my $partitionname = shift(@_);
422 my %metavaluetoOIDhash = %{shift(@_)};
423
424 # Create an hlist partition
425 my %subclassifyinfo = ( 'Title' => $partitionname,
426 'childtype' => "VList",
427 'contains' => [] );
428
429 # Add the children to the hlist partition
430 &add_vlist($self, \@metadata_groups, \%subclassifyinfo, \%metavaluetoOIDhash);
431 push(@{$classifyinfo->{'contains'}}, \%subclassifyinfo);
432}
433
434
435sub add_vlist
436{
437 my $self = shift(@_);
438 my @metadata_groups = @{shift(@_)};
439 my $classifyinfo = shift(@_);
440 my %metavaluetoOIDhash = %{shift(@_)};
441
442 my $metadata_group = shift(@metadata_groups);
443
444 # Create an entry in the vlist for each value
445 foreach my $metavalue (sort(keys %metavaluetoOIDhash)) {
446 my @OIDs = @{$metavaluetoOIDhash{$metavalue}};
447
448 # If there is only one item and 'always_bookshelf' is false, add the item to the list
449 if (@OIDs == 1 && $self->{$metadata_group . ".always_bookshelf"} eq "f") {
450 push(@{$classifyinfo->{'contains'}}, { 'OID' => $OIDs[0] });
451 }
452
453 # Otherwise create a sublist (bookshelf) for the metadata value
454 else {
455 my %subclassifyinfo = ( 'Title' => $metavalue,
456 'childtype' => "VList",
457 'contains' => [] );
458
459 # If there are metadata elements remaining, recursively apply the process
460 if (@metadata_groups > 0) {
461 my $next_metadata_group = $metadata_groups[0];
462 my $childtype = $self->{$next_metadata_group . ".list_type"};
463 $subclassifyinfo{'childtype'} = $childtype;
464 &add_az_list($self, \@metadata_groups, \@OIDs, \%subclassifyinfo);
465 }
466 # Otherwise just add the documents as children of this list
467 else {
468 # Sort the leaf nodes by the metadata elements specified with -sort_leaf_nodes_using
469 my @sort_leaf_nodes_usingmetadata_groups = @{$self->{'sort_leaf_nodes_usingmetadata_groups'}};
470 foreach my $sort_leaf_nodes_usingmetaelem (reverse @sort_leaf_nodes_usingmetadata_groups) {
471 my %OIDtometavaluehash = %{$self->{$sort_leaf_nodes_usingmetaelem . ".list"}};
472 # Force a stable sort (Perl 5.6's sort isn't stable)
473 # !! The [0] bits aren't ideal (multiple metadata values) !!
474 @OIDs = @OIDs[ sort { $OIDtometavaluehash{$OIDs[$a]}[0] cmp $OIDtometavaluehash{$OIDs[$b]}[0] || $a <=> $b; } 0..$#OIDs ];
475 }
476
477 foreach my $OID (@OIDs) {
478 push(@{$subclassifyinfo{'contains'}}, { 'OID' => $OID });
479 }
480 }
481
482 # Add the sublist to the list
483 push(@{$classifyinfo->{'contains'}}, \%subclassifyinfo);
484 }
485 }
486}
487
488
4891;
Note: See TracBrowser for help on using the repository browser.