diff --git a/CountingWords.rb b/CountingWords.rb new file mode 100644 index 0000000..f996cd5 --- /dev/null +++ b/CountingWords.rb @@ -0,0 +1,8 @@ +freq = Hash.new(0) +file = File.open("speech.txt") +file.read.downcase.gsub(/[^a-z ]/, " ").scan(/\b[a-z]{1,16}\b/){|word| freq[word] += 1} +# could use .gsub(/[:punct:]/, " ") too remove all special characters, or .gsub(/[^a-z ]/, " ") to remove everything that is not a through z. + +freq = freq.sort_by{|a, b| b} +freq.reverse! +freq.each{|word, freq| puts freq.to_s + " - " + word}