Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions count.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class WordCount
attr_accessor :speech
attr_reader :counted_words

def load_file(path)
@speech = File.read(path)
end

def parse_words
@speech.gsub!(/[^0-9a-zA-Z\s]/, '')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I load a valid text no substitution will be performed. Your return will be nil.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the return value matter here, though?

@speech.gsub!(/\s+/, ' ')
@speech.downcase!.strip!
@speech = @speech.split(' ')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use this split with your regular expression to split by invalid chars instead of by singe space. That will save you the substitution steps above.

end

def count_words
@counted_words = Hash.new
@speech.each do |word|
if @counted_words.include?(word)
@counted_words[word] += 1
else
@counted_words[word] = 1
end
end
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Take a look at inject method, you can start with a Hash.new(0) to store the count pairs. Check the argument on the Hash constructor, it returns that default when you try to access a new key. http://docs.ruby-lang.org/en/2.0.0/Hash.html#method-c-new


def report
sorted_words = @counted_words.sort_by { |word, count| count }
sorted_words.reverse_each do |word, count|
puts count.to_s + ' - ' + word
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid the double sort. Use |word, count| -count in your first sort to do reverse.

end

def run
load_file('speech.txt')
parse_words
count_words
report
end
end
26 changes: 26 additions & 0 deletions count_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'rspec'
require_relative 'count'

describe 'WordCount' do
let(:words) { WordCount.new }

it "returns text when file is loaded" do
expect(words.load_file('speech.txt')).not_to be_nil
end

it "speech contains no special characters after parsing" do
words.load_file('speech.txt')
words.parse_words
expect(words.speech =~ /^0-9a-zA-z\s/i).to be nil
end

it "counts words correctly" do
words.speech = "One two three one two four"
words.parse_words
words.count_words
expect(words.counted_words["one"]).to eq 2
expect(words.counted_words["two"]).to eq 2
expect(words.counted_words["three"]).to eq 1
expect(words.counted_words["four"]).to eq 1
end
end
4 changes: 4 additions & 0 deletions run.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require_relative 'count'

words = WordCount.new
words.run