Skip to content

Instantly share code, notes, and snippets.

@kei-s
Created March 21, 2009 12:26
Show Gist options
  • Save kei-s/82825 to your computer and use it in GitHub Desktop.
Save kei-s/82825 to your computer and use it in GitHub Desktop.
word count
open('oreilly_web_20.txt').read.
scan(/[^)(:;$*"@'>!\?\/,\s-]+/).
map{|word| word.sub(/\.$/,'').downcase }.
reject{|word| word.empty? }.
inject(Hash.new(0)) { |hash, word|
hash[word] += 1
hash
}.sort{|a, b|
(b[1] <=> a[1]) * 2 + (a[0] <=> b[0])
}.each do |array|
puts array[0]+"\t"+array[1].to_s
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment