Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@arnab
Created February 26, 2010 05:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arnab/315457 to your computer and use it in GitHub Desktop.
Save arnab/315457 to your computer and use it in GitHub Desktop.
Given lots of tags, keep a running count of the top-n
require "pp"
class TagManager
def initialize(top_tags_to_keep)
@all_tags = Hash.new(0)
@top_tags = []
@top_tags_to_keep = top_tags_to_keep
end
def search(tag)
@all_tags[tag] = @all_tags[tag] + 1
recalculate_top_tags(tag, @all_tags[tag])
end
def add_to_top_tags(tag)
@top_tags << tag unless @top_tags.include? tag
end
def recalculate_top_tags(tag, hits)
if @top_tags.size < @top_tags_to_keep
add_to_top_tags(tag)
end
if @all_tags[@top_tags.last] < hits
add_to_top_tags(tag)
end
@top_tags.sort! do |a,b|
@all_tags[b] <=> @all_tags[a]
end
@top_tags = @top_tags[0..(@top_tags_to_keep - 1)]
end
end
mgr = TagManager.new(5)
tags = %w(algorithm code test python ruby java c# .Net Perl jQuery html design-patterns c C++ hashkell Objective-C List Scala Clojure)
tags.each do |tag|
rand(10000).times { |n| mgr.search(tag) }
end
pp mgr
__END__
Output:
#<TagManager:0x10036e420
 @all_tags=
  {"html"=>5921,
   "c#"=>5681,
   "code"=>6040,
   "C++"=>154,
   "c"=>9837,
   "java"=>8012,
   "algorithm"=>4199,
   "Clojure"=>885,
   "Scala"=>3243,
   "Objective-C"=>4394,
   "Perl"=>4316,
   "python"=>3333,
   "List"=>9305,
   "jQuery"=>8049,
   "ruby"=>7665,
   "design-patterns"=>565,
   "test"=>9943,
   "hashkell"=>5033,
   ".Net"=>2606},
 @top_tags=["test", "c", "List", "jQuery", "java"],
 @top_tags_to_keep=5>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment