| 1 | # $Id: /local/perl/HTML-TagClouder/trunk/lib/HTML/TagClouder/Processor/Simple.pm 11418 2007-05-24T00:52:24.708742Z daisuke $ | ||
| 2 | # | ||
| 3 | # Copyright (c) 2007 Daisuke Maki <daisuke@endeworks.jp> | ||
| 4 | |||
| 5 | package HTML::TagClouder::Processor::Simple; | ||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | use base qw(HTML::TagClouder::Processor); | ||
| 9 | |||
| 10 | __PACKAGE__->mk_accessors($_) for qw(font_max font_base font_trunc_step); | ||
| 11 | |||
| 12 | sub new | ||
| 13 | { | ||
| 14 | my $class = shift; | ||
| 15 | $class->next::method(font_max => 250, font_base => 100, font_trunc_step => 5, @_); | ||
| 16 | } | ||
| 17 | |||
| 18 | sub process | ||
| 19 | { | ||
| 20 | my $self = shift; | ||
| 21 | my $cloud = shift; | ||
| 22 | |||
| 23 | my $collection = $cloud->collection; | ||
| 24 | $collection->sort; | ||
| 25 | |||
| 26 | my $iterator = $collection->iterator(); | ||
| 27 | my $font_base = $self->font_base; | ||
| 28 | my $font_max = $self->font_max; | ||
| 29 | my $trunc_step = int($self->font_trunc_step); | ||
| 30 | |||
| 31 | my $min_count = undef; | ||
| 32 | my $max_count = 0; | ||
| 33 | while (my $tag = $iterator->next ) { | ||
| 34 | $min_count = $tag->count if ! defined $min_count || $min_count > $tag->count; | ||
| 35 | $max_count = $tag->count if $max_count < $tag->count; | ||
| 36 | } | ||
| 37 | $iterator->reset; | ||
| 38 | |||
| 39 | my $diff = $max_count - $min_count; | ||
| 40 | my $k = $diff == 0 ? 1 : ($font_max - $font_base) / ($diff ** 2); | ||
| 41 | |||
| 42 | while (my $tag = $iterator->next) { | ||
| 43 | my $norm = int($k * (($tag->count - $min_count) ** 2)); | ||
| 44 | |||
| 45 | if ($trunc_step > 0) { | ||
| 46 | my $remainder = $norm % 5; | ||
| 47 | if ($remainder > 0) { | ||
| 48 | $norm += 5 - $remainder; | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | $tag->count_norm( $norm + $font_base ); | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | 1; | ||
| 57 | |||
| 58 | __END__ |