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