-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Jack Chester Solutions #43
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require 'rspec' | ||
require_relative '../wordcounter' | ||
|
||
describe '#countWords' do | ||
let(:counter) { Counter.new } | ||
|
||
context 'when string is unique' do | ||
it 'returns proper array' do | ||
expect(counter.countWords("maverick goose iceman")).to eq( | ||
[["maverick", 1], | ||
["goose", 1], | ||
["iceman", 1]]) | ||
end | ||
end | ||
|
||
context 'when string is nonunique' do | ||
it 'returns proper array' do | ||
expect(counter.countWords("maverick maverick goose maverick iceman goose")).to eq( | ||
[["maverick", 3], | ||
["goose", 2], | ||
["iceman", 1]]) | ||
end | ||
end | ||
|
||
context 'when string is nonunique, puntuated, with capitalization' do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can actually nests contexts. |
||
it 'returns proper array' do | ||
expect(counter.countWords("GOOSE! Maverick Goose goose, Iceman (icEman)")).to eq( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rspec also has include matchers too. https://relishapp.com/rspec/rspec-expectations/docs/built-in-matchers/include-matcher |
||
[["goose", 3], | ||
["iceman", 2], | ||
["maverick", 1]]) | ||
end | ||
end | ||
end | ||
|
||
describe '#printCount' do | ||
let(:counter) { Counter.new } | ||
|
||
context 'when reading in file' do | ||
it 'does not raise an error' do | ||
expect{counter.printCount("./speech.txt")}.not_to raise_error | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
class Counter | ||
|
||
def countWords(text) | ||
text = text.downcase.gsub(/[^a-z' ]/, '') #make lowercase and only keep allowed characters | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so the indentation is might funky here, eh? |
||
raw_words = text.split(" ") | ||
count = {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. anytime you initialize a new object just to build it up later is a good time to think about using in this case There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 bookmarked the Ruby enumerable documentation |
||
|
||
raw_words.each do |word| | ||
if !count.key? word | ||
count[word] = 1 | ||
else | ||
count[word] += 1 | ||
end | ||
end | ||
|
||
count.sort_by{ |k, v| -v} | ||
end | ||
|
||
def printCount(path) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. love the encapsulation. nitpick: keep in mind that lower snakecase is preferred by rubyists There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 Thanks for the feedback! |
||
text = File.open(path) { |line| line.read } #concatenates every line into one string | ||
countWords(text).each do |word, count| | ||
puts "#{word} - #{count}" | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
love the fact that you wrote tests for this. 👍