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

Last change on this file since 20421 was 20421, checked in by kjdon, 15 years ago

strip off any ex. before getting metadata from doc_obj

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