source: main/trunk/greenstone2/perllib/classify/AZCompactSectionList.pm@ 28564

Last change on this file since 28564 was 28564, checked in by kjdon, 10 years ago

changing some util:: methods to FileUtils:: methods

  • Property svn:keywords set to Author Date Id Revision
File size: 5.9 KB
Line 
1###########################################################################
2#
3# AZCompactSectionList.pm --
4#
5# Experimental AZCompactList with fixes to handle section-level metadata
6#
7###########################################################################
8
9package AZCompactSectionList;
10
11use AZCompactList;
12use FileUtils;
13
14use strict;
15no strict 'refs'; # allow filehandles to be variables and viceversa
16
17sub BEGIN {
18 @AZCompactSectionList::ISA = ('AZCompactList');
19}
20
21my $arguments = [
22 { 'name' => "doclevel",
23 'desc' => "{AZCompactList.doclevel}",
24 'type' => "enum",
25 'list' => $AZCompactList::doclevel_list,
26 'deft' => "section",
27 'reqd' => "no" },
28 ];
29my $options =
30{ 'name' => "AZCompactSectionList",
31 'desc' => "{AZCompactSectionList.desc}",
32 'abstract' => "no",
33 'inherits' => "yes",
34 'args' => $arguments };
35
36sub new {
37 my ($class) = shift (@_);
38 my ($classifierslist,$inputargs,$hashArgOptLists) = @_;
39 push(@$classifierslist, $class);
40
41 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
42 push(@{$hashArgOptLists->{"OptList"}},$options);
43
44 my $self = new AZCompactList($classifierslist, $inputargs, $hashArgOptLists);
45
46 return bless $self, $class;
47}
48
49#
50# override reinit() & reclassify() to demonstrate possible bug fixes
51# (search for SECTIONFIX? to see lines changed from AZCompactList.pm)
52#
53sub reinit
54{
55 my ($self,$classlist_ref) = @_;
56
57 my %mtfreq = ();
58 my @single_classlist = ();
59 my @multiple_classlist = ();
60
61 # find out how often each metavalue occurs
62 map
63 {
64 my $mv;
65 foreach $mv (@{$self->{'listmetavalue'}->{$_}} )
66 {
67 $mtfreq{$mv}++;
68 }
69 } @$classlist_ref;
70
71 # use this information to split the list: single metavalue/repeated value
72 map
73 {
74 my $i = 1;
75 my $metavalue;
76 foreach $metavalue (@{$self->{'listmetavalue'}->{$_}})
77 {
78 if ($mtfreq{$metavalue} >= $self->{'mingroup'})
79 {
80 push(@multiple_classlist,[$_,$i,$metavalue]);
81 }
82 else
83 {
84 push(@single_classlist,[$_,$metavalue]);
85 $metavalue =~ tr/[A-Z]/[a-z]/;
86 $self->{'reclassifylist'}->{"Metavalue_$i.$_"} = $metavalue;
87 }
88 $i++;
89 }
90 } @$classlist_ref;
91
92
93 # Setup sub-classifiers for multiple list
94
95 $self->{'classifiers'} = {};
96
97 my $pm;
98 foreach $pm ("SimpleList", "SectionList")
99 {
100 my $listname
101 = &FileUtils::filenameConcatenate($ENV{'GSDLHOME'},"perllib/classify/$pm.pm");
102 if (-e $listname) { require $listname; }
103 else
104 {
105 my $outhandle = $self->{'outhandle'};
106 print $outhandle "AZCompactList ERROR - couldn't find classifier \"$listname\"\n";
107 die "\n";
108 }
109 }
110
111 # Create classifiers objects for each entry >= mingroup
112 my $metavalue;
113 foreach $metavalue (keys %mtfreq)
114 {
115 if ($mtfreq{$metavalue} >= $self->{'mingroup'})
116 {
117 my $listclassobj;
118 my $doclevel = $self->{'doclevel'};
119 my $metaname = $self->{'metadata'};
120 my @metaname_list = split('/',$metaname);
121 $metaname = shift(@metaname_list);
122 if (@metaname_list==0)
123 {
124 my @args;
125 push @args, ("-metadata", "$metaname");
126# buttonname is also used for the node's title
127 push @args, ("-buttonname", "$metavalue");
128 push @args, ("-sort", $self->{'sort'});
129
130 my $ptArgs = \@args;
131 if ($doclevel =~ m/^top(level)?/i)
132 {
133 eval ("\$listclassobj = new SimpleList([],\$ptArgs)"); warn $@ if $@;
134 }
135 else
136 {
137 # SECTIONFIX?
138 #eval ("\$listclassobj = new SectionList($args)");
139 eval ("\$listclassobj = new SectionList([],\$ptArgs)");
140 }
141 }
142 else
143 {
144 $metaname = join('/',@metaname_list);
145
146 my @args;
147 push @args, ("-metadata", "$metaname");
148# buttonname is also used for the node's title
149 push @args, ("-buttonname", "$metavalue");
150 push @args, ("-doclevel", "$doclevel");
151 push @args, "-recopt";
152
153 # SECTIONFIX?
154 #eval ("\$listclassobj = new AZCompactList($args)");
155 my $ptArgs = \@args;
156 eval ("\$listclassobj = new AZCompactList([],\$ptArgs)");
157 }
158 if ($@) {
159 my $outhandle = $self->{'outhandle'};
160 print $outhandle "$@";
161 die "\n";
162 }
163
164 $listclassobj->init();
165
166 if (defined $metavalue && $metavalue =~ /\w/)
167 {
168 my $formatted_node = $metavalue;
169 if ($self->{'metadata'} =~ m/^Creator(:.*)?$/)
170 {
171 &sorttools::format_string_name_en(\$formatted_node);
172 }
173 else
174 {
175 &sorttools::format_string_en(\$formatted_node);
176 }
177
178 $self->{'classifiers'}->{$metavalue}
179 = { 'classifyobj' => $listclassobj,
180 'formattednode' => $formatted_node };
181 }
182 }
183 }
184
185
186 return (\@single_classlist,\@multiple_classlist);
187}
188
189
190sub reclassify
191{
192 my ($self,$multiple_cl_ref) = @_;
193
194 # Entries in the current classify list that are "book nodes"
195 # should be recursively classified.
196 #--
197 foreach my $dm_pair (@$multiple_cl_ref)
198 {
199 my ($doc_OID,$mdoffset,$metavalue) = @$dm_pair;
200 my $listclassobj;
201
202 # find metavalue in list of sub-classifiers
203 my $found = 0;
204 my $node_name;
205 foreach $node_name (keys %{$self->{'classifiers'}})
206 {
207 my $resafe_node_name = $node_name;
208 $resafe_node_name =~ s/(\(|\)|\[|\]|\{|\}|\^|\$|\.|\+|\*|\?|\|)/\\$1/g;
209 if ($metavalue =~ m/^$resafe_node_name$/i)
210 {
211 my ($doc_obj, $sortmeta) = @{$self->{'reclassify'}->{$doc_OID}};
212
213 # SECTIONFIX? section must include multiple levels, e.g. '1.12'
214 #if ($doc_OID =~ m/^.*\.(\d+)$/)
215 if ($doc_OID =~ m/^[^\.]*\.([\d\.]+)$/)
216 {
217 $self->{'classifiers'}->{$node_name}->{'classifyobj'}
218 # SECTIONFIX? classify can't handle multi-level section
219 #->classify($doc_obj, "Section=$1");
220 ->classify_section($1, $doc_obj, $sortmeta);
221 }
222 else
223 {
224 $self->{'classifiers'}->{$node_name}->{'classifyobj'}
225 ->classify($doc_obj);
226 }
227
228 $found = 1;
229 last;
230 }
231 }
232
233 if (!$found)
234 {
235 my $outhandle = $self->{'outhandle'};
236 print $outhandle "Warning: AZCompactList::reclassify ";
237 print $outhandle "could not find sub-node for $metavalue with doc_OID $doc_OID\n";
238 }
239 }
240}
241
2421;
Note: See TracBrowser for help on using the repository browser.