From 491a1aae052d75bf276bbd46c014c5d1c473e4cb Mon Sep 17 00:00:00 2001 From: awanninger Date: Sat, 13 Feb 2016 08:22:04 -0500 Subject: [PATCH 1/3] completed challenge --- project.rb | 3 +++ word_counter.rb | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 project.rb create mode 100644 word_counter.rb diff --git a/project.rb b/project.rb new file mode 100644 index 0000000..61cbff3 --- /dev/null +++ b/project.rb @@ -0,0 +1,3 @@ +require_relative 'word_counter' + +p WordCounter.count(File.open('speech.txt','r')) diff --git a/word_counter.rb b/word_counter.rb new file mode 100644 index 0000000..25ff3b1 --- /dev/null +++ b/word_counter.rb @@ -0,0 +1,16 @@ +class WordCounter + def self.count file + raise 'WordCounter: no file given' unless file + # use inject to create a hash of word counts + to_word_array(file).inject({}) { |h,w| h[w] ? h[w] += 1 : h[w] = 1; h } + end + + private + + # get array of words + def self.to_word_array(file) + IO.read(file) + .gsub(/[^a-zA-Z']/,' ') + .split(' ') + end +end From c892745ccd2168380d79b9d21eedf5e21906178e Mon Sep 17 00:00:00 2001 From: awanninger Date: Sat, 13 Feb 2016 09:03:03 -0500 Subject: [PATCH 2/3] added reverse sorting of the hash on values --- word_counter.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/word_counter.rb b/word_counter.rb index 25ff3b1..2106004 100644 --- a/word_counter.rb +++ b/word_counter.rb @@ -3,6 +3,8 @@ def self.count file raise 'WordCounter: no file given' unless file # use inject to create a hash of word counts to_word_array(file).inject({}) { |h,w| h[w] ? h[w] += 1 : h[w] = 1; h } + .sort_by { |k,v| v } + .reverse end private From ba58ac710e2de284211d34f6bf17720e3a3adf93 Mon Sep 17 00:00:00 2001 From: awanninger Date: Sun, 14 Feb 2016 09:36:24 -0500 Subject: [PATCH 3/3] transformed static methods to fine-grained instance methods --- project.rb | 9 ++++++++- word_counter.rb | 51 ++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 48 insertions(+), 12 deletions(-) diff --git a/project.rb b/project.rb index 61cbff3..df1a145 100644 --- a/project.rb +++ b/project.rb @@ -1,3 +1,10 @@ require_relative 'word_counter' -p WordCounter.count(File.open('speech.txt','r')) +# you can initialize and use a new WordCounter with a file +counter = WordCounter.new(File.open('speech.txt','r')) +p counter.ordered_word_count + +# but you can call word_count on any file so that you +# don't have to create a new WordCount object every time you +# create a new file; you can just use the existing one +p counter.ordered_word_count(File.open('speech.txt','r')) diff --git a/word_counter.rb b/word_counter.rb index 2106004..9052c13 100644 --- a/word_counter.rb +++ b/word_counter.rb @@ -1,18 +1,47 @@ +require 'pry' + class WordCounter - def self.count file - raise 'WordCounter: no file given' unless file - # use inject to create a hash of word counts - to_word_array(file).inject({}) { |h,w| h[w] ? h[w] += 1 : h[w] = 1; h } - .sort_by { |k,v| v } - .reverse + attr_accessor :file, :file_string, :word_array + + # you can choose to initialize a new WordCounter + # with or without a file + def initialize(file = nil) + setup_new_file(file) unless file.nil? end + + # you can choose to pass any file into WordCounter + # and the internal state of your WordCounter + # instance will be setup to reflect your new file + def ordered_word_count(file = nil) + initialize(file) unless file.nil? + # will create a hash of counts and then transform that hash into an + # array of arrays ordered by highest count to lowest count + @word_array.each_with_object(Hash.new(0)) { |word,hash| hash[word] += 1 } + .sort_by { |key,val| val } + .reverse + end + private - # get array of words - def self.to_word_array(file) - IO.read(file) - .gsub(/[^a-zA-Z']/,' ') - .split(' ') + # get file contents as string + def read_file + @file_string = IO.read(@file) + end + + # remove non-words and set word_array + def parse_file + @file_string.gsub!(/[^a-zA-Z']/,' ') + end + + def set_word_array + @word_array = @file_string.split(' ') + end + + def setup_new_file(file) + @file = file + read_file + parse_file + set_word_array end end