Last active
December 13, 2015 19:09
-
-
Save ferostabio/4960894 to your computer and use it in GitHub Desktop.
Script that prints 50 more used terms in a given Lovecraft story passed as argument
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CthulhuBag | |
def initialize | |
@h = Hash.new{ 0 } | |
end | |
def <<(o) | |
@h[o] += 1 | |
end | |
def [](o) | |
@h[o] | |
end | |
def sort | |
@h.sort_by{ |key, value| value}.reverse! | |
end | |
end | |
filename = ARGV.first | |
content = IO.read(filename).downcase.gsub(/[^a-z\s]/, '') | |
words = content.split(' ').select{|w| w.length > 5} | |
bag = CthulhuBag.new | |
words.each { |w| bag << w } | |
result = bag.sort.take(50) | |
result.each { |h| puts "#{h.first} #{h.last}" } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment