-
Notifications
You must be signed in to change notification settings - Fork 36
Sam's solution #21
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?
Sam's solution #21
Changes from 2 commits
12bc942
5f7e92a
165e1f4
9b0a473
cf3aff3
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,29 @@ | ||
| TEXT = File.read('speech.txt') | ||
|
|
||
| def find_words text | ||
| words_regex = /\w+[a-z']*\w+/ | ||
| text.downcase.scan words_regex | ||
| end | ||
|
|
||
| def count_words words_array | ||
| Hash.new(0).tap do |word_count| | ||
|
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. Nice use of |
||
| words_array.each { |word| word_count[word] += 1 } | ||
| end | ||
| end | ||
|
|
||
| def sort_words words_frequency_hash | ||
| words_frequency_hash.sort_by { |word, count| -count } | ||
|
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. 👍 Don't think I've seen anyone implement it this way before. I was putz'ing around with irb and |
||
| end | ||
|
|
||
| def wordcount_print_format word | ||
| "#{word[1]} - #{word[0].capitalize}" | ||
| end | ||
|
|
||
| def print_sorted_word_counts sorted_words | ||
| sorted_words.each { |word| puts wordcount_print_format(word) } | ||
| end | ||
|
|
||
| words = find_words TEXT | ||
| word_freqs = count_words words | ||
| sorted_words = sort_words word_freqs | ||
| print_sorted_word_counts sorted_words | ||
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.
I think this would make a little more sense defined as a constant outside the method.
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.
It'd probably be a good idea to encapsulate these methods in a class as well, no?