-
Notifications
You must be signed in to change notification settings - Fork 36
FreqOfWords #45
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?
FreqOfWords #45
Conversation
CountingWords.rb
Outdated
| @@ -0,0 +1,6 @@ | |||
| freq = Hash.new(0) | |||
| file = File.open("speech.txt") | |||
| file.read.downcase.scan(/\b[a-z]{3,16}\b/){|word| freq[word] += 1} | |||
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.
how would you rewrite this to handle punctuation?
your script also doesn't handle words less than 3 characters? how would you rewrite this to take those into account?
| file = File.open("speech.txt") | ||
| file.read.downcase.scan(/\b[a-z]{3,16}\b/){|word| freq[word] += 1} | ||
| freq = freq.sort_by{|a, b| b} | ||
| freq.reverse! |
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.
why not use .reverse here?
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.
.reverse made the counters go from 1 to 33. .reverse! allowed it to go from 33 to 1.
CountingWords.rb
Outdated
| @@ -0,0 +1,6 @@ | |||
| freq = Hash.new(0) | |||
| file = File.open("speech.txt") | |||
| file.read.downcase.scan(/\b[a-z]{3,16}\b/){|word| freq[word] += 1} | |||
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 is the first time I think I've seen someone use .scan. me likes it but keep in mind the issues i stated in the other comment. 👍
| @@ -0,0 +1,6 @@ | |||
| freq = Hash.new(0) | |||
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 is something that I learned later on that you can initialize a hash with default values. Kind of cool. Well done.
Anytime you build up an empty collection, iteratively build it up then utilize it as input for something else, you should always consider .reduce or a similar method as possibly a solution. in this case you're using .scan but reduce would have worked as well as .each_with_object.
http://batsov.com/articles/2013/12/04/using-rubys-each-with-object/
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.
Hash with default values 😮 😮
|
Looks good. I'd also recommend encapsulating your stuff inside of a function or class. it's good practice when starting out. |
@jaybobo @jack-chester