source: gsdl/trunk/perllib/classify/List.pm@ 20679

Last change on this file since 20679 was 20679, checked in by mdewsnip, 15 years ago

Fixed up types of partition_type_within_level and partition_size_within_level options -- these need to be strings because they allow values for each level to be specified, separated by '/'.

  • Property svn:keywords set to Author Date Id Revision
File size: 33.1 KB
Line 
1###########################################################################
2#
3# List.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 List;
35
36
37use BaseClassifier;
38
39use strict;
40
41
42sub BEGIN {
43 @List::ISA = ('BaseClassifier');
44}
45
46my $partition_type_list =
47 [ { 'name' => "per_letter",
48 'desc' => "{List.level_partition.per_letter}" },
49 { 'name' => "constant_size",
50 'desc' => "{List.level_partition.constant_size}" },
51 { 'name' => "per_letter_fixed_size",
52 'desc' => "{List.level_partition.per_letter_fixed_size}"},
53 { 'name' => "none",
54 'desc' => "{List.level_partition.none}" } ];
55
56my $bookshelf_type_list =
57 [ { 'name' => "always",
58 'desc' => "{List.bookshelf_type.always}" },
59 { 'name' => "duplicate_only",
60 'desc' => "{List.bookshelf_type.duplicate_only}" },
61 { 'name' => "never",
62 'desc' => "{List.bookshelf_type.never}" } ];
63
64my $arguments =
65 [ { 'name' => "metadata",
66 'desc' => "{List.metadata}",
67 'type' => "metadata",
68 'reqd' => "yes" },
69
70 # The interesting options
71 { 'name' => "bookshelf_type",
72 'desc' => "{List.bookshelf_type}",
73 'type' => "enum",
74 'list' => $bookshelf_type_list,
75 'deft' => "never" },
76 { 'name' => "classify_sections",
77 'desc' => "{List.classify_sections}",
78 'type' => "flag" },
79 { 'name' => "partition_type_within_level",
80 'desc' => "{List.partition_type_within_level}",
81 'type' => "enumstring", # Must be enumstring because multiple values can be specified (separated by '/')
82 'list' => $partition_type_list,
83 'deft' => "per_letter" },
84 { 'name' => "partition_size_within_level",
85 'desc' => "{List.partition_size_within_level}",
86 'type' => "string" }, # Must be string because multiple values can be specified (separated by '/')
87 { 'name' => "partition_name_length",
88 'desc' => "{List.partition_name_length}",
89 'type' => "string" },
90 { 'name' => "sort_leaf_nodes_using",
91 'desc' => "{List.sort_leaf_nodes_using}",
92 'type' => "metadata",
93 'deft' => "Title" },
94 { 'name' => "sort_using_unicode_collation",
95 'desc' => "{List.sort_using_unicode_collation}",
96 'type' => "flag" },
97 { 'name' => "use_hlist_for",
98 'desc' => "{List.use_hlist_for}",
99 'type' => "string" },
100 { 'name' => "removeprefix",
101 'desc' => "{BasClas.removeprefix}",
102 'type' => "regexp" },
103 { 'name' => "removesuffix",
104 'desc' => "{BasClas.removesuffix}",
105 'type' => "regexp" } ];
106
107my $options = { 'name' => "List",
108 'desc' => "{List.desc}",
109 'abstract' => "no",
110 'inherits' => "yes",
111 'args' => $arguments };
112
113
114sub new
115{
116 my ($class) = shift(@_);
117 my ($classifierslist, $inputargs, $hashArgOptLists) = @_;
118 push(@$classifierslist, $class);
119
120 push(@{$hashArgOptLists->{"ArgList"}}, @{$arguments});
121 push(@{$hashArgOptLists->{"OptList"}}, $options);
122
123 my $self = new BaseClassifier($classifierslist, $inputargs, $hashArgOptLists);
124
125 if ($self->{'info_only'}) {
126 # don't worry about any options etc
127 return bless $self, $class;
128 }
129
130 # The metadata elements to use (required)
131 if (!$self->{'metadata'}) {
132 die "Error: No metadata fields specified for List.\n";
133 }
134 my @metadata_groups = split(/\//, $self->{'metadata'});
135 $self->{'metadata_groups'} = \@metadata_groups;
136
137 # The classifier button name (default: the first metadata element specified)
138 if (!$self->{'buttonname'}) {
139 my $first_metadata_group = $metadata_groups[0];
140 my $first_metadata_element = (split(/\;|,/, $first_metadata_group))[0];
141 $self->{'buttonname'} = $self->generate_title_from_metadata($first_metadata_element);
142 }
143
144 # Whether to group items into a bookshelf, (must be 'always' for all metadata fields except the last)
145 foreach my $metadata_group (@metadata_groups) {
146 $self->{$metadata_group . ".bookshelf_type"} = "always";
147 }
148 my $last_metadata_group = $metadata_groups[$#metadata_groups];
149 # Default: duplicate_only, ie. leave leaf nodes ungrouped (equivalent to AZCompactList -mingroup 2)
150 $self->{$last_metadata_group . ".bookshelf_type"} = $self->{'bookshelf_type'};
151
152 # Whether to use an hlist or a vlist for each level in the hierarchy (default: vlist)
153 foreach my $metadata_group (@metadata_groups) {
154 $self->{$metadata_group . ".list_type"} = "VList";
155 }
156 foreach my $metadata_group (split(/\,/, $self->{'use_hlist_for'})) {
157 $self->{$metadata_group . ".list_type"} = "HList";
158 }
159
160 # How the items are grouped into partitions (default: no partition)
161 # for each level (metadata group), separated by '/'
162 if (!$self->{"partition_type_within_level"}) {
163 foreach my $metadata_group (@metadata_groups) {
164 $self->{$metadata_group . ".partition_type_within_level"} = "none";
165 }
166 } else {
167 my @partition_type_within_levellist = split(/\//, $self->{'partition_type_within_level'});
168 foreach my $metadata_group (@metadata_groups) {
169 my $partition_type_within_levelelem = shift(@partition_type_within_levellist);
170 if (defined($partition_type_within_levelelem)) {
171 $self->{$metadata_group . ".partition_type_within_level"} = $partition_type_within_levelelem;
172 }
173 else {
174 $self->{$metadata_group . ".partition_type_within_level"} = $self->{$metadata_groups[0] . ".partition_type_within_level"};
175 }
176 }
177 }
178
179 # The number of items in each partition
180 if (!$self->{'partition_size_within_level'}) {
181 # Default: 20
182 foreach my $metadata_group (@metadata_groups) {
183 $self->{$metadata_group . ".partition_size_within_level"} = 20;
184 }
185 }
186 else {
187 my @partition_size_within_levellist = split(/\//, $self->{'partition_size_within_level'});
188
189 # Assign values based on the partition_size_within_level parameter
190 foreach my $metadata_group (@metadata_groups) {
191 my $partition_size_within_levelelem = shift(@partition_size_within_levellist);
192 if (defined($partition_size_within_levelelem)) {
193 $self->{$metadata_group . ".partition_size_within_level"} = $partition_size_within_levelelem;
194 }
195 else {
196 $self->{$metadata_group . ".partition_size_within_level"} = $self->{$metadata_groups[0] . ".partition_size_within_level"};
197 }
198 }
199 }
200
201 # The removeprefix and removesuffix expressions
202 if ($self->{'removeprefix'}) {
203 # If there are more than one expressions, use '' to quote each experession and '/' to separate
204 my @removeprefix_exprs_within_levellist = split(/'\/'/, $self->{'removeprefix'});
205
206 foreach my $metadata_group (@metadata_groups) {
207 my $removeprefix_expr_within_levelelem = shift(@removeprefix_exprs_within_levellist);
208 if (defined($removeprefix_expr_within_levelelem) && $removeprefix_expr_within_levelelem ne "") {
209 # Remove the other ' at the beginning and the end if there is any
210 $removeprefix_expr_within_levelelem =~ s/^'//;
211 $removeprefix_expr_within_levelelem =~ s/'$//;
212 # Remove the extra ^ at the beginning
213 $removeprefix_expr_within_levelelem =~ s/^\^//;
214 $self->{$metadata_group . ".remove_prefix_expr"} = $removeprefix_expr_within_levelelem;
215 } else {
216 $self->{$metadata_group . ".remove_prefix_expr"} = $self->{$metadata_groups[0] . ".remove_prefix_expr"};
217 }
218 }
219 }
220 if ($self->{'removesuffix'}) {
221 my @removesuffix_exprs_within_levellist = split(/'\/'/, $self->{'removesuffix'});
222
223 foreach my $metadata_group (@metadata_groups) {
224 my $removesuffix_expr_within_levelelem = shift(@removesuffix_exprs_within_levellist);
225 if (defined($removesuffix_expr_within_levelelem) && $removesuffix_expr_within_levelelem ne "") {
226 $removesuffix_expr_within_levelelem =~ s/^'//;
227 $removesuffix_expr_within_levelelem =~ s/'$//;
228 # Remove the extra $ at the end
229 $removesuffix_expr_within_levelelem =~ s/\$$//;
230 $self->{$metadata_group . ".remove_suffix_expr"} = $removesuffix_expr_within_levelelem;
231 } else {
232 $self->{$metadata_group . ".remove_suffix_expr"} = $self->{$metadata_groups[0] . ".remove_suffix_expr"};
233 }
234 }
235 }
236
237 # The metadata elements to use to sort the leaf nodes (default: Title)
238 my @sort_leaf_nodes_using_metadata_groups = ( "Title" );
239 if ($self->{'sort_leaf_nodes_using'}) {
240 @sort_leaf_nodes_using_metadata_groups = split(/\|/, $self->{'sort_leaf_nodes_using'});
241 }
242 $self->{'sort_leaf_nodes_using_metadata_groups'} = \@sort_leaf_nodes_using_metadata_groups;
243
244 # Create an instance of the Unicode::Collate object if better Unicode sorting is desired
245 if ($self->{'sort_using_unicode_collation'}) {
246 # To use this you first need to download the allkeys.txt file from
247 # http://www.unicode.org/Public/UCA/latest/allkeys.txt and put it in the Perl
248 # Unicode/Collate directory.
249 require Unicode::Collate;
250 $self->{'unicode_collator'} = Unicode::Collate->new();
251 }
252
253 # An empty array for the document OIDs
254 $self->{'OIDs'} = [];
255
256 return bless $self, $class;
257}
258
259
260sub init
261{
262 # Nothing to do...
263}
264
265
266# Called for each document in the collection
267sub classify
268{
269 my $self = shift(@_);
270 my ($doc_obj,$edit_mode) = @_;
271
272 # If "-classify_sections" is set, classify every section of the document
273 if ($self->{'classify_sections'}) {
274 my $section = $doc_obj->get_next_section($doc_obj->get_top_section());
275 while (defined $section) {
276 $self->classify_section($doc_obj, $doc_obj->get_OID() . ".$section", $section, $edit_mode);
277 $section = $doc_obj->get_next_section($section);
278 }
279 }
280 # Otherwise just classify the top document section
281 else {
282 $self->classify_section($doc_obj, $doc_obj->get_OID(), $doc_obj->get_top_section(), $edit_mode);
283 }
284}
285
286
287sub classify_section
288{
289 my $self = shift(@_);
290 my ($doc_obj,$section_OID,$section,$edit_mode) = @_;
291
292 my @metadata_groups = @{$self->{'metadata_groups'}};
293
294 # Only classify the section if it has a value for one of the metadata elements in the first group
295 my $classify_section = 0;
296 my $first_metadata_group = $metadata_groups[0];
297 foreach my $first_metadata_group_element (split(/\;|,/, $first_metadata_group)) {
298 my $real_first_metadata_group_element = $self->strip_ex_from_metadata($first_metadata_group_element);
299 my $first_metadata_group_element_value = $doc_obj->get_metadata_element($section, $real_first_metadata_group_element);
300
301 # Remove prefix/suffix if requested
302 my $remove_prefix_expr = $self->{$first_metadata_group_element . ".remove_prefix_expr"};
303 if (defined $remove_prefix_expr && $remove_prefix_expr ne "") {
304 $first_metadata_group_element_value =~ s/^$remove_prefix_expr//;
305 }
306 my $remove_suffix_expr = $self->{$first_metadata_group_element . ".remove_suffix_expr"};
307 if (defined $remove_suffix_expr && $remove_suffix_expr ne "") {
308 $first_metadata_group_element_value =~ s/$remove_suffix_expr$//;
309 }
310
311 if (defined($first_metadata_group_element_value) && $first_metadata_group_element_value ne "") {
312 # This section must be included in the classifier
313 $classify_section = 1;
314 last;
315 }
316 }
317
318 # We're not classifying this section because it doesn't have the required metadata
319 return if (!$classify_section);
320
321 if (($edit_mode eq "delete") || ($edit_mode eq "update")) {
322 $self->oid_array_delete($section_OID,'OIDs');
323 if ($edit_mode eq "delete") {
324 return;
325 }
326 }
327
328 # Otherwise, include this section in the classifier
329 push(@{$self->{'OIDs'}}, $section_OID);
330
331 # Create a hash for the metadata values of each metadata element we're interested in
332 my %metadata_groups_done = ();
333 foreach my $metadata_group (@metadata_groups, @{$self->{'sort_leaf_nodes_using_metadata_groups'}}) {
334 # Take care not to do a metadata group more than once
335 unless ($metadata_groups_done{$metadata_group}) {
336 if ($edit_mode eq "update") {
337 # if we are updating, we delete all the old values before
338 # adding the new ones, otherwise, the section will end up in
339 # the classifier twice.
340 delete $self->{$metadata_group . ".list"}->{$section_OID};
341 }
342 foreach my $metadata_element (split(/\;|,/, $metadata_group)) {
343 my $real_metadata_element = $self->strip_ex_from_metadata($metadata_element);
344
345 my $remove_prefix_expr = $self->{$metadata_element . ".remove_prefix_expr"};
346 my $remove_suffix_expr = $self->{$metadata_element . ".remove_suffix_expr"};
347 my @metadata_values = @{$doc_obj->get_metadata($section, $real_metadata_element)};
348 foreach my $metadata_value (@metadata_values) {
349 # Strip leading and trailing whitespace
350 $metadata_value =~ s/^\s*//;
351 $metadata_value =~ s/\s*$//;
352
353 # Remove prefix/suffix if requested
354 if (defined $remove_prefix_expr && $remove_prefix_expr ne "") {
355 $metadata_value =~ s/^$remove_prefix_expr//;
356 }
357 if (defined $remove_suffix_expr && $remove_suffix_expr ne "") {
358 $metadata_value =~ s/$remove_suffix_expr$//;
359 }
360
361 # Convert the metadata value from a UTF-8 string to a Unicode string
362 # This means that length() and substr() work properly
363 # We need to be careful to convert classifier node title values back to UTF-8, however
364 my $metadata_value_unicode_string = $self->convert_utf8_string_to_unicode_string($metadata_value);
365
366 # Add the metadata value into the list for this combination of metadata group and section
367 push(@{$self->{$metadata_group . ".list"}->{$section_OID}}, $metadata_value_unicode_string);
368 }
369 last if (@metadata_values > 0);
370 }
371
372 $metadata_groups_done{$metadata_group} = 1;
373 }
374 }
375}
376
377
378sub get_classify_info
379{
380 my $self = shift(@_);
381
382 # The metadata groups to classify by
383 my @metadata_groups = @{$self->{'metadata_groups'}};
384 my $first_metadata_group = $metadata_groups[0];
385
386 # The OID values of the documents to include in the classifier
387 my @OIDs = @{$self->{'OIDs'}};
388
389 # Create the root node of the classification hierarchy
390 my %classifier_node = ( 'thistype' => "Invisible",
391 'childtype' => $self->{$first_metadata_group . ".list_type"},
392 'Title' => $self->{'buttonname'},
393 'contains' => [],
394 'mdtype' => $first_metadata_group );
395
396 # Recursively create the classification hierarchy, one level for each metadata group
397 $self->add_level(\@metadata_groups, \@OIDs, \%classifier_node);
398 return \%classifier_node;
399}
400
401
402sub add_level
403{
404 my $self = shift(@_);
405 my @metadata_groups = @{shift(@_)};
406 my @OIDs = @{shift(@_)};
407 my $classifier_node = shift(@_);
408 # print STDERR "\nAdding AZ list for " . $classifier_node->{'Title'} . "\n";
409
410 my $metadata_group = $metadata_groups[0];
411 # print STDERR "Processing metadata group: " . $metadata_group . "\n";
412 # print STDERR "Number of OID values: " . @OIDs . "\n";
413
414 if (!defined($self->{$metadata_group . ".list"})) {
415 print STDERR "Warning: No metadata values assigned to $metadata_group.\n";
416 return;
417 }
418
419 # Create a mapping from metadata value to OID
420 my $OID_to_metadata_values_hash_ref = $self->{$metadata_group . ".list"};
421 my %metadata_value_to_OIDs_hash = ();
422 foreach my $OID (@OIDs)
423 {
424 if ($OID_to_metadata_values_hash_ref->{$OID})
425 {
426 my @metadata_values = @{$OID_to_metadata_values_hash_ref->{$OID}};
427 foreach my $metadata_value (@metadata_values)
428 {
429 push(@{$metadata_value_to_OIDs_hash{$metadata_value}}, $OID);
430 }
431 }
432 }
433 # print STDERR "Number of distinct values: " . scalar(keys %metadata_value_to_OIDs_hash) . "\n";
434
435 # Partition the values (if necessary)
436 my $partition_type_within_level = $self->{$metadata_group . ".partition_type_within_level"};
437 if ($partition_type_within_level =~ /^per_letter$/i) {
438 # Generate one hlist for each letter
439 my @sortedmetadata_values = $self->sort_metadata_values_array(keys(%metadata_value_to_OIDs_hash));
440 my %metadata_value_to_OIDs_subhash = ();
441
442 my $lastpartition = substr($sortedmetadata_values[0], 0, 1);
443 foreach my $metadata_value (@sortedmetadata_values) {
444 my $metadata_valuepartition = substr($metadata_value, 0, 1);
445
446 # Is this the start of a new partition?
447 if ($metadata_valuepartition ne $lastpartition) {
448 $self->add_hlist_partition(\@metadata_groups, $classifier_node, $lastpartition, \%metadata_value_to_OIDs_subhash);
449 %metadata_value_to_OIDs_subhash = ();
450 $lastpartition = $metadata_valuepartition;
451 }
452
453 $metadata_value_to_OIDs_subhash{$metadata_value} = $metadata_value_to_OIDs_hash{$metadata_value};
454 }
455
456 # Don't forget to add the last partition
457 $self->add_hlist_partition(\@metadata_groups, $classifier_node, $lastpartition, \%metadata_value_to_OIDs_subhash);
458
459 # The partitions are stored in an HList
460 $classifier_node->{'childtype'} = "HList";
461 }
462 elsif ($partition_type_within_level =~ /^per_letter_fixed_size$/i) {
463 # Generate hlist based on the first letter of the metadata value (like per_letter) but with restriction on the partition size
464 # If a letter has fewer items than specified by the "partition_size_within_level", then group them together if possible
465 # If a letter has more items than specified, split into several hlists.
466 # Depends on the bookshelf_type, one item can be either a document (when bookshelf_type is "never") or a metadata value (otherwise)
467 my $partition_size_within_level = $self->{$metadata_group . ".partition_size_within_level"};
468 my @sortedmetadata_values = $self->sort_metadata_values_array(keys(%metadata_value_to_OIDs_hash));
469 my $bookshelf_type = $self->{$metadata_group . ".bookshelf_type"};
470
471 # Separate values by their first letter, each form a bucket, like the per_letter partition type
472 my $last_partition = substr($sortedmetadata_values[0], 0, 1);
473 my %partition_buckets = ();
474 my @metadata_values_in_bucket = ();
475 my $num_items_in_bucket = 0;
476 foreach my $metadata_value (@sortedmetadata_values) {
477 my $metadata_valuepartition = substr($metadata_value, 0, 1);
478 if ($metadata_valuepartition ne $last_partition) {
479 my @temp_array = @metadata_values_in_bucket;
480 # Cache the values that belong to this bucket, and the number of items in this bucket, not necessary to be the same number as the metadata values
481 my %partition_info = ();
482 $partition_info{'metadata_values'} = \@temp_array;
483 $partition_info{'size'} = $num_items_in_bucket;
484 $partition_buckets{$last_partition} = \%partition_info;
485
486 @metadata_values_in_bucket = ($metadata_value);
487 $num_items_in_bucket = $bookshelf_type eq "never" ? scalar(@{$metadata_value_to_OIDs_hash{$metadata_value}}) : scalar(@metadata_values_in_bucket);
488 $last_partition = $metadata_valuepartition;
489 } else {
490 $num_items_in_bucket += $bookshelf_type eq "never" ? scalar(@{$metadata_value_to_OIDs_hash{$metadata_value}}) : scalar(@metadata_values_in_bucket);
491 push (@metadata_values_in_bucket, $metadata_value);
492 }
493 }
494 # Last one
495 my %partition_info = ();
496 $partition_info{'metadata_values'} = \@metadata_values_in_bucket;
497 $partition_info{'size'} = $num_items_in_bucket;
498 $partition_buckets{$last_partition} = \%partition_info;
499
500 my @partition_keys = $self->sort_metadata_values_array(keys(%partition_buckets));
501 for (my $i = 0; $i < scalar(@partition_keys) - 1; $i++) {
502 my $partition = $partition_keys[$i];
503 my $items_in_partition = $partition_buckets{$partition}->{'size'};
504 # Merge small buckets together, but keep the numeric bucket apart
505 if ($items_in_partition < $partition_size_within_level) {
506 my $items_in_next_partition = $partition_buckets{$partition_keys[$i+1]}->{'size'};
507 if ($items_in_partition + $items_in_next_partition <= $partition_size_within_level
508 && !(($partition =~ /^[^0-9]/ && $partition_keys[$i+1] =~ /^[0-9]/)
509 || ($partition =~ /^[0-9]/ && $partition_keys[$i+1] =~ /^[^0-9]/))) {
510 foreach my $metadata_value_to_merge (@{$partition_buckets{$partition}->{'metadata_values'}}) {
511 push(@{$partition_buckets{$partition_keys[$i+1]}->{'metadata_values'}}, $metadata_value_to_merge);
512 }
513 $partition_buckets{$partition_keys[$i+1]}->{'size'} += $items_in_partition;
514 delete $partition_buckets{$partition};
515 }
516 }
517 }
518 @partition_keys = $self->sort_metadata_values_array(keys(%partition_buckets));
519
520 # Add partitions, and divide big bucket into several
521 my $last_partition_end = "";
522 my $partition_start = "";
523 foreach my $partition (@partition_keys) {
524 my @metadata_values = $self->sort_metadata_values_array(@{$partition_buckets{$partition}->{'metadata_values'}});
525 my $items_in_partition = $partition_buckets{$partition}->{'size'};
526 $partition_start = $self->generate_partition_start($metadata_values[0], $last_partition_end, $self->{"partition_name_length"});
527
528 if ($items_in_partition > $partition_size_within_level) {
529 my $items_done = 0;
530 my %metadata_values_to_OIDs_subhashes = ();
531 for (my $i = 0; $i < scalar(@metadata_values); $i++) {
532 my $metadata_value = $metadata_values[$i];
533 # If the bookshelf_type is "never", count the documents, otherwise count the distinct metadata values
534 my $items_for_this_md_value = $bookshelf_type eq "never" ? scalar(@{$metadata_value_to_OIDs_hash{$metadata_value}}) : 1;
535
536 my $partitionend = $self->generate_partition_end($metadata_value, $partition_start, $self->{"partition_name_length"});
537 my $partitionname = $partition_start;
538 if ($partitionend ne $partition_start) {
539 $partitionname = $partitionname . "-" . $partitionend;
540 }
541
542 # Start a new partition
543 if ($items_done + $items_for_this_md_value > $partition_size_within_level && $items_done != 0) {
544 $self->add_hlist_partition(\@metadata_groups, $classifier_node, $partitionname, \%metadata_values_to_OIDs_subhashes);
545 $last_partition_end = $partitionend;
546 $partition_start = $self->generate_partition_start($metadata_value, $last_partition_end, $self->{"partition_name_length"});
547 $items_done = 0;
548 %metadata_values_to_OIDs_subhashes = ();
549 }
550
551 # If bookshelf_type is "never" and the current metadata value holds too many items, need to split into several partitions
552 if ($bookshelf_type eq "never" && $items_for_this_md_value > $partition_size_within_level) {
553 my $partitionname_for_this_value = $self->generate_partition_start($metadata_value, $last_partition_end, $self->{"partition_name_length"});
554 # Get the number of partitions needed for this value
555 my $num_splits = int($items_for_this_md_value / $partition_size_within_level);
556 $num_splits++ if ($items_for_this_md_value / $partition_size_within_level > $num_splits);
557
558 my @OIDs_for_this_value = @{$metadata_value_to_OIDs_hash{$metadata_value}};
559 for (my $i = 0; $i < $num_splits; $i++) {
560 my %OIDs_subhashes_for_this_value = ();
561 my @OIDs_for_this_partition = ();
562 for (my $d = $i * $partition_size_within_level; $d < (($i+1) * $partition_size_within_level > $items_for_this_md_value ? $items_for_this_md_value : ($i+1) * $partition_size_within_level); $d++) {
563 push (@OIDs_for_this_partition, $OIDs_for_this_value[$d]);
564 }
565
566 # The last bucket might have only a few items and need to be merged with buckets for subsequent metadata values
567 if ($i == $num_splits - 1 && scalar(@OIDs_for_this_partition) < $partition_size_within_level) {
568 $metadata_values_to_OIDs_subhashes{$metadata_value} = \@OIDs_for_this_partition;
569 $items_done += scalar(@OIDs_for_this_partition);
570 next;
571 }
572
573 # Add an HList for this bucket
574 $OIDs_subhashes_for_this_value{$metadata_value} = \@OIDs_for_this_partition;
575 $self->add_hlist_partition(\@metadata_groups, $classifier_node, $partitionname_for_this_value, \%OIDs_subhashes_for_this_value);
576 $last_partition_end = $partitionname_for_this_value;
577 }
578 next;
579 }
580
581 $metadata_values_to_OIDs_subhashes{$metadata_value} = $metadata_value_to_OIDs_hash{$metadata_value};
582 $items_done += $bookshelf_type eq "never" ? scalar(@{$metadata_values_to_OIDs_subhashes{$metadata_value}}) : 1;
583
584 # The last partition
585 if($i == scalar(@metadata_values) - 1) {
586 $self->add_hlist_partition(\@metadata_groups, $classifier_node, $partitionname, \%metadata_values_to_OIDs_subhashes);
587 }
588 }
589 }
590 else {
591 # The easier case, just add a partition
592 my %metadata_values_to_OIDs_subhashes = ();
593 for (my $i = 0; $i < scalar(@metadata_values); $i++) {
594 my $metadata_value = $metadata_values[$i];
595 $metadata_values_to_OIDs_subhashes{$metadata_value} = $metadata_value_to_OIDs_hash{$metadata_value};
596 }
597 my $last_metadata_value = $metadata_values[scalar(@metadata_values)-1];
598 my $partitionend = $self->generate_partition_end($last_metadata_value, $partition_start, $self->{"partition_name_length"});
599 my $partitionname = $partition_start;
600 if ($partitionend ne $partition_start) {
601 $partitionname = $partitionname . "-" . $partitionend;
602 }
603 $self->add_hlist_partition(\@metadata_groups, $classifier_node, $partitionname, \%metadata_values_to_OIDs_subhashes);
604 $last_partition_end = $partitionend;
605 }
606 }
607 }
608 else {
609 # Generate hlists of a certain size
610 my $partition_size_within_level = $self->{$metadata_group . ".partition_size_within_level"};
611 if ($partition_type_within_level =~ /^constant_size$/i && scalar(keys %metadata_value_to_OIDs_hash) > $partition_size_within_level) {
612 my @sortedmetadata_values = $self->sort_metadata_values_array(keys(%metadata_value_to_OIDs_hash));
613 my $itemsdone = 0;
614 my %metadata_value_to_OIDs_subhash = ();
615 my $lastpartitionend = "";
616 my $partitionstart;
617 foreach my $metadata_value (@sortedmetadata_values) {
618 $metadata_value_to_OIDs_subhash{$metadata_value} = $metadata_value_to_OIDs_hash{$metadata_value};
619 $itemsdone++;
620 my $itemsinpartition = scalar(keys %metadata_value_to_OIDs_subhash);
621
622 # Is this the start of a new partition?
623 if ($itemsinpartition == 1) {
624 $partitionstart = $self->generate_partition_start($metadata_value, $lastpartitionend, $self->{"partition_name_length"});
625 }
626
627 # Is this the end of the partition?
628 if ($itemsinpartition == $partition_size_within_level || $itemsdone == @sortedmetadata_values) {
629 my $partitionend = $self->generate_partition_end($metadata_value, $partitionstart, $self->{"partition_name_length"});
630 my $partitionname = $partitionstart;
631 if ($partitionend ne $partitionstart) {
632 $partitionname = $partitionname . "-" . $partitionend;
633 }
634
635 $self->add_hlist_partition(\@metadata_groups, $classifier_node, $partitionname, \%metadata_value_to_OIDs_subhash);
636 %metadata_value_to_OIDs_subhash = ();
637 $lastpartitionend = $partitionend;
638 }
639 }
640
641 # The partitions are stored in an HList
642 $classifier_node->{'childtype'} = "HList";
643 }
644
645 # Otherwise just add all the values to a VList
646 else {
647 $self->add_vlist(\@metadata_groups, $classifier_node, \%metadata_value_to_OIDs_hash);
648 }
649 }
650}
651
652
653sub convert_utf8_string_to_unicode_string
654{
655 my $self = shift(@_);
656 my $utf8_string = shift(@_);
657
658 my $unicode_string = "";
659 foreach my $unicode_value (@{&unicode::utf82unicode($utf8_string)}) {
660 $unicode_string .= chr($unicode_value);
661 }
662 return $unicode_string;
663}
664
665
666sub convert_unicode_string_to_utf8_string
667{
668 my $self = shift(@_);
669 my $unicode_string = shift(@_);
670
671 my @unicode_array;
672 for (my $i = 0; $i < length($unicode_string); $i++) {
673 push(@unicode_array, ord(substr($unicode_string, $i, 1)));
674 }
675 return &unicode::unicode2utf8(\@unicode_array);
676}
677
678
679sub generate_partition_start
680{
681 my $self = shift(@_);
682 my $metadata_value = shift(@_);
683 my $lastpartitionend = shift(@_);
684 my $partition_name_length = shift(@_);
685
686 if ($partition_name_length) {
687 return substr($metadata_value, 0, $partition_name_length);
688 }
689
690 my $partitionstart = substr($metadata_value, 0, 1);
691 if ($partitionstart le $lastpartitionend) {
692 $partitionstart = substr($metadata_value, 0, 2);
693 # Give up after three characters
694 if ($partitionstart le $lastpartitionend) {
695 $partitionstart = substr($metadata_value, 0, 3);
696 }
697 }
698
699 return $partitionstart;
700}
701
702
703sub generate_partition_end
704{
705 my $self = shift(@_);
706 my $metadata_value = shift(@_);
707 my $partitionstart = shift(@_);
708 my $partition_name_length = shift(@_);
709
710 if ($partition_name_length) {
711 return substr($metadata_value, 0, $partition_name_length);
712 }
713
714 my $partitionend = substr($metadata_value, 0, length($partitionstart));
715 if ($partitionend gt $partitionstart) {
716 $partitionend = substr($metadata_value, 0, 1);
717 if ($partitionend le $partitionstart) {
718 $partitionend = substr($metadata_value, 0, 2);
719 # Give up after three characters
720 if ($partitionend le $partitionstart) {
721 $partitionend = substr($metadata_value, 0, 3);
722 }
723 }
724 }
725
726 return $partitionend;
727}
728
729
730sub add_hlist_partition
731{
732 my $self = shift(@_);
733 my @metadata_groups = @{shift(@_)};
734 my $classifier_node = shift(@_);
735 my $partitionname = shift(@_);
736 my $metadata_value_to_OIDs_hash_ref = shift(@_);
737
738 # Create an hlist partition
739 my %child_classifier_node = ( 'Title' => $self->convert_unicode_string_to_utf8_string($partitionname),
740 'childtype' => "VList",
741 'contains' => [] );
742
743 # Add the children to the hlist partition
744 $self->add_vlist(\@metadata_groups, \%child_classifier_node, $metadata_value_to_OIDs_hash_ref);
745 push(@{$classifier_node->{'contains'}}, \%child_classifier_node);
746}
747
748
749sub add_vlist
750{
751 my $self = shift(@_);
752 my @metadata_groups = @{shift(@_)};
753 my $classifier_node = shift(@_);
754 my $metadata_value_to_OIDs_hash_ref = shift(@_);
755
756 my $metadata_group = shift(@metadata_groups);
757 $classifier_node->{'mdtype'} = $metadata_group;
758
759 # Create an entry in the vlist for each value
760 foreach my $metadata_value ($self->sort_metadata_values_array(keys(%{$metadata_value_to_OIDs_hash_ref})))
761 {
762 my @OIDs = @{$metadata_value_to_OIDs_hash_ref->{$metadata_value}};
763
764 # If there is only one item and 'bookshelf_type' is not always (ie. never or duplicate_only), add the item to the list
765 if (@OIDs == 1 && $self->{$metadata_group . ".bookshelf_type"} ne "always") {
766 my $OID = $OIDs[0];
767
768 # Find the offset of this metadata value
769 my $offset = 0;
770 my $OID_to_metadata_values_hash_ref = $self->{$metadata_group . ".list"};
771 my @metadata_values = @{$OID_to_metadata_values_hash_ref->{$OID}};
772 for (my $i = 0; $i < scalar(@metadata_values); $i++) {
773 if ($metadata_value eq $metadata_values[$i]) {
774 $offset = $i;
775 last;
776 }
777 }
778 push(@{$classifier_node->{'contains'}}, { 'OID' => $OID, 'offset' => $offset });
779 }
780 # If 'bookshelf_type' is 'never', list all the items even if there are duplicated values
781 elsif ($self->{$metadata_group . ".bookshelf_type"} eq "never") {
782 $self->add_sorted_leaf_items(\@OIDs, $classifier_node);
783 }
784 # Otherwise create a sublist (bookshelf) for the metadata value
785 else {
786 my %child_classifier_node = ( 'Title' => $self->convert_unicode_string_to_utf8_string($metadata_value),
787 'childtype' => "VList",
788 'contains' => [] );
789
790 # If there are metadata elements remaining, recursively apply the process
791 if (@metadata_groups > 0) {
792 my $next_metadata_group = $metadata_groups[0];
793 $child_classifier_node{'childtype'} = $self->{$next_metadata_group . ".list_type"};
794 $self->add_level(\@metadata_groups, \@OIDs, \%child_classifier_node);
795 }
796 # Otherwise just add the documents as children of this list
797 else {
798 $self->add_sorted_leaf_items(\@OIDs, \%child_classifier_node);
799 }
800
801 # Add the sublist to the list
802 push(@{$classifier_node->{'contains'}}, \%child_classifier_node);
803 }
804 }
805}
806
807sub add_sorted_leaf_items
808{
809 my $self = shift(@_);
810 my @OIDs = @{shift(@_)};
811 my $classifier_node = shift(@_);
812
813 # Sort leaf nodes and add to list
814 my @sort_leaf_nodes_usingmetadata_groups = @{$self->{'sort_leaf_nodes_using_metadata_groups'}};
815 foreach my $sort_leaf_nodes_usingmetaelem (reverse @sort_leaf_nodes_usingmetadata_groups) {
816 my $OID_to_metadata_values_hash_ref = $self->{$sort_leaf_nodes_usingmetaelem . ".list"};
817 # Force a stable sort (Perl 5.6's sort isn't stable)
818 # !! The [0] bits aren't ideal (multiple metadata values) !!
819 @OIDs = @OIDs[ sort { $OID_to_metadata_values_hash_ref->{$OIDs[$a]}[0] cmp $OID_to_metadata_values_hash_ref->{$OIDs[$b]}[0] || $a <=> $b; } 0..$#OIDs ];
820 }
821
822 foreach my $OID (@OIDs) {
823 push(@{$classifier_node->{'contains'}}, { 'OID' => $OID });
824 }
825}
826
827
828sub sort_metadata_values_array
829{
830 my $self = shift(@_);
831 my @metadata_values = @_;
832
833 if ($self->{'unicode_collator'}) {
834 return $self->{'unicode_collator'}->sort(@metadata_values);
835 }
836 else {
837 return sort(@metadata_values);
838 }
839}
840
841
8421;
Note: See TracBrowser for help on using the repository browser.