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
31 changes: 31 additions & 0 deletions lib/readfile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

def readFile(file)
return File.read(file)
Copy link
Member

Choose a reason for hiding this comment

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

No need for an explicit return here.

With regards to method names, lower snakecase is generally the rule.
https://github.com/bbatsov/ruby-style-guide

end


def getWords(text)
text.gsub!(/[^A-Za-z ]/,'')
Copy link
Member

Choose a reason for hiding this comment

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

.gsub! mutates your data. how about using i.gsub and returning a modified copy instead?

data = Hash.new
Copy link
Member

Choose a reason for hiding this comment

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

Anytime you initialize a new object and then build it up is a good time to ask could I use .reduce or .map.

In this case, there's an even better option .each_with_object. Try refactoring and using it instead.
https://ruby-doc.org/core-2.4.1/Enumerable.html#method-i-each_with_object

temparray = text.to_s.split(" ").map(&:to_s)
for word in temparray
Copy link
Member

Choose a reason for hiding this comment

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

You won't see a lot of "for loops" used in ruby. There's usually a better option available if you sniff around in...

https://ruby-doc.org/core-2.4.1/Enumerable.html
https://ruby-doc.org/core-2.4.1/Array.html
https://ruby-doc.org/core-2.4.1/Hash.html
https://ruby-doc.org/core-2.4.1/Times.html

if(data.has_key?(word))
data[word] += 1
else
data[word] = 1
end
end
return data
end

def printWords(data)
data = data.sort_by { |key, value| value }.reverse
data.each do |key, value|
puts key.to_s + ' ' + value.to_s
end
end


text = readFile("/Users/apetit/CountingWords/speech.txt")
data = getWords(text)
printWords(data)
24 changes: 24 additions & 0 deletions spec/readfile_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'rspec'
require_relative '../lib/readfile.rb'

describe 'Number of words' do
it 'get words return the proper number' do
Copy link
Member

Choose a reason for hiding this comment

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

2 spaces. https://github.com/bbatsov/ruby-style-guide

Check out this great site on rspec best practices.
www.betterspecs.org

expect(getWords("one two three four").count).to eq 4
end
end

describe 'Accounts for duplicates' do
it 'same word only gets counted once' do
expect(getWords("one two three one")["one"]).to eq 2
expect(getWords("one two three one").count).to eq 3
end
end

describe 'No Special Chars' do
it 'No Special Chars' do
expect(getWords("one two! one")["two"]).to eq 1
expect(getWords("one two. one")["two"]).to eq 1
expect(getWords("one two- one")["two"]).to eq 1
expect(getWords("one two? one")["two"]).to eq 1
end
end