Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gem 'rspec'
25 changes: 25 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
GEM
specs:
diff-lcs (1.3)
rspec (3.8.0)
rspec-core (~> 3.8.0)
rspec-expectations (~> 3.8.0)
rspec-mocks (~> 3.8.0)
rspec-core (3.8.0)
rspec-support (~> 3.8.0)
rspec-expectations (3.8.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.8.0)
rspec-mocks (3.8.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.8.0)
rspec-support (3.8.0)

PLATFORMS
ruby

DEPENDENCIES
rspec

BUNDLED WITH
2.0.1
23 changes: 23 additions & 0 deletions lib/count_words.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Count_Words
attr_reader :book

def initialize
@book = Hash.new(0)
end

def count_words(input_string)
input_string.scan(/\w+/).each do |word|
@book[word.downcase]+=1
end
end

def order_book_by_count
@book = @book.sort_by{|k,v|-v}
end

def render_book
@book.map do |k,v|
puts "#{k}: #{v}"
end
end
end
11 changes: 11 additions & 0 deletions main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require_relative 'lib/count_words'

counter = Count_Words.new

File.open('speech.txt','r') do |file|
file.each do |line|
counter.count_words(line)
end
counter.order_book_by_count
counter.render_book
end
Copy link
Member

Choose a reason for hiding this comment

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

I haven't seen this strategy before. Typically folks will give all the file content to the object and then ask for a report. Something like:

corpus = File.read('speech.txt')
counter = Count_Words.new(corpus)
puts counter.words

But I don't mean to imply there's anything wrong with your approach. Reading the file one line at a time is great for really big files.

If you feel like practicing more, you could try switching approaches. This is pretty awesome as-is, though. Let me know if you would like any additional guidance.

32 changes: 32 additions & 0 deletions spec/count_words_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'rspec'
require_relative '../lib/count_words'

describe 'initializing book' do
let(:test) { Count_Words.new }

it 'test starts empty' do
expect(test.book).to eq({})
end
end

describe 'counting a single string' do
let(:test){ Count_Words.new}

it 'counters have correct values' do
test.count_words('The cat is tall. The cat is much bigger than the mouse.')
expected = {
'the' => 3,
'cat' => 2,
'is' => 2,
'tall' => 1,
'mouse' => 1,
'bigger' => 1,
'than' => 1,
'much' => 1
}
expect(test.book).to eq(expected)
end
end

describe 'handle garbage' do
end