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

Last change on this file since 13741 was 13741, checked in by mdewsnip, 17 years ago

Added a second item to the "TO DO" list in this file.

  • Property svn:keywords set to Author Date Id Revision
File size: 18.4 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# - Add an AZCompactList-style hlist option?
31#
32###########################################################################
33
34package GenericList;
35
36
37use BasClas;
38
39use strict;
40
41
42sub BEGIN {
43 @GenericList::ISA = ('BasClas');
44}
45
46
47my $arguments =
48 [ { 'name' => "metadata",
49 'desc' => "{GenericList.metadata}",
50 'type' => "metadata",
51 'reqd' => "yes" },
52
53 # The interesting options
54 { 'name' => "always_bookshelf_last_level",
55 'desc' => "{GenericList.always_bookshelf_last_level}",
56 'type' => "flag" },
57 { 'name' => "classify_sections",
58 'desc' => "{GenericList.classify_sections}",
59 'type' => "flag" },
60 { 'name' => "partition_type_within_level",
61 'desc' => "{GenericList.partition_type_within_level}",
62 'type' => "string",
63 'deft' => "none" },
64 { 'name' => "partition_size_within_level",
65 'desc' => "{GenericList.partition_size_within_level}",
66 'type' => "string" },
67 { 'name' => "sort_leaf_nodes_using",
68 'desc' => "{GenericList.sort_leaf_nodes_using}",
69 'type' => "metadata",
70 'deft' => "Title" },
71 { 'name' => "sort_using_unicode_collation",
72 'desc' => "{GenericList.sort_using_unicode_collation}",
73 'type' => "flag" },
74 { 'name' => "use_hlist_for",
75 'desc' => "{GenericList.use_hlist_for}",
76 'type' => "string" } ];
77
78my $options = { 'name' => "GenericList",
79 'desc' => "{GenericList.desc}",
80 'abstract' => "no",
81 'inherits' => "Yes",
82 'args' => $arguments };
83
84
85sub new
86{
87 my ($class) = shift(@_);
88 my ($classifierslist, $inputargs, $hashArgOptLists) = @_;
89 push(@$classifierslist, $class);
90
91 if (defined $arguments) { push(@{$hashArgOptLists->{"ArgList"}}, @{$arguments}); }
92 if (defined $options) { push(@{$hashArgOptLists->{"OptList"}}, $options); }
93
94 my $self = new BasClas($classifierslist, $inputargs, $hashArgOptLists);
95
96 if ($self->{'info_only'}) {
97 # don't worry about any options etc
98 return bless $self, $class;
99 }
100
101 # The metadata elements to use (required)
102 if (!$self->{'metadata'}) {
103 die "Error: No metadata fields specified for GenericList.\n";
104 }
105 my @metadata_groups = split(/\//, $self->{'metadata'});
106 $self->{'metadata_groups'} = \@metadata_groups;
107
108 # The classifier button name (default: the first metadata element specified)
109 if (!$self->{'buttonname'}) {
110 my $first_metadata_group = $metadata_groups[0];
111 my $first_metadata_element = (split(/\;/, $first_metadata_group))[0];
112 $self->{'buttonname'} = $self->generate_title_from_metadata($first_metadata_element);
113 }
114
115 # Whether to group single items into a bookshelf (must be true for all metadata fields except the last)
116 foreach my $metadata_group (@metadata_groups) {
117 $self->{$metadata_group . ".always_bookshelf"} = "t";
118 }
119 if (!$self->{'always_bookshelf_last_level'}) {
120 # Default: leave leaf nodes ungrouped (equivalent to AZCompactList -mingroup 2)
121 my $last_metadata_group = $metadata_groups[$#metadata_groups];
122 $self->{$last_metadata_group . ".always_bookshelf"} = "f";
123 }
124
125 # Whether to use an hlist or a vlist for each level in the hierarchy (default: vlist)
126 foreach my $metadata_group (@metadata_groups) {
127 $self->{$metadata_group . ".list_type"} = "VList";
128 }
129 foreach my $metadata_group (split(/\,/, $self->{'use_hlist_for'})) {
130 $self->{$metadata_group . ".list_type"} = "HList";
131 }
132
133 # How the items are grouped into partitions (default: no partition)
134 if (!$self->{"partition_type_within_level"}) {
135 $self->{"partition_type_within_level"} = "none";
136 }
137
138 # The number of items in each partition
139 if (!$self->{'partition_size_within_level'}) {
140 # Default: 20
141 foreach my $metadata_group (@metadata_groups) {
142 $self->{$metadata_group . ".partition_size_within_level"} = 20;
143 }
144 }
145 else {
146 my @partition_size_within_levellist = split(/\//, $self->{'partition_size_within_level'});
147
148 # Assign values based on the partition_size_within_level parameter
149 foreach my $metadata_group (@metadata_groups) {
150 my $partition_size_within_levelelem = shift(@partition_size_within_levellist);
151 if (defined($partition_size_within_levelelem)) {
152 $self->{$metadata_group . ".partition_size_within_level"} = $partition_size_within_levelelem;
153 }
154 else {
155 $self->{$metadata_group . ".partition_size_within_level"} = $self->{$metadata_groups[0] . ".partition_size_within_level"};
156 }
157 }
158 }
159
160 # The metadata elements to use to sort the leaf nodes (default: Title)
161 my @sort_leaf_nodes_using_metadata_groups = ( "Title" );
162 if ($self->{'sort_leaf_nodes_using'}) {
163 @sort_leaf_nodes_using_metadata_groups = split(/\|/, $self->{'sort_leaf_nodes_using'});
164 }
165 $self->{'sort_leaf_nodes_using_metadata_groups'} = \@sort_leaf_nodes_using_metadata_groups;
166
167 # Create an instance of the Unicode::Collate object if better Unicode sorting is desired
168 if ($self->{'sort_using_unicode_collation'}) {
169 require Unicode::Collate;
170 $self->{'unicode_collator'} = Unicode::Collate->new();
171 }
172
173 # An empty array for the document OIDs
174 $self->{'OIDs'} = [];
175
176 return bless $self, $class;
177}
178
179
180sub init
181{
182 # Nothing to do...
183}
184
185
186# Called for each document in the collection
187sub classify
188{
189 my $self = shift(@_);
190 my $doc_obj = shift(@_);
191
192 # If "-classify_sections" is set, classify every section of the document
193 if ($self->{'classify_sections'}) {
194 my $section = $doc_obj->get_next_section($doc_obj->get_top_section());
195 while (defined $section) {
196 $self->classify_section($doc_obj, $doc_obj->get_OID() . ".$section", $section);
197 $section = $doc_obj->get_next_section($section);
198 }
199 }
200 # Otherwise just classify the top document section
201 else {
202 $self->classify_section($doc_obj, $doc_obj->get_OID(), $doc_obj->get_top_section());
203 }
204}
205
206
207sub classify_section
208{
209 my $self = shift(@_);
210 my $doc_obj = shift(@_);
211 my $section_OID = shift(@_);
212 my $section = shift(@_);
213
214 my @metadata_groups = @{$self->{'metadata_groups'}};
215
216 # Only classify the section if it has a value for one of the metadata elements in the first group
217 my $classify_section = 0;
218 my $first_metadata_group = $metadata_groups[0];
219 foreach my $first_metadata_group_element (split(/\;/, $first_metadata_group)) {
220 my $first_metadata_group_element_value = $doc_obj->get_metadata_element($section, $first_metadata_group_element);
221 if (defined($first_metadata_group_element_value) && $first_metadata_group_element_value ne "") {
222 # This section must be included in the classifier
223 $classify_section = 1;
224 last;
225 }
226 }
227
228 # We're not classifying this section because it doesn't have the required metadata
229 return if (!$classify_section);
230
231 # Otherwise, include this section in the classifier
232 push(@{$self->{'OIDs'}}, $section_OID);
233
234 # Create a hash for the metadata values of each metadata element we're interested in
235 my %metadata_groups_done = ();
236 foreach my $metadata_group (@metadata_groups, @{$self->{'sort_leaf_nodes_using_metadata_groups'}}) {
237 # Take care not to do a metadata group more than once
238 unless ($metadata_groups_done{$metadata_group}) {
239 foreach my $metadata_element (split(/\;/, $metadata_group)) {
240 my @metadata_values = @{$doc_obj->get_metadata($section, $metadata_element)};
241 foreach my $metadata_value (@metadata_values) {
242 # Strip leading and trailing whitespace
243 $metadata_value =~ s/^\s*//;
244 $metadata_value =~ s/\s*$//;
245
246 # Convert the metadata value from a UTF-8 string to a Unicode string
247 # This means that length() and substr() work properly
248 # We need to be careful to convert classifier node title values back to UTF-8, however
249 my $metadata_value_unicode_string = &convert_utf8_string_to_unicode_string($metadata_value);
250
251 # Add the metadata value into the list for this combination of metadata group and section
252 push(@{$self->{$metadata_group . ".list"}->{$section_OID}}, $metadata_value_unicode_string);
253 }
254 last if (@metadata_values > 0);
255 }
256
257 $metadata_groups_done{$metadata_group} = 1;
258 }
259 }
260}
261
262
263sub get_classify_info
264{
265 my $self = shift(@_);
266
267 # The metadata groups to classify by
268 my @metadata_groups = @{$self->{'metadata_groups'}};
269 my $first_metadata_group = $metadata_groups[0];
270
271 # The OID values of the documents to include in the classifier
272 my @OIDs = @{$self->{'OIDs'}};
273
274 # Create the root node of the classification hierarchy
275 my %classifier_node = ( 'thistype' => "Invisible",
276 'childtype' => $self->{$first_metadata_group . ".list_type"},
277 'Title' => $self->{'buttonname'},
278 'contains' => [],
279 'mdtype' => $first_metadata_group );
280
281 # Recursively create the classification hierarchy, one level for each metadata group
282 &add_level($self, \@metadata_groups, \@OIDs, \%classifier_node);
283 return \%classifier_node;
284}
285
286
287sub add_level
288{
289 my $self = shift(@_);
290 my @metadata_groups = @{shift(@_)};
291 my @OIDs = @{shift(@_)};
292 my $classifier_node = shift(@_);
293 # print STDERR "\nAdding AZ list for " . $classifier_node->{'Title'} . "\n";
294
295 my $metadata_group = $metadata_groups[0];
296 # print STDERR "Processing metadata group: " . $metadata_group . "\n";
297 # print STDERR "Number of OID values: " . @OIDs . "\n";
298
299 if (!defined($self->{$metadata_group . ".list"})) {
300 print STDERR "Warning: No metadata values assigned to $metadata_group.\n";
301 return;
302 }
303 my %OIDtometavalueshash = %{$self->{$metadata_group . ".list"}};
304
305 # Create a mapping from metadata value to OID
306 my %metavaluetoOIDhash = ();
307 foreach my $OID (@OIDs) {
308 if ($OIDtometavalueshash{$OID}) {
309 my @metavalues = @{$OIDtometavalueshash{$OID}};
310 foreach my $metavalue (@metavalues) {
311 push(@{$metavaluetoOIDhash{$metavalue}}, $OID);
312 }
313 }
314 }
315 # print STDERR "Number of distinct values: " . scalar(keys %metavaluetoOIDhash) . "\n";
316
317 # Partition the values (if necessary)
318 my $partition_type_within_level = $self->{"partition_type_within_level"};
319 if ($partition_type_within_level =~ /^per_letter$/i) {
320 # Generate one hlist for each letter
321 my @sortedmetavalues = &sort_metadata_values_array($self, keys(%metavaluetoOIDhash));
322 my %metavaluetoOIDsubhash = ();
323
324 my $lastpartition = substr($sortedmetavalues[0], 0, 1);
325 foreach my $metavalue (@sortedmetavalues) {
326 my $metavaluepartition = substr($metavalue, 0, 1);
327
328 # Is this the start of a new partition?
329 if ($metavaluepartition ne $lastpartition) {
330 &add_hlist_partition($self, \@metadata_groups, $classifier_node, $lastpartition, \%metavaluetoOIDsubhash);
331 %metavaluetoOIDsubhash = ();
332 $lastpartition = $metavaluepartition;
333 }
334
335 $metavaluetoOIDsubhash{$metavalue} = $metavaluetoOIDhash{$metavalue};
336 }
337
338 # Don't forget to add the last partition
339 &add_hlist_partition($self, \@metadata_groups, $classifier_node, $lastpartition, \%metavaluetoOIDsubhash);
340
341 # The partitions are stored in an HList
342 $classifier_node->{'childtype'} = "HList";
343 }
344
345 else {
346 # Generate hlists of a certain size
347 my $partition_size_within_level = $self->{$metadata_group . ".partition_size_within_level"};
348 if ($partition_type_within_level =~ /^constant_size$/i && scalar(keys %metavaluetoOIDhash) > $partition_size_within_level) {
349 my @sortedmetavalues = &sort_metadata_values_array($self, keys(%metavaluetoOIDhash));
350 my $itemsdone = 0;
351 my %metavaluetoOIDsubhash = ();
352 my $lastpartitionend = "";
353 my $partitionstart;
354 foreach my $metavalue (@sortedmetavalues) {
355 $metavaluetoOIDsubhash{$metavalue} = $metavaluetoOIDhash{$metavalue};
356 $itemsdone++;
357 my $itemsinpartition = scalar(keys %metavaluetoOIDsubhash);
358
359 # Is this the start of a new partition?
360 if ($itemsinpartition == 1) {
361 $partitionstart = &generate_partition_start($metavalue, $lastpartitionend);
362 }
363
364 # Is this the end of the partition?
365 if ($itemsinpartition == $partition_size_within_level || $itemsdone == @sortedmetavalues) {
366 my $partitionend = &generate_partition_end($metavalue, $partitionstart);
367 my $partitionname = $partitionstart;
368 if ($partitionend ne $partitionstart) {
369 $partitionname = $partitionname . "-" . $partitionend;
370 }
371
372 &add_hlist_partition($self, \@metadata_groups, $classifier_node, $partitionname, \%metavaluetoOIDsubhash);
373 %metavaluetoOIDsubhash = ();
374 $lastpartitionend = $partitionend;
375 }
376 }
377
378 # The partitions are stored in an HList
379 $classifier_node->{'childtype'} = "HList";
380 }
381
382 # Otherwise just add all the values to a VList
383 else {
384 &add_vlist($self, \@metadata_groups, $classifier_node, \%metavaluetoOIDhash);
385 }
386 }
387}
388
389
390sub convert_utf8_string_to_unicode_string
391{
392 my $utf8_string = shift(@_);
393
394 my $unicode_string = "";
395 foreach my $unicode_value (@{&unicode::utf82unicode($utf8_string)}) {
396 $unicode_string .= chr($unicode_value);
397 }
398 return $unicode_string;
399}
400
401
402sub convert_unicode_string_to_utf8_string
403{
404 my $unicode_string = shift(@_);
405
406 my @unicode_array;
407 for (my $i = 0; $i < length($unicode_string); $i++) {
408 push(@unicode_array, ord(substr($unicode_string, $i, 1)));
409 }
410 return &unicode::unicode2utf8(\@unicode_array);
411}
412
413
414sub generate_partition_start
415{
416 my $metavalue = shift(@_);
417 my $lastpartitionend = shift(@_);
418
419 my $partitionstart = substr($metavalue, 0, 1);
420 if ($partitionstart le $lastpartitionend) {
421 $partitionstart = substr($metavalue, 0, 2);
422 # Give up after three characters
423 if ($partitionstart le $lastpartitionend) {
424 $partitionstart = substr($metavalue, 0, 3);
425 }
426 }
427
428 return $partitionstart;
429}
430
431
432sub generate_partition_end
433{
434 my $metavalue = shift(@_);
435 my $partitionstart = shift(@_);
436
437 my $partitionend = substr($metavalue, 0, length($partitionstart));
438 if ($partitionend gt $partitionstart) {
439 $partitionend = substr($metavalue, 0, 1);
440 if ($partitionend le $partitionstart) {
441 $partitionend = substr($metavalue, 0, 2);
442 # Give up after three characters
443 if ($partitionend le $partitionstart) {
444 $partitionend = substr($metavalue, 0, 3);
445 }
446 }
447 }
448
449 return $partitionend;
450}
451
452
453sub add_hlist_partition
454{
455 my $self = shift(@_);
456 my @metadata_groups = @{shift(@_)};
457 my $classifier_node = shift(@_);
458 my $partitionname = shift(@_);
459 my %metavaluetoOIDhash = %{shift(@_)};
460
461 # Create an hlist partition
462 my %child_classifier_node = ( 'Title' => &convert_unicode_string_to_utf8_string($partitionname),
463 'childtype' => "VList",
464 'contains' => [] );
465
466 # Add the children to the hlist partition
467 &add_vlist($self, \@metadata_groups, \%child_classifier_node, \%metavaluetoOIDhash);
468 push(@{$classifier_node->{'contains'}}, \%child_classifier_node);
469}
470
471
472sub add_vlist
473{
474 my $self = shift(@_);
475 my @metadata_groups = @{shift(@_)};
476 my $classifier_node = shift(@_);
477 my %metavaluetoOIDhash = %{shift(@_)};
478
479 my $metadata_group = shift(@metadata_groups);
480 $classifier_node->{'mdtype'} = $metadata_group;
481
482 # Create an entry in the vlist for each value
483 foreach my $metavalue (&sort_metadata_values_array($self, keys(%metavaluetoOIDhash))) {
484 my @OIDs = @{$metavaluetoOIDhash{$metavalue}};
485
486 # If there is only one item and 'always_bookshelf' is false, add the item to the list
487 if (@OIDs == 1 && $self->{$metadata_group . ".always_bookshelf"} eq "f") {
488 my $OID = $OIDs[0];
489
490 # Find the offset of this metadata value
491 my $offset = 0;
492 my %OIDtometavalueshash = %{$self->{$metadata_group . ".list"}};
493 my @metavalues = @{$OIDtometavalueshash{$OID}};
494 for (my $i = 0; $i < scalar(@metavalues); $i++) {
495 if ($metavalue eq $metavalues[$i]) {
496 $offset = $i;
497 last;
498 }
499 }
500 push(@{$classifier_node->{'contains'}}, { 'OID' => $OID, 'offset' => $offset });
501 }
502
503 # Otherwise create a sublist (bookshelf) for the metadata value
504 else {
505 my %child_classifier_node = ( 'Title' => &convert_unicode_string_to_utf8_string($metavalue),
506 'childtype' => "VList",
507 'contains' => [] );
508
509 # If there are metadata elements remaining, recursively apply the process
510 if (@metadata_groups > 0) {
511 my $next_metadata_group = $metadata_groups[0];
512 $child_classifier_node{'childtype'} = $self->{$next_metadata_group . ".list_type"};
513 &add_level($self, \@metadata_groups, \@OIDs, \%child_classifier_node);
514 }
515 # Otherwise just add the documents as children of this list
516 else {
517 # Sort the leaf nodes by the metadata elements specified with -sort_leaf_nodes_using
518 my @sort_leaf_nodes_usingmetadata_groups = @{$self->{'sort_leaf_nodes_using_metadata_groups'}};
519 foreach my $sort_leaf_nodes_usingmetaelem (reverse @sort_leaf_nodes_usingmetadata_groups) {
520 my %OIDtometavalueshash = %{$self->{$sort_leaf_nodes_usingmetaelem . ".list"}};
521 # Force a stable sort (Perl 5.6's sort isn't stable)
522 # !! The [0] bits aren't ideal (multiple metadata values) !!
523 @OIDs = @OIDs[ sort { $OIDtometavalueshash{$OIDs[$a]}[0] cmp $OIDtometavalueshash{$OIDs[$b]}[0] || $a <=> $b; } 0..$#OIDs ];
524 }
525
526 foreach my $OID (@OIDs) {
527 push(@{$child_classifier_node{'contains'}}, { 'OID' => $OID });
528 }
529 }
530
531 # Add the sublist to the list
532 push(@{$classifier_node->{'contains'}}, \%child_classifier_node);
533 }
534 }
535}
536
537
538sub sort_metadata_values_array
539{
540 my $self = shift(@_);
541 my @metadata_values = @_;
542
543 if ($self->{'unicode_collator'}) {
544 return $self->{'unicode_collator'}->sort(@metadata_values);
545 }
546 else {
547 return sort(@metadata_values);
548 }
549}
550
551
5521;
Note: See TracBrowser for help on using the repository browser.