Created
April 3, 2017 09:54
-
-
Save fractalatcarf/a0e196756445e070e414916094096ad5 to your computer and use it in GitHub Desktop.
count words in a text file
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
article = open('article.txt').read | |
words = article.split(" ") | |
hash_words = {} | |
p words.length | |
words.each do |word| | |
if hash_words.key? (word) | |
hash_words[word] += 1 | |
else | |
hash_words[word] = 1 | |
end | |
end | |
sorted_hash = hash_words.sort_by do |word, frequency| | |
frequency | |
end | |
p sorted_hash |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment