Created
March 21, 2009 12:26
-
-
Save kei-s/82825 to your computer and use it in GitHub Desktop.
word count
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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