Class: TagCloud
- Inherits:
-
Object
- Object
- TagCloud
- Defined in:
- lib/sdb_dal/tag_cloud.rb
Overview
TagCloud.rb July 6, 2007
Instance Method Summary collapse
- #calculate_max_min ⇒ Object
- #get_count(tag_name) ⇒ Object
- #get_magnitude(tag_name) ⇒ Object
-
#initialize(tag_data) ⇒ TagCloud
constructor
A new instance of TagCloud.
- #tags ⇒ Object
Constructor Details
#initialize(tag_data) ⇒ TagCloud
Returns a new instance of TagCloud.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/sdb_dal/tag_cloud.rb', line 18 def initialize(tag_data) @histogram=tag_data.inject(Hash.new(0)){|hash,x| hash[x.tag_name]+=1;hash} @tag_data=tag_data.sort_by{|x|[@histogram[x.tag_name],x.tag_name]} calculate_max_min while @histogram.size>40 @histogram.reject!{|key, value| value == @min } calculate_max_min end @normal_histogram={} if tag_data.length>0 range=@max-@min for key in @histogram.keys if range==0 @normal_histogram[key]=2 else @normal_histogram[key]=((@histogram[key]-@min)*4)/range end end @tags=@histogram.keys @tags.sort! else @tags=[] end end |
Instance Method Details
#calculate_max_min ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/sdb_dal/tag_cloud.rb', line 6 def calculate_max_min @max=nil @min=nil for key in @histogram.keys if @min==nil || @histogram[key]<@min @min=@histogram[key] end if @max==nil || @histogram[key]>@max @max=@histogram[key] end end end |
#get_count(tag_name) ⇒ Object
48 49 50 |
# File 'lib/sdb_dal/tag_cloud.rb', line 48 def get_count(tag_name) return @histogram[tag_name] end |
#get_magnitude(tag_name) ⇒ Object
51 52 53 |
# File 'lib/sdb_dal/tag_cloud.rb', line 51 def get_magnitude(tag_name) return @normal_histogram[tag_name] end |
#tags ⇒ Object
45 46 47 |
# File 'lib/sdb_dal/tag_cloud.rb', line 45 def return @tags end |