From 177db8104a614da27126786e7a44ee08d768b2a5 Mon Sep 17 00:00:00 2001 From: Rajashree K Maiya Date: Thu, 14 Jan 2016 10:18:43 -0500 Subject: [PATCH] Counting words-completed --- countWords.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 countWords.rb diff --git a/countWords.rb b/countWords.rb new file mode 100644 index 0000000..cae4a0a --- /dev/null +++ b/countWords.rb @@ -0,0 +1,13 @@ +file = File.open("speech.txt", "r") +content = file.read +frequency = Hash.new(0) +wordcount = content.split(" ") +wordcount.each do |w| + w.gsub!(/\W|_/,"") + frequency[w] += 1 +end +frequency = frequency.sort_by {|word,count| count}.reverse +frequency.each do |key,value| + puts "#{key}:#{value}" +end +file.close