source: main/trunk/greenstone2/perllib/classify/List.pm@ 33479

Last change on this file since 33479 was 33479, checked in by kjdon, 5 years ago

changed numeric option order to match letter options

  • Property svn:keywords set to Author Date Id Revision
File size: 55.5 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#
12# Copyright (C) 2005 New Zealand Digital Library Project
13#
14# This program is free software; you can redistribute it and/or modify
15# it under the terms of the GNU General Public License as published by
16# the Free Software Foundation; either version 2 of the License, or
17# (at your option) any later version.
18#
19# This program is distributed in the hope that it will be useful,
20# but WITHOUT ANY WARRANTY; without even the implied warranty of
21# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22# GNU General Public License for more details.
23#
24# You should have received a copy of the GNU General Public License
25# along with this program; if not, write to the Free Software
26# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27#
28# TO DO: - Remove punctuation from metadata values before sorting.
29# - Add an AZCompactList-style hlist option?
30#
31###########################################################################
32
33
34package List;
35
36
37use BaseClassifier;
38use Sort::Naturally;
39use strict;
40
41
42sub BEGIN {
43 @List::ISA = ('BaseClassifier');
44}
45
46
47my $metadata_selection_mode_list =
48 [
49 { 'name' => "firstvalue",
50 'desc' => "{List.metadata_selection.firstvalue}"},
51 { 'name' => "firstvalidmetadata",
52 'desc' => "{List.metadata_selection.firstvalidmetadata}"},
53 { 'name' => "allvalues",
54 'desc' => "{List.metadata_selection.allvalues}"} ];
55
56my $metadata_selection_mode_default = "firstvalidmetadata";
57
58my $valid_metadata_selection_modes = { 'firstvalue' => 1,
59 'firstvalidmetadata' => 1,
60 'allvalues' => 1 };
61
62my $metadata_sort_mode_list =
63 [
64 { 'name' => "unicode",
65 'desc' => "{List.metadata_sort.unicode}"},
66 { 'name' => "alphabetic",
67 'desc' => "{List.metadata_sort.alphabetic}"},
68 { 'name' => "alphanumeric",
69 'desc' => "{List.metadata_sort.alphanumeric}"} ];
70
71my $metadata_sort_mode_default = "alphanumeric";
72
73my $valid_metadata_sort_modes = { 'unicode' => 1,
74 'alphabetic' => 1,
75 'alphanumeric' => 1};
76my $partition_type_list =
77 [ { 'name' => "per_letter",
78 'desc' => "{List.level_partition.per_letter}" },
79 { 'name' => "approximate_size",
80 'desc' => "{List.level_partition.approximate_size}"},
81 { 'name' => "constant_size",
82 'desc' => "{List.level_partition.constant_size}" },
83 { 'name' => "all_values",
84 'desc' => "{List.level_partition.all_values}" },
85 { 'name' => "none",
86 'desc' => "{List.level_partition.none}" } ];
87
88my $partition_type_default = "per_letter";
89
90my $valid_partition_types = { 'per_letter' => 1,
91 'constant_size' => 1,
92 'approximate_size' => 1,
93 'all_values' => 1,
94 'none' => 1};
95
96my $partition_size_default = 30;
97
98my $numeric_partition_type_list =
99 [ { 'name' => "per_digit",
100 'desc' => "{List.level_partition.per_digit}" },
101 { 'name' => "per_number",
102 'desc' => "{List.level_partition.per_number}" },
103 { 'name' => "single_partition",
104 'desc' => "{List.level_partition.single}" },
105 { 'name' => "approximate_size",
106 'desc' => "{List.level_partition.approximate_size_numeric}"},
107 { 'name' => "constant_size",
108 'desc' => "{List.level_partition.constant_size}" },
109 { 'name' => "all_values",
110 'desc' => "{List.level_partition.all_values}" },
111 { 'name' => "none",
112 'desc' => "{List.level_partition.none}" } ];
113
114my $numeric_partition_type_default = "single_partition";
115
116my $valid_numeric_partition_types = { 'per_digit' => 1,
117 'per_number' => 1,
118 'constant_size' => 1,
119 'single_partition' => 1,
120 'approximate_size' => 1,
121 'all_values' => 1,
122 'none' =>1 };
123
124my $numeric_partition_size_default = 30;
125
126my $numeric_partition_name_length_default = "-1"; # use the full number
127
128my $bookshelf_type_list =
129 [ { 'name' => "always",
130 'desc' => "{List.bookshelf_type.always}" },
131 { 'name' => "duplicate_only",
132 'desc' => "{List.bookshelf_type.duplicate_only}" },
133 { 'name' => "never",
134 'desc' => "{List.bookshelf_type.never}" } ];
135
136my $bookshelf_type_default = "never";
137my $sort_leaf_nodes_using_default = "Title";
138my $arguments =
139 [ { 'name' => "metadata",
140 'desc' => "{List.metadata}",
141 'type' => "metadata",
142 'reqd' => "yes" },
143
144 { 'name' => "metadata_selection_mode_within_level",
145 'desc' => "{List.metadata_selection_mode_within_level}",
146 'type' => "enumstring", # Must be enumstring because multiple values can be specified (separated by '/')
147 'list' => $metadata_selection_mode_list,
148 'deft' => $metadata_selection_mode_default },
149
150 { 'name' => "metadata_sort_mode_within_level",
151 'desc' => "{List.metadata_sort_mode_within_level}",
152 'type' => "enumstring", # Must be enumstring because multiple values can be specified (separated by '/')
153 'list' => $metadata_sort_mode_list,
154 'deft' => $metadata_sort_mode_default },
155
156 { 'name' => "bookshelf_type",
157 'desc' => "{List.bookshelf_type}",
158 'type' => "enum",
159 'list' => $bookshelf_type_list,
160 'deft' => $bookshelf_type_default },
161
162 { 'name' => "classify_sections",
163 'desc' => "{List.classify_sections}",
164 'type' => "flag" },
165
166 { 'name' => "partition_type_within_level",
167 'desc' => "{List.partition_type_within_level}",
168 'type' => "enumstring", # Must be enumstring because multiple values can be specified (separated by '/')
169 'list' => $partition_type_list,
170 'deft' => $partition_type_default },
171
172 { 'name' => "partition_size_within_level",
173 'desc' => "{List.partition_size_within_level}",
174 'type' => "string", # Must be string because multiple values can be specified (separated by '/')
175 'deft' => $partition_size_default},
176
177 { 'name' => "partition_name_length",
178 'desc' => "{List.partition_name_length}",
179 'type' => "string" },
180
181 {'name' => "partition_sort_mode_within_level",
182 'desc' => "{List.partition_sort_mode_within_level}",
183 'type' => "enumstring", # Must be enumstring because multiple values can be specified (separated by '/')
184 'list' => $metadata_sort_mode_list,
185 'deft' => $metadata_sort_mode_default },
186
187 { 'name' => "numeric_partition_type_within_level",
188 'desc' => "{List.numeric_partition_type_within_level}",
189 'type' => "enumstring", # Must be enumstring because multiple values can be specified (separated by '/')
190 'list' => $numeric_partition_type_list,
191 'deft' => $numeric_partition_type_default },
192
193 { 'name' => "numeric_partition_size_within_level",
194 'desc' => "{List.numeric_partition_size_within_level}",
195 'type' => "string", # Must be string because multiple values can be specified (separated by '/')
196 'deft' => $numeric_partition_size_default},
197
198 { 'name' => "numeric_partition_name_length_within_level",
199 'desc' => "{List.numeric_partition_name_length_within_level}",
200 'type' => "string",
201 'deft' => $numeric_partition_name_length_default },
202
203 {'name' => "numeric_partition_sort_mode_within_level",
204 'desc' => "{List.numeric_partition_sort_mode_within_level}",
205 'type' => "enumstring", # Must be enumstring because multiple values can be specified (separated by '/')
206 'list' => $metadata_sort_mode_list,
207 'deft' => $metadata_sort_mode_default },
208
209 { 'name' => "numbers_first",
210 'desc' => "{List.numbers_first}",
211 'type' => 'flag'},
212
213 { 'name' => "sort_leaf_nodes_using",
214 'desc' => "{List.sort_leaf_nodes_using}",
215 'type' => "metadata",
216 'deft' => $sort_leaf_nodes_using_default },
217
218 { 'name' => "sort_leaf_nodes_sort_mode",
219 'desc' => "{List.sort_leaf_nodes_sort_mode}",
220 'type' => "enum",
221 'list' => $metadata_sort_mode_list,
222 'deft' => $metadata_sort_mode_default },
223
224 { 'name' => "reverse_sort_leaf_nodes",
225 'desc' => "{List.reverse_sort_leaf_nodes}",
226 'type' => "flag"},
227
228 { 'name' => "sort_using_unicode_collation",
229 'desc' => "{List.metadata_sort.unicode} {List.sort_using_unicode_collation}",
230 'type' => "flag" },
231
232 {'name' => "filter_metadata",
233 'desc' => "{List.filter_metadata}",
234 'type' => "metadata"},
235
236 {'name' => "filter_regex",
237 'desc' => "{List.filter_regex}",
238 'type' => "regexp"},
239
240 { 'name' => "standardize_capitalization",
241 'desc' => "{List.standardize_capitalization}",
242 'type' => "flag"},
243
244 { 'name' => "removeprefix",
245 'desc' => "{BasClas.removeprefix}",
246 'type' => "regexp" },
247
248 { 'name' => "removesuffix",
249 'desc' => "{BasClas.removesuffix}",
250 'type' => "regexp" } ];
251
252my $options = { 'name' => "List",
253 'desc' => "{List.desc}",
254 'abstract' => "no",
255 'inherits' => "yes",
256 'args' => $arguments };
257
258
259sub new
260{
261 my ($class) = shift(@_);
262 my ($classifierslist, $inputargs, $hashArgOptLists) = @_;
263 push(@$classifierslist, $class);
264
265 push(@{$hashArgOptLists->{"ArgList"}}, @{$arguments});
266 push(@{$hashArgOptLists->{"OptList"}}, $options);
267
268 my $self = new BaseClassifier($classifierslist, $inputargs, $hashArgOptLists);
269
270 bless $self, $class;
271
272 if ($self->{'info_only'}) {
273 # don't worry about any options etc
274 return $self;
275 }
276
277 # The metadata elements to use (required)
278 if (!$self->{'metadata'}) {
279 die "Error: No metadata fields specified for List.\n";
280 }
281
282 my @metadata_groups = split(/[\/]/, $self->{'metadata'});
283 $self->{'metadata_groups'} = \@metadata_groups;
284
285 # The classifier button name (default: the first metadata element specified)
286 if (!$self->{'buttonname'}) {
287 my $first_metadata_group = $metadata_groups[0];
288 my $first_metadata_element = (split(/[\;|,\/]/, $first_metadata_group))[0];
289 $self->{'buttonname'} = $self->generate_title_from_metadata($first_metadata_element);
290 }
291
292 # meta selection mode for each level
293 $self->set_metadata_groups_info_per_level("metadata_selection_mode_within_level", $metadata_selection_mode_default, $valid_metadata_selection_modes);
294
295 # meta sort mode for each level
296 if ($self->{'sort_using_unicode_collation'}) {
297 print STDERR "WARNING: sort_using_unicode_collation is set, setting metadata_sort_mode_within_level to unicode for all levels, regardless of current setting\n";
298 $self->{'metadata_sort_mode_within_level'} = "unicode";
299 $metadata_sort_mode_default = "unicode";
300 } else {
301 if ($self->{'metadata_sort_mode_within_level'} =~ /unicode/) {
302 $self->{'sort_using_unicode_collation'} = 1;
303 }
304 }
305
306
307 $self->set_metadata_groups_info_per_level('metadata_sort_mode_within_level', $metadata_sort_mode_default, $valid_metadata_sort_modes);
308
309 # Whether to group items into a bookshelf, (must be 'always' for all metadata fields except the last)
310 foreach my $metadata_group (@metadata_groups) {
311 $self->{$metadata_group . ".bookshelf_type"} = "always";
312 }
313 my $last_metadata_group = $metadata_groups[$#metadata_groups];
314 # Default: duplicate_only, ie. leave leaf nodes ungrouped (equivalent to AZCompactList -mingroup 2)
315 $self->{$last_metadata_group . ".bookshelf_type"} = $self->{'bookshelf_type'};
316
317 # How the items are grouped into partitions (default: by letter)
318 # for each level (metadata group), separated by '/'
319 $self->set_metadata_groups_info_per_level("partition_type_within_level", $partition_type_default, $valid_partition_types);
320 $self->set_metadata_groups_info_per_level("numeric_partition_type_within_level", $numeric_partition_type_default, $valid_numeric_partition_types);
321
322 # now check whether a level was none - need to set the equivalent level in the other half also to none
323 foreach my $metadata_group (@metadata_groups) {
324 if ($self->{$metadata_group . ".partition_type_within_level"} eq "none" || $self->{$metadata_group . ".numeric_partition_type_within_level"} eq "none") {
325
326 print STDERR "WARNING: one of -partition_type_within_level or -numeric_partition_type_within_level was set to 'none' for level $metadata_group, overriding current value of both these options to 'none'\n";
327
328 $self->{$metadata_group . ".partition_type_within_level"} = "none";
329 $self->{$metadata_group . ".numeric_partition_type_within_level"} = "none";
330 }
331 }
332
333 $self->set_metadata_groups_info_per_level("partition_size_within_level", $partition_size_default);
334 $self->set_metadata_groups_info_per_level("numeric_partition_size_within_level", $numeric_partition_size_default);
335
336 $self->set_metadata_groups_info_per_level('partition_sort_mode_within_level', $metadata_sort_mode_default, $valid_metadata_sort_modes);
337
338 $self->set_metadata_groups_info_per_level('numeric_partition_sort_mode_within_level', $metadata_sort_mode_default, $valid_metadata_sort_modes);
339
340 $self->set_metadata_groups_info_per_level("numeric_partition_name_length_within_level", $numeric_partition_name_length_default);
341
342 # The removeprefix and removesuffix expressions
343 if ($self->{'removeprefix'}) {
344 # If there are more than one expressions, use '' to quote each experession and '/' to separate
345 my @removeprefix_exprs_within_levellist = split(/'\/'/, $self->{'removeprefix'});
346
347 foreach my $metadata_group (@metadata_groups) {
348 my $removeprefix_expr_within_levelelem = shift(@removeprefix_exprs_within_levellist);
349 if (defined($removeprefix_expr_within_levelelem) && $removeprefix_expr_within_levelelem ne "") {
350 # Remove the other ' at the beginning and the end if there is any
351 $removeprefix_expr_within_levelelem =~ s/^'//;
352 $removeprefix_expr_within_levelelem =~ s/'$//;
353 # Remove the extra ^ at the beginning
354 $removeprefix_expr_within_levelelem =~ s/^\^//;
355 $self->{$metadata_group . ".remove_prefix_expr"} = $removeprefix_expr_within_levelelem;
356 } else {
357 $self->{$metadata_group . ".remove_prefix_expr"} = $self->{$metadata_groups[0] . ".remove_prefix_expr"};
358 }
359 }
360 }
361 if ($self->{'removesuffix'}) {
362 my @removesuffix_exprs_within_levellist = split(/'\/'/, $self->{'removesuffix'});
363
364 foreach my $metadata_group (@metadata_groups) {
365 my $removesuffix_expr_within_levelelem = shift(@removesuffix_exprs_within_levellist);
366 if (defined($removesuffix_expr_within_levelelem) && $removesuffix_expr_within_levelelem ne "") {
367 $removesuffix_expr_within_levelelem =~ s/^'//;
368 $removesuffix_expr_within_levelelem =~ s/'$//;
369 # Remove the extra $ at the end
370 $removesuffix_expr_within_levelelem =~ s/\$$//;
371 $self->{$metadata_group . ".remove_suffix_expr"} = $removesuffix_expr_within_levelelem;
372 } else {
373 $self->{$metadata_group . ".remove_suffix_expr"} = $self->{$metadata_groups[0] . ".remove_suffix_expr"};
374 }
375 }
376 }
377
378 # The metadata elements to use to sort the leaf nodes (default: Title)
379 my @sort_leaf_nodes_using_metadata_groups = split(/\|/, $self->{'sort_leaf_nodes_using'});
380 $self->{'sort_leaf_nodes_using_metadata_groups'} = \@sort_leaf_nodes_using_metadata_groups;
381
382 foreach my $sort_group (@sort_leaf_nodes_using_metadata_groups) {
383 # set metadata_select_type, if not already set - might be already set if the same group was used in -metadata
384 if (!defined $self->{$sort_group . ".metadata_selection_mode_within_level"}) {
385 $self->{$sort_group . ".metadata_selection_mode_within_level"} = $metadata_selection_mode_default;
386 }
387
388 }
389
390 my @leaf_nodes_sort_modes = split (/\|/, $self->{'sort_leaf_nodes_sort_mode'});
391 foreach my $sort_group (@sort_leaf_nodes_using_metadata_groups) {
392 my $leaf_sort_mode = shift(@leaf_nodes_sort_modes);
393 if (!defined $self->{$sort_group . ".metadata_sort_mode_within_level"}) {
394 if (defined $leaf_sort_mode && defined $valid_metadata_sort_modes->{$leaf_sort_mode}) {
395 $self->{$sort_group . ".metadata_sort_mode_within_level"} = $leaf_sort_mode;
396 }
397 else {
398 $self->{$sort_group . ".metadata_sort_mode_within_level"} = $metadata_sort_mode_default;
399 }
400 }
401
402 }
403
404 # Create an instance of the Unicode::Collate object if better Unicode sorting is desired
405 if ($self->{'sort_using_unicode_collation'}) {
406 # To use this you first need to download the allkeys.txt file from
407 # http://www.unicode.org/Public/UCA/latest/allkeys.txt and put it in the Perl
408 # Unicode/Collate directory.
409 require Unicode::Collate;
410 $self->{'unicode_collator'} = Unicode::Collate->new();
411 }
412
413 # An empty array for the document/section OIDs that we are classifying
414 $self->{'OIDs'} = [];
415 # A hash for all the doc ids that we have seen, so we don't classify something twice
416 $self->{'all_doc_OIDs'} = {};
417 return $self;
418}
419
420
421sub init
422{
423 # Nothing to do...
424}
425
426sub set_metadata_groups_info_per_level
427{
428 my $self = shift(@_);
429 my $info_name = shift(@_);
430 my $info_default = shift(@_);
431 my $info_valid_types_hash_ref = shift(@_);
432
433 if (!defined $self->{$info_name}) {
434 print STDERR "List Error: no values were set for option $info_name\n";
435 }
436 my @info_list = split(/\//, $self->{$info_name});
437
438 my $first = 1;
439 foreach my $metadata_group (@{$self->{'metadata_groups'}}) {
440 my $info_elem = shift(@info_list);
441 if (defined ($info_elem) && (!defined $info_valid_types_hash_ref || defined $info_valid_types_hash_ref->{$info_elem})) {
442 $self->{$metadata_group .".$info_name"} = $info_elem;
443 } else {
444 # its empty or an invalid entry
445 my $new_info_elem;
446 if ($first) {
447 $new_info_elem = $info_default;
448 } else {
449 # get the value we had at first
450 $new_info_elem = $self->{@{$self->{'metadata_groups'}}[0] . ".$info_name"};
451 }
452 $self->{$metadata_group .".$info_name"} = $new_info_elem;
453 if (defined $info_elem) {
454 print STDERR "List Error: $info_elem is not a valid value for $info_name, changing it to $new_info_elem\n";
455
456 }
457 }
458 $first = 0;
459 }
460
461}
462
463# Called for each document in the collection
464sub classify
465{
466 my $self = shift(@_);
467 my ($doc_obj) = @_;
468
469 if (defined $self->{'all_doc_OIDs'}->{$doc_obj->get_OID()}) {
470 print STDERR "Warning, List classifier has already seen document ".$doc_obj->get_OID().", not classifying again\n";
471 return;
472 }
473
474 $self->{'all_doc_OIDs'}->{$doc_obj->get_OID()} = 1;
475
476 # check against filter here
477 if ($self->{'filter_metadata'}) {
478 my $meta = $doc_obj->get_metadata_element($doc_obj->get_top_section(), $self->{'filter_metadata'});
479 return unless defined $meta;
480 if ($self->{'filter_regex'} ne "" && $meta !~ /$self->{'filter_regex'}/) {
481 print STDERR "doc $doc_obj doesn't pass filtering requirement\n" if ($self->{'verbosity'} > 3);
482 return;
483 }
484 }
485 # if we get here, we have passed the test for filtering
486 # If "-classify_sections" is set, classify every section of the document
487 if ($self->{'classify_sections'}) {
488 my $section = $doc_obj->get_next_section($doc_obj->get_top_section());
489 while (defined $section) {
490 $self->classify_section($doc_obj, $doc_obj->get_OID() . ".$section", $section);
491 $section = $doc_obj->get_next_section($section);
492 }
493 }
494 # Otherwise just classify the top document section
495 else {
496 $self->classify_section($doc_obj, $doc_obj->get_OID(), $doc_obj->get_top_section());
497 }
498
499}
500
501sub classify_section
502{
503 my $self = shift(@_);
504 my ($doc_obj,$section_OID,$section) = @_;
505
506 my @metadata_groups = @{$self->{'metadata_groups'}};
507
508 # Only classify the section if it has a value for one of the metadata elements in the first group
509 my $classify_section = 0;
510 my $first_metadata_group = $metadata_groups[0];
511 my $remove_prefix_expr = $self->{$first_metadata_group . ".remove_prefix_expr"};
512 my $remove_suffix_expr = $self->{$first_metadata_group . ".remove_suffix_expr"};
513 foreach my $first_metadata_group_element (split(/\;|,/, $first_metadata_group)) {
514 my $real_first_metadata_group_element = $self->strip_ex_from_metadata($first_metadata_group_element);
515 my $first_metadata_group_element_value = $doc_obj->get_metadata_element($section, $real_first_metadata_group_element);
516 # Remove prefix/suffix if requested
517 if (defined ($first_metadata_group_element_value)) {
518 if (defined $remove_prefix_expr && $remove_prefix_expr ne "") {
519 $first_metadata_group_element_value =~ s/^$remove_prefix_expr//;
520 }
521
522 if (defined $remove_suffix_expr && $remove_suffix_expr ne "") {
523 $first_metadata_group_element_value =~ s/$remove_suffix_expr$//;
524 }
525 }
526
527 $first_metadata_group_element_value = &sorttools::format_metadata_for_sorting($first_metadata_group, $first_metadata_group_element_value, $doc_obj) unless $self->{'no_metadata_formatting'};
528 if (defined($first_metadata_group_element_value) && $first_metadata_group_element_value ne "") {
529 # This section must be included in the classifier as we have found a value
530 $classify_section = 1;
531 last;
532 }
533 }
534
535 # We're not classifying this section because it doesn't have the required metadata
536 return if (!$classify_section);
537
538 # Otherwise, include this section in the classifier
539
540 push(@{$self->{'OIDs'}}, $section_OID);
541
542 # Create a hash for the metadata values of each metadata element we're interested in
543 my %metadata_groups_done = ();
544 foreach my $metadata_group (@metadata_groups, @{$self->{'sort_leaf_nodes_using_metadata_groups'}}) {
545 # Take care not to do a metadata group more than once
546 unless ($metadata_groups_done{$metadata_group}) {
547 my $remove_prefix_expr = $self->{$metadata_group . ".remove_prefix_expr"};
548 my $remove_suffix_expr = $self->{$metadata_group . ".remove_suffix_expr"};
549 foreach my $metadata_element (split(/\;|,/, $metadata_group)) {
550 my $real_metadata_element = $self->strip_ex_from_metadata($metadata_element);
551
552 my @metadata_values = @{$doc_obj->get_metadata($section, $real_metadata_element)};
553 foreach my $metadata_value (@metadata_values) {
554 #print STDERR "working with value $metadata_value\n";
555 # Strip leading and trailing whitespace
556 $metadata_value =~ s/^\s*//;
557 $metadata_value =~ s/\s*$//;
558
559 # Remove prefix/suffix if requested
560 if (defined $remove_prefix_expr && $remove_prefix_expr ne "") {
561 $metadata_value =~ s/^$remove_prefix_expr//;
562 }
563 if (defined $remove_suffix_expr && $remove_suffix_expr ne "") {
564 $metadata_value =~ s/$remove_suffix_expr$//;
565 }
566
567 # lowercase metadata both for sorting meta (d/D under D), and to allow CSS to be able to
568 # text-transform the stored lowercase values as capitalize or uppercase (can't CSS
569 # text-transform if stored uppercase). 2 CSS text-transforms have been added to core.css
570 my $lc_metadata_value = lc($metadata_value);
571 $lc_metadata_value = &sorttools::format_metadata_for_sorting($real_metadata_element, $lc_metadata_value, $doc_obj) unless $self->{'no_metadata_formatting'};
572
573 # Add the metadata value into the list for this combination of metadata group
574 # and section - if we have some non-whitespace chars
575 # text that we have some non-whitespace chars
576 if ($lc_metadata_value =~ /\S/) {
577
578 push(@{$self->{$metadata_group . ".list"}->{$section_OID}}, $lc_metadata_value);
579
580 # add the actual value into the stored values so we can remember the case
581 if (!$self->{'standardize_capitalization'}) {
582 if (defined $self->{$metadata_group . ".actualvalues"}->{$lc_metadata_value}->{$metadata_value}) {
583 $self->{$metadata_group . ".actualvalues"}->{$lc_metadata_value}->{$metadata_value}++;
584 } else {
585 $self->{$metadata_group . ".actualvalues"}->{$lc_metadata_value}->{$metadata_value} = 1;
586 }
587 }
588 last if ($self->{$metadata_group . ".metadata_selection_mode_within_level"} eq "firstvalue");
589 }
590 } # foreach metadatavalue
591 last if ((@metadata_values > 0) && $self->{$metadata_group . ".metadata_selection_mode_within_level"} =~ /^(firstvalue|firstvalidmetadata)$/ );
592 } # foreach metadata element
593
594 $metadata_groups_done{$metadata_group} = 1;
595 }
596 }
597}
598
599
600sub get_classify_info
601{
602 my $self = shift(@_);
603
604 # The metadata groups to classify by
605 my @metadata_groups = @{$self->{'metadata_groups'}};
606 my $first_metadata_group = $metadata_groups[0];
607
608 # The OID values of the documents to include in the classifier
609 my @OIDs = @{$self->{'OIDs'}};
610
611 # Create the root node of the classification hierarchy
612 my %classifier_node = ( 'thistype' => "Invisible",
613 'childtype' => "VList",
614 'Title' => $self->{'buttonname'},
615 'contains' => [],
616 'mdtype' => $first_metadata_group );
617
618 # Recursively create the classification hierarchy, one level for each metadata group
619 $self->add_level(\@metadata_groups, \@OIDs, \%classifier_node);
620 return \%classifier_node;
621}
622
623
624sub add_level
625{
626 my $self = shift(@_);
627 my @metadata_groups = @{shift(@_)};
628 my @OIDs = @{shift(@_)};
629 my $classifier_node = shift(@_);
630
631 my $metadata_group = $metadata_groups[0];
632 if (!defined($self->{$metadata_group . ".list"})) {
633 print STDERR "Warning: No metadata values assigned to $metadata_group.\n";
634 return;
635 }
636
637 # Create a mapping from metadata value to OID
638 my $OID_to_metadata_values_hash_ref = $self->{$metadata_group . ".list"};
639 my %metadata_value_to_OIDs_hash = ();
640 my %numeric_metadata_value_to_OIDs_hash = ();
641 foreach my $OID (@OIDs)
642 {
643 if ($OID_to_metadata_values_hash_ref->{$OID})
644 {
645 my @metadata_values = @{$OID_to_metadata_values_hash_ref->{$OID}};
646 foreach my $metadata_value (@metadata_values)
647 {
648 if ($metadata_value =~ /^[0-9]/) {
649 push(@{$numeric_metadata_value_to_OIDs_hash{$metadata_value}}, $OID);
650 } else {
651 push(@{$metadata_value_to_OIDs_hash{$metadata_value}}, $OID);
652 }
653 }
654 }
655 }
656 #print STDERR "Number of distinct values: " . scalar(keys %metadata_value_to_OIDs_hash) . "\n";
657 #print STDERR "Number of distinct numeric values: " . scalar(keys %numeric_metadata_value_to_OIDs_hash) . "\n";
658
659 # Partition the values (if necessary)
660 my $partition_type_within_level = $self->{$metadata_group . ".partition_type_within_level"};
661 my $partition_size_within_level = $self->{$metadata_group . ".partition_size_within_level"};
662 my $partition_sort_mode_within_level = $self->{$metadata_group . ".partition_sort_mode_within_level"};
663 my $bookshelf_type_within_level = $self->{$metadata_group. ".bookshelf_type"};
664
665
666 #############################################
667 ### DO THE NUMBERS IF THEY ARE TO COME FIRST
668 #############################################
669 if ($self->{'numbers_first'} && keys(%numeric_metadata_value_to_OIDs_hash)) {
670 $self->partition_numeric_values(\@metadata_groups, $classifier_node, \%numeric_metadata_value_to_OIDs_hash);
671 }
672
673 ############################################
674 # DO THE LETTERS
675 ############################################
676 if (keys(%metadata_value_to_OIDs_hash)){ # make sure we have some values
677 if ($partition_type_within_level =~ /^per_letter$/i) {
678 $self->split_per_letter_or_digit(\@metadata_groups, $classifier_node, $partition_sort_mode_within_level, \%metadata_value_to_OIDs_hash);
679 }
680 elsif ($partition_type_within_level =~ /^approximate_size$/i && scalar(keys %metadata_value_to_OIDs_hash) > $partition_size_within_level) {
681 $self->split_approximate_size(\@metadata_groups, $classifier_node, $partition_size_within_level, $partition_sort_mode_within_level, $bookshelf_type_within_level, \%metadata_value_to_OIDs_hash, $self->{'partition_name_length'});
682
683 }
684 elsif ($partition_type_within_level =~ /^constant_size$/i && scalar(keys %metadata_value_to_OIDs_hash) > $partition_size_within_level) {
685 $self->split_constant_size(\@metadata_groups, $classifier_node, $partition_size_within_level, $partition_sort_mode_within_level, $bookshelf_type_within_level, \%metadata_value_to_OIDs_hash, $self->{'partition_name_length'});
686 }
687
688 # Otherwise just add all the values to a VList
689 else {
690 if ($partition_type_within_level =~ /^all_values$/i) {
691 $classifier_node->{'childtype'} = "HList";
692 $self->{$metadata_group. ".bookshelf_type"} = "always";
693 }
694 $self->add_vlist(\@metadata_groups, $classifier_node, \%metadata_value_to_OIDs_hash);
695 }
696 }
697
698 ###########################################
699 ### DO THE NUMBERS IF THEY ARE TO COME LAST
700 ###########################################
701 if (!$self->{'numbers_first'} && keys(%numeric_metadata_value_to_OIDs_hash)) {
702 $self->partition_numeric_values(\@metadata_groups, $classifier_node, \%numeric_metadata_value_to_OIDs_hash);
703 }
704}
705
706sub partition_numeric_values
707{
708 my $self = shift(@_);
709 my @metadata_groups = @{shift(@_)};
710 my $classifier_node = shift(@_);
711 my $numeric_metadata_value_to_OIDs_hash_ref = shift(@_);
712
713 my $metadata_group = $metadata_groups[0];
714 my $numeric_partition_type_within_level = $self->{$metadata_group . ".numeric_partition_type_within_level"};
715 my $numeric_partition_size_within_level = $self->{$metadata_group . ".numeric_partition_size_within_level"};
716 my $numeric_partition_sort_mode_within_level = $self->{$metadata_group . ".numeric_partition_sort_mode_within_level"};
717 my $numeric_partition_name_length_within_level = $self->{$metadata_group . ".numeric_partition_name_length_within_level"};
718 my $bookshelf_type_within_level = $self->{$metadata_group. ".bookshelf_type"};
719
720 if ($numeric_partition_type_within_level eq "single_partition") {
721 $self->add_hlist_partition(\@metadata_groups, $classifier_node, "0-9", $numeric_metadata_value_to_OIDs_hash_ref);
722 }
723 elsif ($numeric_partition_type_within_level eq "per_digit") {
724 $self->split_per_letter_or_digit(\@metadata_groups, $classifier_node, $numeric_partition_sort_mode_within_level, $numeric_metadata_value_to_OIDs_hash_ref, 1, 1);
725 }
726 elsif ($numeric_partition_type_within_level eq "per_number") {
727 # each different number is a bucket
728 $self->split_per_letter_or_digit(\@metadata_groups, $classifier_node, $numeric_partition_sort_mode_within_level, $numeric_metadata_value_to_OIDs_hash_ref, 1, $numeric_partition_name_length_within_level);
729 }
730 elsif ($numeric_partition_type_within_level eq "constant_size" && scalar(keys %$numeric_metadata_value_to_OIDs_hash_ref) > $numeric_partition_size_within_level) {
731 # Generate hlists of a certain size
732
733 $self->split_constant_size(\@metadata_groups, $classifier_node, $numeric_partition_size_within_level, $numeric_partition_sort_mode_within_level, $bookshelf_type_within_level, $numeric_metadata_value_to_OIDs_hash_ref, $numeric_partition_name_length_within_level, 1);
734 } elsif ($numeric_partition_type_within_level eq "approximate_size" && scalar(keys %$numeric_metadata_value_to_OIDs_hash_ref) > $numeric_partition_size_within_level) {
735 $self->split_approximate_size(\@metadata_groups, $classifier_node, $numeric_partition_size_within_level, $numeric_partition_sort_mode_within_level, $bookshelf_type_within_level, $numeric_metadata_value_to_OIDs_hash_ref, $numeric_partition_name_length_within_level, 1);
736 }
737 # Otherwise just add all the values to a VList
738 else {
739 if ($numeric_partition_type_within_level =~ /^all_values$/i) {
740 $classifier_node->{'childtype'} = "HList";
741 $self->{$metadata_group. ".bookshelf_type"} = "always";
742 }
743
744 $self->add_vlist(\@metadata_groups, $classifier_node, $numeric_metadata_value_to_OIDs_hash_ref);
745 }
746
747}
748
749sub split_approximate_size
750{
751
752 my $self = shift(@_);
753 my @metadata_groups = @{shift(@_)};
754 my $classifier_node = shift(@_);
755 my $partition_size = shift(@_);
756 my $sort_mode = shift(@_);
757 my $bookshelf_type = shift(@_);
758 my $metadata_value_to_OIDs_hash_ref = shift(@_);
759 my $partition_name_length = shift(@_);
760 my $is_numeric = shift(@_);
761
762 # Generate hlist based on the first letter of the metadata value (like per_letter), or based on
763 # numbers, but also with restriction on the partition size
764 # If a partition has fewer items than specified by the "partition_size_within_level", then group them together if possible
765 # If a partition has more items than specified, split into several hlists.
766 # Depends on the bookshelf_type, one item can be either a document (when bookshelf_type is "never") or a metadata value (otherwise)
767
768 my @sortedmetadata_values = $self->sort_metadata_values_array($sort_mode, keys(%$metadata_value_to_OIDs_hash_ref));
769
770 # Separate values by their first letter, each form a bucket, like the per_letter partition type
771 my $last_partition = $self->generate_partition_name($sortedmetadata_values[0], $partition_name_length, $is_numeric);
772
773 my @partition_buckets = ();
774 my @metadata_values_in_bucket = ();
775 my $num_items_in_bucket = 0;
776
777 foreach my $metadata_value (@sortedmetadata_values) {
778 my $metadata_valuepartition = $self->generate_partition_name($metadata_value, $partition_name_length, $is_numeric);
779 if ($metadata_valuepartition ne $last_partition) {
780 my @temp_array = @metadata_values_in_bucket;
781 # 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
782 my %partition_info = ();
783 $partition_info{'metadata_values'} = \@temp_array;
784 $partition_info{'size'} = $num_items_in_bucket;
785 $partition_info{'name'} = $last_partition;
786 push (@partition_buckets, \%partition_info);
787
788 @metadata_values_in_bucket = ($metadata_value);
789 $num_items_in_bucket = ($bookshelf_type eq "never" ? scalar(@{$metadata_value_to_OIDs_hash_ref->{$metadata_value}}) : scalar(@metadata_values_in_bucket));
790 $last_partition = $metadata_valuepartition;
791 } else {
792 $num_items_in_bucket += ($bookshelf_type eq "never" ? scalar(@{$metadata_value_to_OIDs_hash_ref->{$metadata_value}}) : 1); #scalar(@metadata_values_in_bucket);
793 push (@metadata_values_in_bucket, $metadata_value);
794 }
795 }
796 # Last one
797 my %partition_info = ();
798 $partition_info{'metadata_values'} = \@metadata_values_in_bucket;
799 $partition_info{'size'} = $num_items_in_bucket;
800 $partition_info{'name'} = $last_partition;
801
802 push (@partition_buckets, \%partition_info);
803
804 # now go through the array of buckets, and merge small buckets
805 my @new_partition_buckets = ();
806 for (my $i = 0; $i < scalar(@partition_buckets) - 1; $i++) {
807
808 my $this_bucket = $partition_buckets[$i];
809 my $next_bucket = $partition_buckets[$i+1];
810
811 my $items_in_partition = $this_bucket->{'size'};
812
813 if ($items_in_partition < $partition_size ) {
814 my $items_in_next_partition = $next_bucket->{'size'};
815 if ($items_in_partition + $items_in_next_partition <= $partition_size ) {
816 # merge this bucket into the next bucket
817 foreach my $metadata_value_to_merge (@{$this_bucket->{'metadata_values'}}) {
818 push(@{$next_bucket->{'metadata_values'}}, $metadata_value_to_merge);
819 }
820 $next_bucket->{'size'} += $items_in_partition;
821
822 } else {
823 # remember this bucket
824 push (@new_partition_buckets, $this_bucket);
825 }
826 } else {
827 # remember this bucket
828 push (@new_partition_buckets, $this_bucket);
829 }
830 }
831 # add in the last bucket
832 my $last_bucket = $partition_buckets[scalar(@partition_buckets) - 1];
833 push (@new_partition_buckets, $last_bucket);
834
835 # Add partitions to the main list, but divide big bucket into several
836 my $last_partition_end = "";
837 my $partition_start = "";
838 my $partition_end = "";
839 my $partition_name = "";
840 foreach my $partition (@new_partition_buckets) {
841 my @metadata_values = $self->sort_metadata_values_array($sort_mode, @{$partition->{'metadata_values'}});
842 my $items_in_partition = $partition->{'size'};
843 $partition_start = $self->generate_partition_start($metadata_values[0], $last_partition_end, $partition_name_length, $is_numeric);
844
845 if ($items_in_partition <= $partition_size) {
846 # we can just add the partition as is
847 my %metadata_values_to_OIDs_subhashes = ();
848 for (my $i = 0; $i < scalar(@metadata_values); $i++) {
849 my $metadata_value = $metadata_values[$i];
850 $metadata_values_to_OIDs_subhashes{$metadata_value} = $metadata_value_to_OIDs_hash_ref->{$metadata_value};
851 }
852 my $last_metadata_value = $metadata_values[scalar(@metadata_values)-1];
853 $partition_end = $self->generate_partition_end($last_metadata_value, $partition_start, $partition_name_length, $is_numeric);
854 $partition_name = $partition_start;
855 if ($partition_end ne $partition_start) {
856 $partition_name = $partition_name . "-" . $partition_end;
857 }
858 $self->add_hlist_partition(\@metadata_groups, $classifier_node, $partition_name, \%metadata_values_to_OIDs_subhashes);
859 $last_partition_end = $partition_end;
860 } else {
861 # we have too many items, need to split the partition
862 my $items_done = 0;
863 my %metadata_values_to_OIDs_subhashes = ();
864 for (my $i = 0; $i < scalar(@metadata_values); $i++) {
865 my $metadata_value = $metadata_values[$i];
866 # If the bookshelf_type is "never", count the documents, otherwise count the distinct metadata values
867 my $items_for_this_md_value = $bookshelf_type eq "never" ? scalar(@{$metadata_value_to_OIDs_hash_ref->{$metadata_value}}) : 1;
868
869 if ($items_done + $items_for_this_md_value > $partition_size && $items_done != 0) {
870 # Save the stored items into a partition
871 $self->add_hlist_partition(\@metadata_groups, $classifier_node, $partition_name, \%metadata_values_to_OIDs_subhashes);
872 $last_partition_end = $partition_end;
873 $partition_start = $self->generate_partition_start($metadata_value, $last_partition_end, $partition_name_length, $is_numeric);
874 $items_done = 0;
875 %metadata_values_to_OIDs_subhashes = ();
876 }
877
878 # If bookshelf_type is "never" and the current metadata value holds too many items, need to split into several partitions
879 if ($bookshelf_type eq "never" && $items_for_this_md_value > $partition_size) {
880
881 my $partitionname_for_this_value = $self->generate_partition_start($metadata_value, $last_partition_end, $partition_name_length, $is_numeric);
882 # Get the number of partitions needed for this value
883 my $num_splits = int($items_for_this_md_value / $partition_size);
884 $num_splits++ if ($items_for_this_md_value / $partition_size > $num_splits);
885 my @OIDs_for_this_value = @{$metadata_value_to_OIDs_hash_ref->{$metadata_value}};
886 for (my $i = 0; $i < $num_splits; $i++) {
887 my %OIDs_subhashes_for_this_value = ();
888 my @OIDs_for_this_partition = ();
889 for (my $d = $i * $partition_size; $d < (($i+1) * $partition_size > $items_for_this_md_value ? $items_for_this_md_value : ($i+1) * $partition_size); $d++) {
890 push (@OIDs_for_this_partition, $OIDs_for_this_value[$d]);
891 }
892
893 # The last bucket might have only a few items and need to be merged with buckets for subsequent metadata values
894 if ($i == $num_splits - 1 && scalar(@OIDs_for_this_partition) < $partition_size) {
895 $partition_start = $partitionname_for_this_value;
896 $partition_name = $partition_start;
897 $metadata_values_to_OIDs_subhashes{$metadata_value} = \@OIDs_for_this_partition;
898 $items_done += scalar(@OIDs_for_this_partition);
899 $last_partition_end = $partitionname_for_this_value
900 } else {
901
902 # Add an HList for this bucket
903 $OIDs_subhashes_for_this_value{$metadata_value} = \@OIDs_for_this_partition;
904 $self->add_hlist_partition(\@metadata_groups, $classifier_node, $partitionname_for_this_value, \%OIDs_subhashes_for_this_value);
905 $last_partition_end = $partitionname_for_this_value;
906 }
907 }
908 } else {
909
910 $metadata_values_to_OIDs_subhashes{$metadata_value} = $metadata_value_to_OIDs_hash_ref->{$metadata_value};
911 $items_done += $bookshelf_type eq "never" ? scalar(@{$metadata_values_to_OIDs_subhashes{$metadata_value}}) : 1;
912 $partition_end = $self->generate_partition_end($metadata_value, $partition_start, $partition_name_length, $is_numeric);
913 $partition_name = $partition_start;
914 if ($partition_end ne $partition_start) {
915 $partition_name = $partition_name . "-" . $partition_end;
916 }
917
918 }
919
920 # The last partition
921 if($i == scalar(@metadata_values) - 1 && $items_done >0) {
922 $self->add_hlist_partition(\@metadata_groups, $classifier_node, $partition_name, \%metadata_values_to_OIDs_subhashes);
923 }
924
925 }
926 } # end if items in partition > partition size
927
928 }
929
930 # The partitions are stored in an HList
931 $classifier_node->{'childtype'} = "HList";
932
933}
934
935sub split_constant_size
936{
937 my $self = shift(@_);
938 my @metadata_groups = @{shift(@_)};
939 my $classifier_node = shift(@_);
940 my $partition_size = shift(@_);
941 my $sort_mode = shift(@_);
942 my $bookshelf_type = shift(@_);
943 my $metadata_value_to_OIDs_hash_ref = shift(@_);
944 my $partition_name_length = shift(@_);
945 my $is_numeric = shift(@_);
946
947 my @sortedmetadata_values = $self->sort_metadata_values_array($sort_mode, keys(%$metadata_value_to_OIDs_hash_ref));
948 my $items_in_partition = 0;
949 my %metadata_value_to_OIDs_subhash = ();
950 my $lastpartitionend = "";
951 my $partitionstart;
952
953 foreach my $metadata_value (@sortedmetadata_values) {
954 if ($items_in_partition == 0) {
955 # a new partition, set the name
956 $partitionstart = $self->generate_partition_start($metadata_value, $lastpartitionend, $partition_name_length, $is_numeric);
957 }
958 my $numitems_for_this_value = ($bookshelf_type eq "never" ? scalar(@{$metadata_value_to_OIDs_hash_ref->{$metadata_value}}) : 1);
959 if ($items_in_partition + $numitems_for_this_value <= $partition_size) {
960 # add all the current values into the temporary list
961 $metadata_value_to_OIDs_subhash{$metadata_value} = $metadata_value_to_OIDs_hash_ref->{$metadata_value};
962 $items_in_partition += $numitems_for_this_value;
963 } elsif ($items_in_partition < $partition_size) {
964 # only want to add some of the values into temporary list
965 # note, we only get here if bookshelf type is never
966 my @OIDs = @{$metadata_value_to_OIDs_hash_ref->{$metadata_value}};
967 @OIDs = $self->sort_leaf_items(\@OIDs);
968 my $num_items_needed = $partition_size - $items_in_partition;
969 my @slice = splice(@OIDs, 0, $num_items_needed);
970 $metadata_value_to_OIDs_subhash{$metadata_value} = \@slice;
971
972 # now we have filled up the partition
973 my $partitionend = $self->generate_partition_end($metadata_value, $partitionstart, $partition_name_length, $is_numeric);
974 my $partitionname = $partitionstart;
975 if ($partitionend ne $partitionstart) {
976 $partitionname = $partitionname . "-" . $partitionend;
977 }
978
979 $self->add_hlist_partition(\@metadata_groups, $classifier_node, $partitionname, \%metadata_value_to_OIDs_subhash);
980 %metadata_value_to_OIDs_subhash = ();
981 $items_in_partition = 0;
982 $lastpartitionend = $partitionend;
983
984 # can we get more partitions from this metadata value?
985 while (scalar(@OIDs) >= $partition_size) {
986 my @slice = splice(@OIDs, 0, $partition_size);
987 $metadata_value_to_OIDs_subhash{$metadata_value} = \@slice;
988 $partitionstart = $self->generate_partition_start($metadata_value, $lastpartitionend, $partition_name_length, $is_numeric);
989 my $partitionend = $self->generate_partition_end($metadata_value, $partitionstart, $partition_name_length, $is_numeric);
990 my $partitionname = $partitionstart;
991 if ($partitionend ne $partitionstart) {
992 $partitionname = $partitionname . "-" . $partitionend;
993 }
994 $self->add_hlist_partition(\@metadata_groups, $classifier_node, $partitionname, \%metadata_value_to_OIDs_subhash);
995 %metadata_value_to_OIDs_subhash = ();
996 $items_in_partition = 0;
997 $lastpartitionend = $partitionend;
998
999 }
1000 if (scalar(@OIDs) > 0) {
1001 $metadata_value_to_OIDs_subhash{$metadata_value} = \@OIDs;
1002 $items_in_partition = scalar(@OIDs);
1003 $partitionstart = $self->generate_partition_start($metadata_value, $lastpartitionend, $partition_name_length, $is_numeric);
1004 }
1005
1006
1007 }
1008
1009 if ($items_in_partition == $partition_size) {
1010 # its the end of a partition
1011 my $partitionend = $self->generate_partition_end($metadata_value, $partitionstart, $partition_name_length, $is_numeric);
1012 my $partitionname = $partitionstart;
1013 if ($partitionend ne $partitionstart) {
1014 $partitionname = $partitionname . "-" . $partitionend;
1015 }
1016
1017 $self->add_hlist_partition(\@metadata_groups, $classifier_node, $partitionname, \%metadata_value_to_OIDs_subhash);
1018 %metadata_value_to_OIDs_subhash = ();
1019 $items_in_partition = 0;
1020 $lastpartitionend = $partitionend;
1021 }
1022 } # foreach metadata value
1023
1024 if ($items_in_partition > 0) {
1025 # we have to add the last partition
1026 my $partitionend = $self->generate_partition_end(@sortedmetadata_values[@sortedmetadata_values-1], $partitionstart, $partition_name_length, $is_numeric);
1027 my $partitionname = $partitionstart;
1028 if ($partitionend ne $partitionstart) {
1029 $partitionname = $partitionname . "-" . $partitionend;
1030 }
1031
1032 $self->add_hlist_partition(\@metadata_groups, $classifier_node, $partitionname, \%metadata_value_to_OIDs_subhash);
1033 }
1034
1035 # The partitions are stored in an HList
1036 $classifier_node->{'childtype'} = "HList";
1037
1038}
1039
1040sub split_per_letter_or_digit
1041{
1042 my $self = shift(@_);
1043 my @metadata_groups = @{shift(@_)};
1044 my $classifier_node = shift(@_);
1045 my $sort_mode = shift(@_);
1046 my $metadata_value_to_OIDs_hash_ref = shift(@_);
1047 my $is_numeric = shift(@_);
1048 my $numeric_partition_length = shift(@_);
1049
1050 if (not defined $is_numeric) {
1051 $is_numeric = 0;
1052 }
1053 if ($is_numeric && not defined($numeric_partition_length)) {
1054 $numeric_partition_length = 1;
1055 }
1056 # Generate one hlist for each letter
1057 my @sortedmetadata_values = $self->sort_metadata_values_array($sort_mode, keys(%$metadata_value_to_OIDs_hash_ref));
1058 my %metadata_value_to_OIDs_subhash = ();
1059
1060 my $lastpartition = $self->generate_partition_name($sortedmetadata_values[0], $numeric_partition_length, $is_numeric);
1061 foreach my $metadata_value (@sortedmetadata_values) {
1062
1063 my $metadata_valuepartition = $self->generate_partition_name($metadata_value, $numeric_partition_length, $is_numeric);
1064
1065 # Is this the start of a new partition?
1066 if ($metadata_valuepartition ne $lastpartition) {
1067 $self->add_hlist_partition(\@metadata_groups, $classifier_node, $lastpartition, \%metadata_value_to_OIDs_subhash);
1068 %metadata_value_to_OIDs_subhash = ();
1069 $lastpartition = $metadata_valuepartition;
1070 }
1071
1072 $metadata_value_to_OIDs_subhash{$metadata_value} = $metadata_value_to_OIDs_hash_ref->{$metadata_value};
1073 }
1074
1075 # Don't forget to add the last partition
1076 $self->add_hlist_partition(\@metadata_groups, $classifier_node, $lastpartition, \%metadata_value_to_OIDs_subhash);
1077
1078 # The partitions are stored in an HList
1079 $classifier_node->{'childtype'} = "HList";
1080
1081}
1082
1083sub generate_partition_name
1084{
1085 my $self = shift(@_);
1086 my $mvalue = shift(@_);
1087 my $numeric_partition_length = shift(@_);
1088 my $is_numeric = shift(@_);
1089
1090 if (!$is_numeric || $numeric_partition_length == 1 ) {
1091 return substr($mvalue, 0, 1);
1092 }
1093 if ($numeric_partition_length == -1) {
1094 my ($all_digits) = $mvalue =~ /^([0-9]+)/;
1095 return $all_digits;
1096 }
1097 my ($some_digits) = $mvalue =~ /^([0-9]{1,$numeric_partition_length})/;
1098 return $some_digits;
1099}
1100
1101sub generate_partition_start
1102{
1103 my $self = shift(@_);
1104 my $metadata_value = shift(@_);
1105 my $lastpartitionend = shift(@_);
1106 my $partition_name_length = shift(@_);
1107 my $is_numeric = shift(@_);
1108
1109 if ($is_numeric) {
1110 return $self->generate_partition_name($metadata_value, $partition_name_length, $is_numeric);
1111 }
1112
1113 if ($partition_name_length) {
1114 return substr($metadata_value, 0, $partition_name_length);
1115 }
1116
1117 my $partitionstart = substr($metadata_value, 0, 1);
1118 if ($partitionstart le $lastpartitionend) {
1119 $partitionstart = substr($metadata_value, 0, 2);
1120 # Give up after three characters
1121 if ($partitionstart le $lastpartitionend) {
1122 $partitionstart = substr($metadata_value, 0, 3);
1123 }
1124 }
1125
1126 return $partitionstart;
1127}
1128
1129
1130sub generate_partition_end
1131{
1132 my $self = shift(@_);
1133 my $metadata_value = shift(@_);
1134 my $partitionstart = shift(@_);
1135 my $partition_name_length = shift(@_);
1136 my $is_numeric = shift(@_);
1137
1138 if ($is_numeric) {
1139 return $self->generate_partition_name($metadata_value, $partition_name_length, $is_numeric);
1140 }
1141 if ($partition_name_length) {
1142 return substr($metadata_value, 0, $partition_name_length);
1143 }
1144
1145 my $partitionend = substr($metadata_value, 0, length($partitionstart));
1146 if ($partitionend gt $partitionstart) {
1147 $partitionend = substr($metadata_value, 0, 1);
1148 if ($partitionend le $partitionstart) {
1149 $partitionend = substr($metadata_value, 0, 2);
1150 # Give up after three characters
1151 if ($partitionend le $partitionstart) {
1152 $partitionend = substr($metadata_value, 0, 3);
1153 }
1154 }
1155 }
1156
1157 return $partitionend;
1158}
1159
1160
1161sub add_hlist_partition
1162{
1163 my $self = shift(@_);
1164 my @metadata_groups = @{shift(@_)};
1165 my $classifier_node = shift(@_);
1166 my $partitionname = shift(@_);
1167 my $metadata_value_to_OIDs_hash_ref = shift(@_);
1168
1169 # Create an hlist partition
1170 # Note that we don't need to convert from unicode-aware strings
1171 # to utf8 here, as that is handled elsewhere in the code
1172 my %child_classifier_node = ( 'Title' => $partitionname, #'Title' => $self->convert_unicode_string_to_utf8_string($partitionname),
1173 'childtype' => "VList",
1174 'contains' => [] );
1175
1176 # Add the children to the hlist partition
1177 $self->add_vlist(\@metadata_groups, \%child_classifier_node, $metadata_value_to_OIDs_hash_ref);
1178 push(@{$classifier_node->{'contains'}}, \%child_classifier_node);
1179}
1180
1181
1182sub add_vlist
1183{
1184 my $self = shift(@_);
1185 my @metadata_groups = @{shift(@_)};
1186 my $classifier_node = shift(@_);
1187 my $metadata_value_to_OIDs_hash_ref = shift(@_);
1188 my $metadata_group = shift(@metadata_groups);
1189 $classifier_node->{'mdtype'} = $metadata_group;
1190
1191 my $sort_type = $self->{$metadata_group .".metadata_sort_mode_within_level"};
1192 # Create an entry in the vlist for each value
1193 foreach my $metadata_value ($self->sort_metadata_values_array($sort_type, keys(%{$metadata_value_to_OIDs_hash_ref})))
1194 {
1195 my @OIDs = @{$metadata_value_to_OIDs_hash_ref->{$metadata_value}};
1196
1197 # If there is only one item and 'bookshelf_type' is not always (ie. never or duplicate_only), add the item to the list
1198 if (@OIDs == 1 && $self->{$metadata_group . ".bookshelf_type"} ne "always") {
1199 my $OID = $OIDs[0];
1200 my $offset = $self->metadata_offset($metadata_group, $OID, $metadata_value);
1201 push(@{$classifier_node->{'contains'}}, { 'OID' => $OID, 'offset' => $offset });
1202 }
1203 # If 'bookshelf_type' is 'never', list all the items even if there are duplicated values
1204 elsif ($self->{$metadata_group . ".bookshelf_type"} eq "never") {
1205
1206 @OIDs = $self->sort_leaf_items(\@OIDs);
1207 foreach my $OID (@OIDs) {
1208 my $offset = $self->metadata_offset($metadata_group, $OID, $metadata_value);
1209 push(@{$classifier_node->{'contains'}}, { 'OID' => $OID , 'offset' => $offset });
1210 }
1211
1212 }
1213 # Otherwise create a sublist (bookshelf) for the metadata value
1214 else {
1215 my $metadata_value_display = $self->get_metadata_value_display($metadata_group, $metadata_value);
1216 my %child_classifier_node = ( 'Title' => $metadata_value_display,
1217 'childtype' => "VList",
1218 'mdtype' => $metadata_group,
1219 'contains' => [] );
1220
1221 # If there are metadata elements remaining, recursively apply the process
1222 if (@metadata_groups > 0) {
1223 my $next_metadata_group = $metadata_groups[0];
1224
1225 # separate metadata into those that belong in the next/sub-metadata_group
1226 # and those that belong at the current level's metadata_group
1227
1228 my $OID_to_metadata_values_hash_ref = $self->{$next_metadata_group . ".list"};
1229 my @current_level_OIDs = ();
1230 my @next_level_OIDs = ();
1231 foreach my $OID (@OIDs)
1232 {
1233 if ($OID_to_metadata_values_hash_ref->{$OID}) {
1234 push(@next_level_OIDs, $OID);
1235 } else {
1236 push(@current_level_OIDs, $OID);
1237 }
1238 }
1239 # recursively process those docs belonging to the sub-metadata_group
1240 $self->add_level(\@metadata_groups, \@next_level_OIDs, \%child_classifier_node);
1241
1242 # For those docs that don't belong in the sub/next_metadata_group, but which belong
1243 # at this level, just add the documents as children of this list at the current level
1244 @current_level_OIDs = $self->sort_leaf_items(\@current_level_OIDs);
1245 foreach my $current_level_OID (@current_level_OIDs) {
1246 my $offset = $self->metadata_offset($metadata_group, $current_level_OID, $metadata_value);
1247 push(@{$child_classifier_node{'contains'}}, { 'OID' => $current_level_OID , 'offset' => $offset });
1248 }
1249 }
1250 # Otherwise just add the documents as children of this list
1251 else {
1252 @OIDs = $self->sort_leaf_items(\@OIDs);
1253 foreach my $OID (@OIDs) {
1254 my $offset = $self->metadata_offset($metadata_group, $OID, $metadata_value);
1255 push(@{$child_classifier_node{'contains'}}, { 'OID' => $OID , 'offset' => $offset });
1256 }
1257
1258 }
1259
1260 # Add the sublist to the list
1261 push(@{$classifier_node->{'contains'}}, \%child_classifier_node);
1262 }
1263 }
1264}
1265
1266sub metadata_offset
1267{
1268 my $self = shift(@_);
1269 my $metadata_group = shift(@_);
1270 my $OID = shift(@_);
1271 my $metadata_value = shift(@_);
1272
1273 my $OID_to_metadata_values_hash_ref = $self->{$metadata_group . ".list"};
1274 my @metadata_values = @{$OID_to_metadata_values_hash_ref->{$OID}};
1275 for (my $i = 0; $i < scalar(@metadata_values); $i++) {
1276 if ($metadata_value eq $metadata_values[$i]) {
1277 return $i;
1278 }
1279 }
1280
1281 return 0;
1282}
1283
1284sub sort_leaf_items
1285{
1286 my $self = shift(@_);
1287 my @OIDs = @{shift(@_)};
1288
1289 # Sort leaf nodes and add to list
1290 my @sort_leaf_nodes_using_metadata_groups = @{$self->{'sort_leaf_nodes_using_metadata_groups'}};
1291 foreach my $sort_leaf_nodes_usingmetaelem (reverse @sort_leaf_nodes_using_metadata_groups) {
1292 my $OID_to_metadata_values_hash_ref = $self->{$sort_leaf_nodes_usingmetaelem . ".list"};
1293 my $sort_type = $self->{$sort_leaf_nodes_usingmetaelem . ".metadata_sort_mode_within_level"};
1294 # Force a stable sort (Perl 5.6's sort isn't stable)
1295 # !! The [0] bits aren't ideal (multiple metadata values) !!
1296 @OIDs = @OIDs[ sort {
1297 if (defined($OID_to_metadata_values_hash_ref->{$OIDs[$a]}) && defined($OID_to_metadata_values_hash_ref->{$OIDs[$b]}))
1298 {
1299 if ($sort_type eq "numeric") {
1300 $OID_to_metadata_values_hash_ref->{$OIDs[$a]}[0] <=> $OID_to_metadata_values_hash_ref->{$OIDs[$b]}[0];
1301 } elsif ($sort_type eq "alphabetic") {
1302 $OID_to_metadata_values_hash_ref->{$OIDs[$a]}[0] cmp $OID_to_metadata_values_hash_ref->{$OIDs[$b]}[0];
1303 } else {
1304 ncmp($OID_to_metadata_values_hash_ref->{$OIDs[$a]}[0], $OID_to_metadata_values_hash_ref->{$OIDs[$b]}[0]);
1305 }
1306 }
1307 else
1308 {
1309 $a <=> $b;
1310 }
1311 } 0..$#OIDs ];
1312 }
1313 if ($self->{'reverse_sort_leaf_nodes'}) {
1314 return reverse @OIDs;
1315 }
1316 return @OIDs;
1317}
1318
1319
1320sub sort_metadata_values_array
1321{
1322 my $self = shift(@_);
1323 my ($sort_mode) = shift(@_);
1324 my @metadata_values = @_;
1325
1326 if ($sort_mode eq "unicode") {
1327 if ($self->{'unicode_collator'}) {
1328 return $self->{'unicode_collator'}->sort(@metadata_values);
1329 }
1330 # the collator wasn't loaded, fall back on default
1331 $sort_mode = "alphanumeric";
1332 }
1333 if ($sort_mode eq "numeric") {
1334 return sort {$a <=> $b} @metadata_values;
1335 }
1336 if ($sort_mode eq "alphabetic") {
1337 return sort {$a cmp $b} @metadata_values;
1338 }
1339 # natural sort
1340 return nsort(@metadata_values);
1341}
1342
1343
1344# we are not using this any more. Using nsort instead
1345# $a and $b args automatically passed in and shouldn't be declared
1346sub alpha_numeric_cmp
1347{
1348 my $self = shift (@_);
1349 my ($aStr, $bStr) = @_;
1350 if ($aStr =~ m/^(\d+(\.\d+)?)/)
1351 {
1352 my $val_a = $1;
1353 if ($bStr =~ m/^(\d+(\.\d+)?)/)
1354 {
1355 my $val_b = $1;
1356 if ($val_a != $val_b)
1357 {
1358 return ($val_a <=> $val_b);
1359 }
1360 }
1361 }
1362
1363 return ($aStr cmp $bStr);
1364}
1365
1366
1367sub get_metadata_value_display {
1368 my $self = shift(@_);
1369 my ($metadata_group, $metadata_value) = @_;
1370 return $metadata_value if $self->{'standardize_capitalization'};
1371 my $actual_values_hash = $self->{$metadata_group . ".actualvalues"}->{$metadata_value};
1372 my $display_value ="";
1373 my $max_count=0;
1374 foreach my $v (keys %$actual_values_hash) {
1375 if ($actual_values_hash->{$v} > $max_count) {
1376 $display_value = $v;
1377 $max_count = $actual_values_hash->{$v};
1378 }
1379 }
1380 return $display_value;
1381}
13821;
Note: See TracBrowser for help on using the repository browser.