-
Notifications
You must be signed in to change notification settings - Fork 36
completed challenge #22
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,41 @@ | ||
|
|
||
| class Parser | ||
| def initialize(filename) | ||
| file = File.open(filename, "r") | ||
| @text = '' | ||
| file.each_line do |txt| | ||
| @text += txt | ||
| end | ||
| end | ||
|
|
||
| def array_of_words | ||
| @text.split(/\W+/) | ||
|
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. This is super-simple, and it works pretty well. Some trouble with contractions ("it's"), but maybe that's unavoidable without a lot of complexity. 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. If you wanted to make this work with contractions, you could keep using the split method, but change the regex so that you split on anything that isn't a word character or a single quote. Alternatively you could look into using the String#scan method, which will create an array of words that match a regex. link 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. @sampriddy, The first part had me excited at first, but in fact that's going to give trouble with the single quotes in the original passage, which you would want to cut out. Ultimately, you'd need to split on a regex that finds non-word-characters and single-quotes-that-aren't-between-other-word-characters. |
||
| end | ||
|
|
||
| def map_words_to_count | ||
| array = array_of_words | ||
| hash = {} | ||
| array.each do |item| | ||
| item = item.downcase | ||
| if hash[item] == nil | ||
| hash[item] = 1 | ||
| else | ||
| hash[item] += 1 | ||
| end | ||
| end | ||
| hash | ||
| end | ||
|
|
||
| def decreasing_order_word_count | ||
| map_arr = map_words_to_count.to_a | ||
| map_arr = map_arr.sort_by {|tuple| tuple[1]}.reverse | ||
| 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. whoa. This doesn't need to be so complicated. Try
Author
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 would removing lines 28 - 32 and replacing out_array.sort_by with map.sort_by be a possible solution? @sjreich 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. Yeah, that sounds about right. |
||
| end | ||
|
|
||
| speechParser = Parser.new './speech.txt' | ||
|
|
||
| map_of_words_and_counts = speechParser.decreasing_order_word_count | ||
|
|
||
| map_of_words_and_counts.each do |item| | ||
| puts "#{item[0]} => #{item[1]}" | ||
| 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.
Right here you're iterating over each line of the file and appending them onto @text one at a time, but there's a method you could use to just read the whole file at once and get the same result. Check out File#read