Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def word_cloud(words)
- wordcount = {}
- words.split(/\s/).each do |word|
- word.downcase!
- if word.strip.size > 0
- wordcount[word.strip] = (wordcount.key?(word.strip)) ? (wordcount[word.strip] + 1) : 0
- end
- end
- s = []
- wordcount.each_value do |v|
- s << v
- end
- s = s.sort.reverse.slice(0,20)
- min, max = s.last, s.first
- ratio = 18.0 / (max - min)
- cloud = String.new
- wordcount.each_key do |word|
- font_size = (9 + (wordcount[word] * ratio))
- cloud += link_to word, search_path(:type => "tag", :term => word), :style => "font-size: #{font_size}pt;"
- cloud << " "
- end
- cloud
- end
Add Comment
Please, Sign In to add comment