| | 138 | sub rating { |
| | 139 | my ( $self, %args ) = @_; |
| | 140 | |
| | 141 | my $rate = delete $args{'rate'} or Carp::croak "Argument 'rate' is not specified."; |
| | 142 | Carp::croak "Argument 'rate' is not positive number" if ( $rate < 0 ); |
| | 143 | Carp::croak "Argument 'rate' is zero" if ( $rate == 0 ); |
| | 144 | |
| | 145 | my $all = scalar( keys %{ $self->data } ); |
| | 146 | my $num = int( ($all / $rate) + 0.5 ); |
| | 147 | $num = 1 if ( $num == 0 ); |
| | 148 | |
| | 149 | my $results = []; |
| | 150 | my $count = $num; |
| | 151 | my $rank = $num; |
| | 152 | for my $word ( sort { $self->data->{$b} <=> $self->data->{$a} } sort keys %{ $self->data } ) { |
| | 153 | push @{ $results }, +{ |
| | 154 | name => $word, |
| | 155 | count => $self->data->{$word}, |
| | 156 | rank => $rank, |
| | 157 | }; |
| | 158 | |
| | 159 | $count--; |
| | 160 | if ( $count == 0 ) { |
| | 161 | $rank--; |
| | 162 | $rank = 1 if ( $rank <= 0 ); |
| | 163 | $count = $num; |
| | 164 | } |
| | 165 | } |
| | 166 | |
| | 167 | return $results; |
| | 168 | } |
| | 169 | |
| | 282 | This method assigns the rate to a word. |
| | 283 | |
| | 284 | The rate is specified as argument C<'rate'>. |
| | 285 | Argument 'rate' has to be an integer of more than zero. |
| | 286 | |
| | 287 | A return value is the following feeling: |
| | 288 | |
| | 289 | $resource = [ |
| | 290 | { name => 'foo', count => 20, rank => 5 }, |
| | 291 | { name => 'bar', count => 15, rank => 4 }, |
| | 292 | { name => 'baz', count => 10, rank => 3 }, |
| | 293 | ... |
| | 294 | ]; |
| | 295 | |