Changeset 13377

Show
Ignore:
Timestamp:
06/07/08 16:40:56 (5 years ago)
Author:
nyarla
Message:

I implemented rating method.

Location:
lang/perl/Data-Cloud/trunk
Files:
1 added
1 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/Data-Cloud/trunk/lib/Data/Cloud.pm

    r13375 r13377  
    136136} 
    137137 
     138sub 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 
    1381701; 
    139171__END__ 
     
    248280  my $resource = $cloud->rating( rate => 5 ); 
    249281 
     282This method assigns the rate to a word. 
     283 
     284The rate is specified as argument C<'rate'>. 
     285Argument 'rate' has to be an integer of more than zero. 
     286 
     287A 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 
    250296=head2 filter 
    251297