Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script to count word frequency.
#!/usr/bin/env ruby -w
text = IO.read(ARGV.first)
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 sprintf("%20s %s", word, freq.to_s) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.