Guest User

Untitled

a guest
Feb 21st, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.47 KB | None | 0 0
  1. require "rubygems"
  2. require "open-uri"
  3. require "nokogiri"
  4.  
  5. def count_word(url)
  6. text = fetch_text(Nokogiri::HTML(open(url)))
  7.  
  8. text.scan(/\w+/).inject(Hash.new(0)) { |count, word|
  9. count[word.downcase] += 1
  10. count
  11. }.sort { |a, b|
  12. b[1] <=> a[1]
  13. }
  14. end
  15.  
  16. def fetch_text(e)
  17. if e.is_a? Nokogiri::XML::Text
  18. return e.text
  19. end
  20.  
  21. e.children.inject(String.new) { |text, child|
  22. text << fetch_text(child)
  23. text << "\n"
  24. text
  25. }
  26. end
Add Comment
Please, Sign In to add comment