Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

class Parser
def initialize(filename)
@file = File.open(filename, "r")
@text = ''
@file.each_line do |txt|
@text = @text + txt
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@text += txt

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did not know += worked in Ruby. Good to know!

end
end

def array_of_words
@text.split(/\W+/)
Copy link

Choose a reason for hiding this comment

The 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.

Choose a reason for hiding this comment

The 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

Copy link

Choose a reason for hiding this comment

The 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
obj = {}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In ruby, it's called a hash, not an object.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha, forgot about that one, thanks!

array.each do |item|
item = item.downcase
obj[item] = obj[item] + 1 if obj[item] != nil
obj[item] = 1 if obj[item] == nil
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe try to combine these two lines into a single if-else? (Also: += .)

end
obj
end

def decreasing_order_word_count
map = map_words_to_count
out_array = []
map.each do |item|
new_obj = {name: item[0], value: item[1]}
out_array.push new_obj
end
out_array.sort_by {|item| item[:value]}.reverse
end
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whoa. This doesn't need to be so complicated. Try .sort_by { |_key, value| value } on a hash.

Copy link
Author

Choose a reason for hiding this comment

The 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

Copy link

Choose a reason for hiding this comment

The 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[:name]}, #{item[:value]}"
end