Skip to content

Instantly share code, notes, and snippets.

Created July 6, 2013 11:48
Show Gist options
  • Save anonymous/5939682 to your computer and use it in GitHub Desktop.
Save anonymous/5939682 to your computer and use it in GitHub Desktop.
word frequency
puts 'What is the name and path of the file?'
filename = gets.chomp
text = String.new
File.open(filename) { |f| text = f.read }
words = text.split(/[^a-zA-Z]/)
freqs = Hash.new(0)
words.each { |word| freqs[word] += 1 }
freqs = freqs.sort_by {|x,y| y }
freqs.reverse!
freqs.each {|word, freq| puts word+' '+freq.to_s}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment