Guest User

Untitled

a guest
May 27th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. def word_cloud(words)
  2. wordcount = {}
  3. words.split(/\s/).each do |word|
  4. word.downcase!
  5. if word.strip.size > 0
  6. wordcount[word.strip] = (wordcount.key?(word.strip)) ? (wordcount[word.strip] + 1) : 0
  7. end
  8. end
  9.  
  10. s = []
  11. wordcount.each_value do |v|
  12. s << v
  13. end
  14. s = s.sort.reverse.slice(0,20)
  15. min, max = s.last, s.first
  16. ratio = 18.0 / (max - min)
  17.  
  18. cloud = String.new
  19. wordcount.each_key do |word|
  20. font_size = (9 + (wordcount[word] * ratio))
  21. cloud += link_to word, search_path(:type => "tag", :term => word), :style => "font-size: #{font_size}pt;"
  22. cloud << " "
  23. end
  24.  
  25. cloud
  26. end
Add Comment
Please, Sign In to add comment