-
Notifications
You must be signed in to change notification settings - Fork 36
Initial word counter #20
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
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,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]/, '') | ||
| @speech.gsub!(/\s+/, ' ') | ||
| @speech.downcase!.strip! | ||
| @speech = @speech.split(' ') | ||
|
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 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 | ||
|
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. 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 | ||
|
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. 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 | ||
| 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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| require_relative 'count' | ||
|
|
||
| words = WordCount.new | ||
| words.run |
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.
If I load a valid text no substitution will be performed. Your return will be nil.
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.
Does the return value matter here, though?