Skip to content

Instantly share code, notes, and snippets.

@rgreenjr
Created January 18, 2010 15:30
Show Gist options
  • Save rgreenjr/280096 to your computer and use it in GitHub Desktop.
Save rgreenjr/280096 to your computer and use it in GitHub Desktop.
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