Created
March 13, 2013 15:57
-
-
Save blackwatertepes/5153494 to your computer and use it in GitHub Desktop.
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
=begin | |
INCOMPLETE: I realized last night (as I tried to not think about coding), how to fix at least the issue with the previous iteration. This one works partially, it just doesn't account for capitalization, punctuation, and | |
common words (the, of, etc.) | |
=end | |
def words_in_a_file (source_text, number) | |
file_as_a_string = File.open(source_text).read | |
word_count = Hash.new(0) | |
array_of_text = file_as_a_string.split | |
array_of_text.each do |word| | |
word_count[word] += 1 | |
end | |
array_of_freq = word_count.sort_by {|key, value| value} | |
(array_of_freq.length-1).downto(array_of_freq.length-number) do |i| | |
puts array_of_freq[i][0].to_s + " " + array_of_freq[i][1].to_s | |
end | |
end | |
words_in_a_file("source.txt", 3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment