source: main/trunk/greenstone2/perllib/classify/AZCompactList.pm@ 24691

Last change on this file since 24691 was 24691, checked in by davidb, 13 years ago

Improved multi-lingual support

  • Property svn:keywords set to Author Date Id Revision
File size: 23.9 KB
Line 
1###########################################################################
2#
3# AZCompactList.pm --
4# A component of the Greenstone digital library software
5# from the New Zealand Digital Library Project at the
6# University of Waikato, New Zealand.
7#
8# Copyright (C) 1999 New Zealand Digital Library Project
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
25# classifier plugin for sorting alphabetically
26
27package AZCompactList;
28
29use strict;
30no strict 'refs'; # allow filehandles to be variables and viceversa
31
32use BaseClassifier;
33use sorttools;
34
35use Unicode::Normalize;
36
37sub BEGIN {
38 @AZCompactList::ISA = ('BaseClassifier');
39}
40
41my $doclevel_list =
42 [ { 'name' => "top",
43 'desc' => "{AZCompactList.doclevel.top}" },
44 { 'name' => "firstlevel",
45 'desc' => "{AZCompactList.doclevel.firstlevel}" },
46 { 'name' => "section",
47 'desc' => "{AZCompactList.doclevel.section}" } ];
48
49my $arguments =
50 [ { 'name' => "metadata",
51 'desc' => "{AZCompactList.metadata}",
52 'type' => "metadata",
53 'reqd' => "yes" },
54 { 'name' => "firstvalueonly",
55 'desc' => "{AZCompactList.firstvalueonly}",
56 'type' => "flag",
57 'reqd' => "no" },
58 { 'name' => "allvalues",
59 'desc' => "{AZCompactList.allvalues}",
60 'type' => "flag",
61 'reqd' => "no" },
62 { 'name' => "sort",
63 'desc' => "{AZCompactList.sort}",
64 'type' => "metadata",
65# 'deft' => "Title",
66 'reqd' => "no" },
67 { 'name' => "removeprefix",
68 'desc' => "{BasClas.removeprefix}",
69 'type' => "regexp",
70 'deft' => "",
71 'reqd' => "no" },
72 { 'name' => "removesuffix",
73 'desc' => "{BasClas.removesuffix}",
74 'type' => "regexp",
75 'deft' => "",
76 'reqd' => "no" },
77 { 'name' => "mingroup",
78 'desc' => "{AZCompactList.mingroup}",
79 'type' => "int",
80 'deft' => "1",
81 'range' => "1,",
82 'reqd' => "no" },
83 { 'name' => "minnesting",
84 'desc' => "{AZCompactList.minnesting}",
85 'type' => "int",
86 'deft' => "20",
87 'range' => "2,",
88 'reqd' => "no" },
89 { 'name' => "mincompact",
90 'desc' => "{AZCompactList.mincompact}",
91 'type' => "int",
92 'deft' => "10",
93 'range' => "1,",
94 'reqd' => "no" },
95 { 'name' => "maxcompact",
96 'desc' => "{AZCompactList.maxcompact}",
97 'type' => "int",
98 'deft' => "30",
99 'range' => "1,",
100 'reqd' => "no" },
101 { 'name' => "doclevel",
102 'desc' => "{AZCompactList.doclevel}",
103 'type' => "enum",
104 'list' => $doclevel_list,
105 'deft' => "top",
106 'reqd' => "no" },
107 { 'name' => "freqsort",
108 'desc' => "{AZCompactList.freqsort}",
109 'type' => "flag"},
110 { 'name' => "recopt",
111 'desc' => "{AZCompactList.recopt}",
112 'type' => "flag",
113 'reqd' => "no" } ];
114
115my $options =
116{ 'name' => "AZCompactList",
117 'desc' => "{AZCompactList.desc}",
118 'abstract' => "no",
119 'inherits' => "yes",
120 'args' => $arguments };
121
122
123sub new {
124 my ($class) = shift (@_);
125 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
126 push(@$pluginlist, $class);
127
128 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
129 push(@{$hashArgOptLists->{"OptList"}},$options);
130
131 my $self = new BaseClassifier($pluginlist, $inputargs, $hashArgOptLists);
132
133 if ($self->{'info_only'}) {
134 # don't worry about any options etc
135 return bless $self, $class;
136 }
137
138 if (!$self->{"metadata"}) {
139 my $outhandle = $self->{'outhandle'};
140 print $outhandle "AZCompactList Error: required option -metadata not supplied\n";
141 $self->print_txt_usage("");
142 die "AZCompactList Error: required option -metadata not supplied\n";
143 }
144
145 $self->{'metadata'} = $self->strip_ex_from_metadata($self->{'metadata'});
146 # Manually set $self parameters.
147 $self->{'list'} = {};
148 $self->{'listmetavalue'} = {};
149 $self->{'list_mvpair'} = {};
150 $self->{'reclassify'} = {};
151 $self->{'reclassifylist'} = {};
152
153 $self->{'buttonname'} = $self->generate_title_from_metadata($self->{'metadata'}) unless ($self->{'buttonname'});
154
155 if (defined($self->{"removeprefix"}) && $self->{"removeprefix"}) {
156 $self->{"removeprefix"} =~ s/^\^//; # don't need a leading ^
157 }
158 if (defined($self->{"removesuffix"}) && $self->{"removesuffix"}) {
159 $self->{"removesuffix"} =~ s/\$$//; # don't need a trailing $
160 }
161
162 $self->{'recopt'} = ($self->{'recopt'} == 0) ? undef : "on";
163
164 if (defined $self->{'sort'}) {
165 $self->{'sort'} = $self->strip_ex_from_metadata($self->{'sort'});
166 }
167 # Clean out the unused keys
168 if($self->{"removeprefix"} eq "") {delete $self->{"removeprefix"};}
169 if($self->{"removesuffix"} eq "") {delete $self->{"removesuffix"};}
170
171 return bless $self, $class;
172}
173
174sub init
175{
176 my $self = shift (@_);
177
178 $self->{'list'} = {};
179 $self->{'listmetavalue'} = {};
180 $self->{'list_mvpair'} = {};
181 $self->{'reclassify'} = {};
182 $self->{'reclassifylist'} = {};
183}
184
185my $tmp = 0;
186
187sub classify
188{
189 my $self = shift (@_);
190 my ($doc_obj) = @_;
191
192 my $doc_OID = $doc_obj->get_OID();
193 my $outhandle = $self->{'outhandle'};
194
195 my @sectionlist = ();
196 my $topsection = $doc_obj->get_top_section();
197 my $metaname = $self->{'metadata'};
198
199 $metaname =~ s/(\/|\|).*//; # grab first name in n1/n2/n3 or n1|n2|n3 list
200 my @commameta_list = split(/,|;/, $metaname);
201
202 if ($self->{'doclevel'} =~ /^top(level)?/i)
203 {
204 push(@sectionlist,$topsection);
205 }
206 elsif ($self->{'doclevel'} =~ /^first(level)?/i)
207 {
208 my $toplevel_children = $doc_obj->get_children($topsection);
209 push(@sectionlist,@$toplevel_children);
210 }
211 else # (all)?section(s)?
212 {
213 my $thissection = $doc_obj->get_next_section($topsection);
214 while (defined $thissection)
215 {
216 push(@sectionlist,$thissection);
217 $thissection = $doc_obj->get_next_section ($thissection);
218 }
219 }
220
221 my $thissection;
222 foreach $thissection (@sectionlist)
223 {
224 my $full_doc_OID
225 = ($thissection ne "") ? "$doc_OID.$thissection" : $doc_OID;
226
227 if (defined $self->{'list_mvpair'}->{$full_doc_OID})
228 {
229 print $outhandle "WARNING: AZCompactList::classify called multiple times for $full_doc_OID\n";
230 }
231 $self->{'list'}->{$full_doc_OID} = [];
232 $self->{'listmetavalue'}->{$full_doc_OID} = [];
233 $self->{'list_mvpair'}->{$full_doc_OID} = [];
234
235 my $metavalues = [];
236 foreach my $cmn (@commameta_list) {
237 my $cmvalues = $doc_obj->get_metadata($thissection,$cmn);
238 push(@$metavalues,@$cmvalues) if (@{$cmvalues});
239 last if (@{$cmvalues} && !$self->{'allvalues'});
240 }
241
242 my $metavalue;
243 foreach $metavalue (@$metavalues)
244 {
245 # Tidy up use of white space in metavalue for classifying
246 $metavalue =~ s/^\s*//s;
247 $metavalue =~ s/\s*$//s;
248 $metavalue =~ s/\n/ /s;
249 $metavalue =~ s/\s{2,}/ /s;
250
251 # if this document doesn't contain the metadata element we're
252 # sorting by we won't include it in this classification
253 if (defined $metavalue && $metavalue =~ /\w/)
254 {
255 if (defined($self->{'removeprefix'}) &&
256 length($self->{'removeprefix'})) {
257 $metavalue =~ s/^$self->{'removeprefix'}//;
258
259 # check that it's not now empty
260 if (!$metavalue) {next;}
261 }
262
263 if (defined($self->{'removesuffix'}) &&
264 length($self->{'removesuffix'})) {
265 $metavalue =~ s/$self->{'removesuffix'}$//;
266
267 # check that it's not now empty
268 if (!$metavalue) {next;}
269 }
270
271 my $formatted_metavalue;
272 if ($self->{'no_metadata_formatting'}) {
273 $formatted_metavalue = $metavalue;
274 } else {
275 $formatted_metavalue = &sorttools::format_metadata_for_sorting($self->{'metadata'}, $metavalue, $doc_obj);
276 }
277
278 #### prefix-str
279 if (! defined($formatted_metavalue)) {
280 print $outhandle "Warning: AZCompactList: metavalue is ";
281 print $outhandle "empty\n";
282 $formatted_metavalue="";
283 }
284
285 my $mv_pair = { 'mv' => $metavalue, 'fmv' => $formatted_metavalue };
286 push(@{$self->{'list'}->{$full_doc_OID}},$formatted_metavalue);
287 push(@{$self->{'listmetavalue'}->{$full_doc_OID}} ,$metavalue);
288 push(@{$self->{'list_mvpair'}->{$full_doc_OID}},$mv_pair);
289
290
291 last if ($self->{'firstvalueonly'});
292 }
293 }
294
295 # This is used in reclassify below for AZCompactSectionList
296 my $sortmeta = $doc_obj->get_metadata_element($thissection, $self->{'sort'});
297 $self->{'reclassify'}->{$full_doc_OID} = [$doc_obj,$sortmeta];
298 }
299}
300
301sub reinit
302{
303 my ($self,$classlist_ref) = @_;
304 my $outhandle = $self->{'outhandle'};
305
306 my %mtfreq = ();
307 my @single_classlist = ();
308 my @multiple_classlist = ();
309
310 # find out how often each metavalue occurs
311 map
312 {
313 foreach my $mvp (@{$self->{'list_mvpair'}->{$_}} )
314 {
315### print STDERR "*** plain mv = $mvp->{'mv'}\n";
316### print STDERR "*** format mv = $mvp->{'fmv'}\n";
317
318 my $metavalue = $mvp->{'mv'};
319 $metavalue =~ s!^-!\\-!; # in case it starts with "-"
320 $mtfreq{$metavalue}++;
321 }
322 } @$classlist_ref;
323
324 # use this information to split the list: single metavalue/repeated value
325 map
326 {
327 my $i = 1;
328 my $metavalue;
329 foreach my $mvp (@{$self->{'list_mvpair'}->{$_}})
330 {
331 my $metavalue = $mvp->{'mv'};
332 $metavalue =~ s!^-!\\-!; # in case it starts with "-"
333 my $cs_metavalue = $mvp->{'mv'}; # case sensative
334 if ($mtfreq{$metavalue} >= $self->{'mingroup'})
335 {
336### print STDERR "*** pushing on $cs_metavalue\n";
337 push(@multiple_classlist,[$_,$i,$metavalue]);
338 }
339 else
340 {
341 push(@single_classlist,[$_,$cs_metavalue]);
342 $metavalue =~ tr/[A-Z]/[a-z]/;
343 $self->{'reclassifylist'}->{"Metavalue_$i.$_"} = $metavalue;
344 }
345 $i++;
346 }
347 } @$classlist_ref;
348
349
350 # Setup sub-classifiers for multiple list
351
352 $self->{'classifiers'} = {};
353
354 my $pm;
355 foreach $pm ("SimpleList", "SectionList")
356 {
357 my $listname
358 = &util::filename_cat($ENV{'GSDLHOME'},"perllib/classify/$pm.pm");
359 if (-e $listname) { require $listname; }
360 else
361 {
362 print $outhandle "AZCompactList ERROR - couldn't find classifier \"$listname\"\n";
363 die "\n";
364 }
365 }
366
367 # Create classifiers objects for each entry >= mingroup
368 my $metavalue;
369 my $doclevel = $self->{'doclevel'};
370 my $mingroup = $self->{'mingroup'};
371 my @metaname_list = split(/\/|\|/,$self->{'metadata'});
372 my $metaname = shift(@metaname_list);
373 my $hierarchical = 0;
374 if (scalar(@metaname_list) > 1) {
375 $hierarchical = 1;
376 $metaname = join('/',@metaname_list);
377 }
378 foreach $metavalue (keys %mtfreq)
379 {
380 if ($mtfreq{$metavalue} >= $mingroup)
381 {
382 # occurs more often than minimum required to compact into a group
383 my $listclassobj;
384
385 if (!$hierarchical)
386 {
387 my @args;
388 push @args, ("-metadata", "$metaname");
389 # buttonname is also used for the node's title
390 push @args, ("-buttonname", "$metavalue");
391 push @args, ("-sort", $self->{'sort'});
392
393 my $ptArgs = \@args;
394 if ($doclevel =~ m/^top(level)?/i)
395 {
396 eval ("\$listclassobj = new SimpleList([],\$ptArgs)");
397 }
398 else
399 {
400 # first(level)? or (all)?section(s)?
401 eval ("\$listclassobj = new SectionList([],\$ptArgs)");
402 }
403 }
404 else
405 {
406
407 my @args;
408 push @args, ("-metadata", "$metaname");
409 # buttonname is also used for the node's title
410 push @args, ("-buttonname", "$metavalue");
411 push @args, ("-sort", $self->{'sort'});
412
413 if (defined $self->{'removeprefix'}) {
414 push @args, ("-removeprefix", $self->{'removeprefix'});
415 }
416 if (defined $self->{'removesuffix'}) {
417 push @args, ("-removesuffix", $self->{'removesuffix'});
418 }
419
420 push @args, ("-doclevel", "$doclevel");
421 push @args, ("-mingroup", $mingroup);
422 push @args, "-recopt ";
423
424 my $ptArgs = \@args;
425 eval ("\$listclassobj = new AZCompactList([],\$ptArgs)");
426 }
427
428 if ($@) {
429 print $outhandle "$@";
430 die "\n";
431 }
432
433 $listclassobj->init();
434
435 if (defined $metavalue && $metavalue =~ /\w/)
436 {
437 my $formatted_node = $metavalue;
438
439 if (defined($self->{'removeprefix'}) &&
440 length($self->{'removeprefix'})) {
441 $formatted_node =~ s/^$self->{'removeprefix'}//;
442 # check that it's not now empty
443 if (!$formatted_node) {next;}
444 }
445 if (defined($self->{'removesuffix'}) &&
446 length($self->{'removesuffix'})) {
447 $formatted_node =~ s/$self->{'removesuffix'}$//;
448 # check that it's not now empty
449 if (!$formatted_node) {next;}
450 }
451
452 $formatted_node = &sorttools::format_metadata_for_sorting($self->{'metadata'}, $formatted_node) unless $self->{'no_metadata_formatting'};
453
454 # In case our formatted string is empty...
455 if (! defined($formatted_node)) {
456 print $outhandle "Warning: AZCompactList: metavalue is ";
457 print $outhandle "empty\n";
458 $formatted_node="";
459 }
460
461 # use the lower case, for speed of lookup.
462 my $meta_lc=lc($metavalue);
463 $self->{'classifiers'}->{$meta_lc}
464 = { 'classifyobj' => $listclassobj,
465 'formattednode' => $formatted_node };
466 }
467 }
468 }
469
470
471 return (\@single_classlist,\@multiple_classlist);
472}
473
474
475sub reclassify
476{
477 my ($self,$multiple_cl_ref) = @_;
478
479 # Entries in the current classify list that are "book nodes"
480 # should be recursively classified.
481 #--
482 foreach my $dm_pair (@$multiple_cl_ref)
483 {
484 my ($doc_OID,$mdoffset,$metavalue,$cs_metavalue) = @$dm_pair;
485 my $listclassobj;
486
487 # find metavalue in list of sub-classifiers
488 # check if we have a key (lower case) for this metadata value
489 my $node_name=lc($metavalue);
490 if (exists $self->{'classifiers'}->{$node_name})
491 {
492 my ($doc_obj, $sortmeta) = @{$self->{'reclassify'}->{$doc_OID}};
493
494 # record the metadata value offset temporarily, so eg AZList can
495 # get the correct metadata value (for multi-valued metadata fields)
496 $doc_obj->{'mdoffset'}=$mdoffset;
497
498 if ($doc_OID =~ m/^[^\.]*\.([\d\.]+)$/)
499 {
500 my $section=$1;
501 if ($self->{'doclevel'} =~ m/^top(level)?/i) { # toplevel
502 $self->{'classifiers'}->{$node_name}->{'classifyobj'}
503 ->classify($doc_obj,"Section=$section");
504 } else {
505 # first(level)? or (all)?section(s)?
506
507 # classify() can't handle multi-level section, so use
508 # classify_section()
509 # ... thanks to Don Gourley for this...
510
511 $self->{'classifiers'}->{$node_name}->{'classifyobj'}
512 ->classify_section($section, $doc_obj, $sortmeta);
513 }
514 }
515 else
516 {
517 $self->{'classifiers'}->{$node_name}->{'classifyobj'}
518 ->classify($doc_obj);
519 }
520 } else { # this key is not in the hash
521 my $outhandle=$self->{outhandle};
522 print $outhandle "Warning: AZCompactList::reclassify ";
523 print $outhandle "could not find sub-node for metadata=`$metavalue' with doc_OID $doc_OID\n";
524 }
525 }
526}
527
528
529
530sub get_reclassify_info
531{
532 my $self = shift (@_);
533
534 my $node_name;
535 foreach $node_name (keys %{$self->{'classifiers'}})
536 {
537 my $classifyinfo
538 = $self->{'classifiers'}->{$node_name}->{'classifyobj'}
539 ->get_classify_info();
540 $self->{'classifiers'}->{$node_name}->{'classifyinfo'}
541 = $classifyinfo;
542 $self->{'reclassifylist'}->{"CLASSIFY.$node_name"}
543 = $self->{'classifiers'}->{$node_name}->{'formattednode'};
544 }
545}
546
547
548sub alpha_numeric_cmp
549{
550 my ($self,$a,$b) = @_;
551
552 my $title_a = $self->{'reclassifylist'}->{$a};
553 my $title_b = $self->{'reclassifylist'}->{$b};
554
555 if ($title_a =~ m/^(\d+(\.\d+)?)/)
556 {
557 my $val_a = $1;
558 if ($title_b =~ m/^(\d+(\.\d+)?)/)
559 {
560 my $val_b = $1;
561 if ($val_a != $val_b)
562 {
563 return ($val_a <=> $val_b);
564 }
565 }
566 }
567
568 return ($title_a cmp $title_b);
569}
570
571sub frequency_cmp
572{
573 my ($self,$a,$b) = @_;
574
575
576 my $title_a = $self->{'reclassifylist'}->{$a};
577 my $title_b = $self->{'reclassifylist'}->{$b};
578
579 my $a_freq = 1;
580 my $b_freq = 1;
581
582 if ($a =~ m/^CLASSIFY\.(.*)$/)
583 {
584 my $a_node = $1;
585 my $a_nodeinfo = $self->{'classifiers'}->{$a_node}->{'classifyinfo'};
586 $a_freq = scalar(@{$a_nodeinfo->{'contains'}});
587 }
588
589 if ($b =~ m/^CLASSIFY\.(.*)$/)
590 {
591 my $b_node = $1;
592 my $b_nodeinfo = $self->{'classifiers'}->{$b_node}->{'classifyinfo'};
593 $b_freq = scalar(@{$b_nodeinfo->{'contains'}});
594 }
595
596 return $b_freq <=> $a_freq;
597}
598
599sub get_classify_info {
600 my $self = shift (@_);
601
602 my @classlist =keys %{$self->{'list_mvpair'}}; # list all doc oids
603
604 my ($single_cl_ref,$multiple_cl_ref) = $self->reinit(\@classlist);
605 $self->reclassify($multiple_cl_ref);
606 $self->get_reclassify_info();
607
608 my @reclassified_classlist;
609 if ($self->{'freqsort'})
610 {
611 @reclassified_classlist
612 = sort { $self->frequency_cmp($a,$b) } keys %{$self->{'reclassifylist'}};
613 # supress sub-grouping by alphabet
614 map { $self->{'reclassifylist'}->{$_} = "A".$self->{'reclassifylist'}; } keys %{$self->{'reclassifylist'}};
615 }
616 else
617 {
618# @reclassified_classlist
619# = sort {$self->{'reclassifylist'}->{$a} cmp $self->{'reclassifylist'}->{$b};} keys %{$self->{'reclassifylist'}};
620
621 # alpha_numeric_cmp is slower than "cmp" but handles numbers better ...
622
623 @reclassified_classlist
624 = sort { $self->alpha_numeric_cmp($a,$b) } keys %{$self->{'reclassifylist'}};
625
626 }
627
628 return $self->splitlist (\@reclassified_classlist);
629}
630
631sub get_entry {
632 my $self = shift (@_);
633 my ($title, $childtype, $metaname, $thistype) = @_;
634 # organise into classification structure
635 my %classifyinfo = ('childtype'=>$childtype,
636 'Title'=>$title,
637 'contains'=>[],
638 'mdtype'=>$metaname);
639
640 $classifyinfo{'thistype'} = $thistype
641 if defined $thistype && $thistype =~ /\w/;
642
643 return \%classifyinfo;
644}
645
646
647
648# splitlist takes an ordered list of classifications (@$classlistref) and
649# splits it up into alphabetical sub-sections.
650sub splitlist {
651 my $self = shift (@_);
652 my ($classlistref) = @_;
653 my $classhash = {};
654
655 # top level
656 my @metanames = split(/\/|\|/,$self->{'metadata'});
657 my $metaname = shift(@metanames);
658
659 my $childtype = "HList";
660 $childtype = "VList" if (scalar (@$classlistref) <= $self->{'minnesting'});
661
662 my $title = $self->{'buttonname'}; # should always be defined by now.
663 my $classifyinfo;
664 if (!defined($self->{'recopt'}))
665 {
666 $classifyinfo
667 = $self->get_entry ($title, $childtype, $metaname, "Invisible");
668 }
669 else
670 {
671 $classifyinfo
672 = $self->get_entry ($title, $childtype, $metaname, "VList");
673 }
674
675 # don't need to do any splitting if there are less than 'minnesting' classifications
676 if ((scalar @$classlistref) <= $self->{'minnesting'}) {
677 foreach my $subOID (@$classlistref) {
678 if ($subOID =~ /^CLASSIFY\.(.*)$/
679 && defined $self->{'classifiers'}->{$1})
680 {
681 push (@{$classifyinfo->{'contains'}},
682 $self->{'classifiers'}->{$1}->{'classifyinfo'});
683 }
684 else
685 {
686 $subOID =~ s/^Metavalue_(\d+)\.//;
687 my $metaname_offset = $1 -1;
688 my $oid_rec = {'OID'=>$subOID, 'offset'=>$metaname_offset};
689 push (@{$classifyinfo->{'contains'}}, $oid_rec);
690 }
691 }
692 return $classifyinfo;
693 }
694
695 # first split up the list into separate A-Z and 0-9 classifications
696 foreach my $classification (@$classlistref) {
697 my $title = $self->{'reclassifylist'}->{$classification};
698 $title =~ s/&(.){2,4};//g; # remove any HTML special chars
699 $title =~ s/^(\W|_)+//g; # remove leading non-word chars
700
701 # only want first character for classification
702 $title =~ m/^(.)/; # always a char, or first byte of wide char?
703 if (defined($1) && $1 ne "") {
704 $title=$1;
705
706 # remove any accents on initial character by mapping to Unicode's
707 # normalized decomposed form (accents follow initial letter)
708 # and then pick off the initial letter
709 my $title_decomposed = NFD($title);
710 $title = substr($title_decomposed,0,1);
711 } else {
712 print STDERR "no first character found for \"$title\" - \"" .
713 $self->{'reclassifylist'}->{$classification} . "\"\n";
714 }
715 $title =~ tr/[a-z]/[A-Z]/;
716
717 if ($title =~ /^[0-9]$/) {$title = '0-9';}
718 elsif ($title !~ /^[A-Z]$/) {
719 my $outhandle = $self->{'outhandle'};
720 print $outhandle "AZCompactList: WARNING $classification has badly formatted title ($title)\n";
721 }
722 $classhash->{$title} = [] unless defined $classhash->{$title};
723 push (@{$classhash->{$title}}, $classification);
724 }
725 $classhash = $self->compactlist ($classhash);
726
727 my @tmparr = ();
728 foreach my $subsection (sort keys (%$classhash)) {
729 push (@tmparr, $subsection);
730 }
731
732 # if there's a 0-9 section it will have been sorted to the beginning
733 # but we want it at the end
734 if ($tmparr[0] eq '0-9') {
735 shift @tmparr;
736 push (@tmparr, '0-9');
737 }
738 foreach my $subclass (@tmparr)
739 {
740 my $tempclassify
741 = (scalar(@tmparr)==1)
742 ? ($self->get_entry(" ", "VList", $metaname))
743 : ($self->get_entry($subclass, "VList", $metaname));
744
745
746 foreach my $subsubOID (@{$classhash->{$subclass}})
747 {
748 if ($subsubOID =~ /^CLASSIFY\.(.*)$/
749 && defined $self->{'classifiers'}->{$1})
750 {
751 # this is a "bookshelf" node... >1 entry compacted
752 push (@{$tempclassify->{'contains'}},
753 $self->{'classifiers'}->{$1}->{'classifyinfo'});
754 # set the metadata field name, for mdoffset to work
755 $self->{'classifiers'}->{$1}->{'classifyinfo'}->{'mdtype'}=
756 $metaname;
757 }
758 else
759 {
760 $subsubOID =~ s/^Metavalue_(\d+)\.//;
761 # record the offset if this metadata type has multiple values
762 my $metaname_offset = $1 -1;
763 my $oid_rec = {'OID'=>$subsubOID, 'offset'=>$metaname_offset};
764 push (@{$tempclassify->{'contains'}}, $oid_rec);
765 }
766 }
767 push (@{$classifyinfo->{'contains'}}, $tempclassify);
768 }
769
770 return $classifyinfo;
771}
772
773sub compactlist {
774 my $self = shift (@_);
775 my ($classhashref) = @_;
776 my $compactedhash = {};
777 my @currentOIDs = ();
778 my $currentfirstletter = "";
779 my $currentlastletter = "";
780 my $lastkey = "";
781
782 # minimum and maximum documents to be displayed per page.
783 # the actual maximum will be max + (min-1).
784 # the smallest sub-section is a single letter at present
785 # so in this case there may be many times max documents
786 # displayed on a page.
787 my $min = $self->{'mincompact'};
788 my $max = $self->{'maxcompact'};
789
790 foreach my $subsection (sort keys %$classhashref) {
791 if ($subsection eq '0-9') {
792 @{$compactedhash->{$subsection}} = @{$classhashref->{$subsection}};
793 next;
794 }
795 $currentfirstletter = $subsection if $currentfirstletter eq "";
796 if ((scalar (@currentOIDs) < $min) ||
797 ((scalar (@currentOIDs) + scalar (@{$classhashref->{$subsection}})) <= $max)) {
798 push (@currentOIDs, @{$classhashref->{$subsection}});
799 $currentlastletter = $subsection;
800 } else {
801
802 if ($currentfirstletter eq $currentlastletter) {
803 @{$compactedhash->{$currentfirstletter}} = @currentOIDs;
804 $lastkey = $currentfirstletter;
805 } else {
806 @{$compactedhash->{"$currentfirstletter-$currentlastletter"}} = @currentOIDs;
807 $lastkey = "$currentfirstletter-$currentlastletter";
808 }
809 if (scalar (@{$classhashref->{$subsection}}) >= $max) {
810 $compactedhash->{$subsection} = $classhashref->{$subsection};
811 @currentOIDs = ();
812 $currentfirstletter = "";
813 $lastkey=$subsection;
814 } else {
815 @currentOIDs = @{$classhashref->{$subsection}};
816 $currentfirstletter = $subsection;
817 $currentlastletter = $subsection;
818 }
819 }
820 }
821
822 # add final OIDs to last sub-classification if there aren't many otherwise
823 # add final sub-classification
824
825 # don't add if there aren't any oids
826 if (! scalar (@currentOIDs)) {return $compactedhash;}
827
828 if (scalar (@currentOIDs) < $min) {
829 my ($newkey) = $lastkey =~ /^(.)/;
830 @currentOIDs = (@{$compactedhash->{$lastkey}}, @currentOIDs);
831 delete $compactedhash->{$lastkey};
832 @{$compactedhash->{"$newkey-$currentlastletter"}} = @currentOIDs;
833 } else {
834 if ($currentfirstletter eq $currentlastletter) {
835 @{$compactedhash->{$currentfirstletter}} = @currentOIDs;
836 } else {
837 @{$compactedhash->{"$currentfirstletter-$currentlastletter"}} = @currentOIDs;
838 }
839 }
840
841 return $compactedhash;
842}
843
8441;
845
846
Note: See TracBrowser for help on using the repository browser.