-
Notifications
You must be signed in to change notification settings - Fork 36
Ready for evaluation #32
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 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 @@ | ||
| class CountWords | ||
|
|
||
| attr_accessor :word_count | ||
|
|
||
| def count_words_from_file(file) | ||
| count_file = File.open(file, "r") | ||
| count_file.each_line do |line| | ||
| next if line.empty? | ||
| @@word_count = Hash.new | ||
|
Member
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. It seems like we start with a fresh Hash on every line. Is that a bug? |
||
| word = line.split | ||
| word.each do |wrd| | ||
| next if wrd !~ /(\w+)/ | ||
| wrd = wrd[/(\w+)/] | ||
|
Member
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. Splitting the line, iterating, and making sure each word matches |
||
| if @@word_count.has_key?(wrd) | ||
| @@word_count[wrd] = @@word_count[wrd] + 1 | ||
| else | ||
| @@word_count[wrd] = 1 | ||
| end | ||
| end | ||
| display_word_count_asc | ||
|
Member
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. I think it would be better to not display while you build up the data. How about if the client code (outside this class) asks for the display something like: word_counter = CountWords.new
word_counter.count_words_from_file("speech.txt")
word_counter.display_word_count_asc |
||
| end | ||
|
|
||
| count_file.close | ||
| @@word_count | ||
| end | ||
|
|
||
| def display_word_count_asc | ||
| puts "" | ||
| puts "'''''" | ||
| @@word_count.sort{|a,b| a[1]<=>b[1]}.each { |word| | ||
|
Member
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. Rubyists tend to use Check out Rubocop for automated checks for stuff like this. It enforces this style guide. |
||
| puts "#{word[1]} - #{word[0]}" | ||
| } | ||
| puts "'''''" | ||
| end | ||
|
|
||
| end | ||
|
|
||
| def count_data | ||
| data = CountWords.new | ||
| data.count_words_from_file("speech.txt") | ||
| end | ||
|
|
||
| count_data | ||
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.
This
word_countisn't used yet. You can probably delete all the@@s and then you would be using it.