Skip to content

Instantly share code, notes, and snippets.

@ferostabio
Last active December 13, 2015 19:09
Show Gist options
  • Save ferostabio/4960894 to your computer and use it in GitHub Desktop.
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
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