Created
October 10, 2012 15:30
-
-
Save kjwierenga/3866358 to your computer and use it in GitHub Desktop.
Character frequency of text in Ruby
This file contains hidden or 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
#!/usr/bin/env ruby | |
text = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do | |
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim | |
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo | |
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse | |
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non | |
proident, sunt in culpa qui officia deserunt mollit anim id est laborum." | |
freqs = text.downcase.tr(".\n ",'').chars.inject(Hash.new(0)) { |freq,c| | |
freq[c] += 1; freq }.to_a.sort{ |a,b| a.last <=> b.last} | |
puts freqs.inspect | |
# Output | |
# [["h", 1], ["v", 3], ["f", 3], ["g", 3], ["b", 3], ["x", 3], [",", 4], ["q", 5], ["p", 11], ["c", 16], ["m", 17], ["s", 18], ["d", 19], ["r", 22], ["l", 22], ["n", 24], ["o", 29], ["a"# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment