Tamil/Zebra/Clouder.pm untested

filter: Statementstmt 0.0% 0 / 132 Branchbran 0.0% 0 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 169 CC 13 SCAR 43.6
CC
13
Cov
0%
process
132.0
process_message
6.0
html_cloud
6.0
CRAP 78.1
1 package Koha::Contrib::Tamil::Zebra::Clouder;
2 # ABSTRACT: Class generating keywords clouds from Koha Zebra indexes
3 $Koha::Contrib::Tamil::Zebra::Clouder::VERSION = '0.074';
4 use Moose;
5 use Carp;
6  
7 extends 'AnyEvent::Processor';
8  
9 my $MAX_OCCURENCE = 1000000000;
10  
11 has koha => ( is => 'rw', isa => 'Koha::Contrib::Tamil::Koha' );
12  
13 has index => (
14 is => 'rw',
15 isa => 'Str',
16 trigger => sub {
17 my ($self, $name) = @_;
18 my $zc = $self->koha->zbiblio;
19 eval {
20 $zc->scan_pqf('@attr 1=' . $name . ' @attr 4=1 @attr 6=3 "a"');
21 };
22 croak "Invalid Zebra index: ", $name if $@;
23 return $name;
24 }
25 );
26  
27 has levels_cloud => (
28 is => 'rw',
29 isa => 'Int',
30 default => 24
31 );
32  
33 has max_terms => (
34 is => 'rw',
35 isa => 'Int'
36 );
37  
38 has number_of_terms => (
39 is => 'rw',
40 isa => 'Int',
41 default => 0
42 );
43  
44 has terms => (
45 is => 'rw',
46 isa => 'ArrayRef'
47 );
48  
49 has min_occurence_index => (
50 is => 'rw',
51 isa => 'Int',
52 default => -1
53 );
54  
55 has min_occurence => (
56 is => 'rw',
57 isa => 'Int',
58 default => 0
59 );
60  
61 has from => (
62 is => 'rw',
63 isa => 'Str',
64 default => '0'
65 );
66  
67  
68 #
69 # run
70 # Scan zebra index and populate an array of top terms
71 #
72 # PARAMETERS:
73 # $max_terms Max number of top terms
74 #
75 # RETURN:
76 # A 4-dimensionnal array in $self->{terms}
77 # [0] term
78 # [1] term number of occurences
79 # [2] term proportional relative weight in terms set E[0-1]
80 # [3] term logarithmic relative weight E [0-levels_cloud]
81 #
82 # This array is sorted alphabetically by terms ([0])
83 # It can be easily sorted by occurences:
84 # @t = sort { $a[1] <=> $a[1] } @{$self->{top_terms}};
85 #
86 sub run {
87 my $self = shift;
88 $self->max_terms( shift );
89 $self->SUPER::run();
90 }
91  
92  
93 ############################################################################
94 # Usage : $zebra_index->process()
95 # Purpose : Do a Zebra ZOOM scan and keep top keywords
96 # Returns : TRUE(1) if end of index isn't reached, otherwise FALSE(0)
97 #
98 sub process {
99 my $self = shift;
100 my $max_terms = $self->max_terms;
101 my $levels_cloud = $self->levels_cloud;
102 my $zbiblio = $self->koha->zbiblio;
103 my $number_of_terms = $self->number_of_terms;
104 my @terms = $self->terms ? @{$self->terms} : ();
105 my $min_occurence_index = $self->min_occurence_index;
106 my $min_occurence = $self->min_occurence;
107 my $from = $self->from;
108  
109 my $ss;
110 SCAN:
111 while (1) {
112 eval {
113 #print "$from\n" if $verbose;
114 $from =~ s/\"/\\\"/g;
115 my $query = '@attr 1=' . $self->index . ' @attr 4=1 @attr 6=3 "'
116 . $from . 'a"';
117 $ss = $zbiblio->scan_pqf( $query );
118 };
119 if ($@) {
120 chop $from;
121 next SCAN;
122 }
123 last SCAN;
124 }
125 $ss->option( rpnCharset => 'UTF-8' );
126 if ( $ss->size() == 0 ) { # End
127 # Sort array of array by terms weight
128 #@terms = sort { @{$a}[1] <=> @{$b}[1] } @terms;
129
130 # A relatif weight to other set terms is added to each term
131 my $min = $terms[0][1];
132 my $log_min = log( $min );
133 my $max = $terms[$#terms][1];
134 my $log_max = log( $max );
135 my $delta = $max - $min;
136 $delta = 1 if $delta == 0; # Very unlikely
137 my $factor;
138 if ($log_max - $log_min == 0) {
139 $log_min = $log_min - $levels_cloud;
140 $factor = 1;
141 }
142 else {
143 $factor = $levels_cloud / ($log_max - $log_min);
144 }
145
146 foreach my $term ( @terms ) {
147 my $count = @$term[1];
148 my $weight = ( $count - $min ) / $delta;
149 my $log_weight = int( (log($count) - $log_min) * $factor);
150 push @$term, $weight, $log_weight;
151 }
152 # Sort array of array by terms alphabetical order
153 @terms = sort { @{$a}[0] cmp @{$b}[0] } @terms;
154 $self->terms( \@terms );
155 return 0;
156 }
157 else {
158 my $term = '';
159 my $occ = 0;
160 $self->count( $self->count + $ss->size() );
161 for my $index ( 0..$ss->size()-1 ) {
162 ($term, $occ) = $ss->display_term($index);
163 if ( $number_of_terms < $max_terms ) {
164 push( @terms, [ $term, $occ ] );
165 ++$number_of_terms;
166 if ( $number_of_terms == $max_terms ) {
167 $min_occurence = $MAX_OCCURENCE;
168 for (0..$number_of_terms-1) {
169 my @term = @{ $terms[$_] };
170 if ( $term[1] <= $min_occurence ) {
171 $min_occurence = $term[1];
172 $min_occurence_index = $_;
173 }
174 }
175 }
176 }
177 else {
178 if ( $occ > $min_occurence) {
179 @{ $terms[$min_occurence_index] }[0] = $term;
180 @{ $terms[$min_occurence_index] }[1] = $occ;
181 $min_occurence = $MAX_OCCURENCE;
182 for (0..$max_terms-1) {
183 my @term = @{ $terms[$_] };
184 if ( $term[1] <= $min_occurence ) {
185 $min_occurence = $term[1];
186 $min_occurence_index = $_;
187 }
188 }
189 }
190 }
191 }
192 $self->number_of_terms( $number_of_terms );
193 $self->terms( \@terms );
194 $self->min_occurence_index( $min_occurence_index );
195 $self->min_occurence( $min_occurence );
196 $self->from( $term );
197 return 1;
198 }
199 }
200  
201  
202 sub process_message {
203 my $self = shift;
204 my $from = $self->from;
205 $from = substr($from, 0, 70) . "..." if length($from) > 70;
206 print sprintf(" %#6d", $self->count), " - ", $from, "\n";
207 }
208  
209  
210 #
211 # Returns a HTML version of index top terms formated
212 # as a 'tag cloud'.
213 #
214 sub html_cloud {
215 my $self = shift;
216 my $koha_index = shift;
217 my $withcss = shift;
218 my @terms = @{ $self->terms() };
219 my $html = '';
220 if ( $withcss ) {
221 $html = <<EOS;
222 <style>
223 .subjectcloud {
224 text-align: center;
225 line-height: 16px;
226 margin: 20px;
227 background: #f0f0f0;
228 padding: 3%;
229 }
230 .subjectcloud a {
231 font-weight: lighter;
232 text-decoration: none;
233 }
234 span.tagcloud0 { font-size: 12px;}
235 span.tagcloud1 { font-size: 13px;}
236 span.tagcloud2 { font-size: 14px;}
237 span.tagcloud3 { font-size: 15px;}
238 span.tagcloud4 { font-size: 16px;}
239 span.tagcloud5 { font-size: 17px;}
240 span.tagcloud6 { font-size: 18px;}
241 span.tagcloud7 { font-size: 19px;}
242 span.tagcloud8 { font-size: 20px;}
243 span.tagcloud9 { font-size: 21px;}
244 span.tagcloud10 { font-size: 22px;}
245 span.tagcloud11 { font-size: 23px;}
246 span.tagcloud12 { font-size: 24px;}
247 span.tagcloud13 { font-size: 25px;}
248 span.tagcloud14 { font-size: 26px;}
249 span.tagcloud15 { font-size: 27px;}
250 span.tagcloud16 { font-size: 28px;}
251 span.tagcloud17 { font-size: 29px;}
252 span.tagcloud18 { font-size: 30px;}
253 span.tagcloud19 { font-size: 31px;}
254 span.tagcloud20 { font-size: 32px;}
255 span.tagcloud21 { font-size: 33px;}
256 span.tagcloud22 { font-size: 34px;}
257 span.tagcloud23 { font-size: 35px;}
258 span.tagcloud24 { font-size: 36px;}
259 </style>
260 <div class="subjectcloud">
261 EOS
262 }
263 for (0..$#terms) {
264 my @term = @{ $terms[$_] };
265 my $uri = $term[0];
266 $uri =~ s/\(//g;
267 #print " 0=", $term[0]," - 1=", $term[1], " - 2=", $term[2], " - 3=", $term[3],"\n";
268 $html = $html
269 . '<span class="tagcloud'
270 . $term[3]
271 . '">'
272 . '<a href="/cgi-bin/koha/opac-search.pl?q='
273 . $koha_index
274 . '%3A'
275 . $uri
276 . '">'
277 . $term[0]
278 . "</a></span>\n";
279 }
280 $html .= "</div>\n";
281 return $html;
282 }
283  
284 no Moose;
285  
286 __PACKAGE__->meta->make_immutable;
287  
288 1;
289  
290 __END__