Skip to content
Open
Changes from all commits
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
41 changes: 41 additions & 0 deletions parser.rb
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|

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

@text += txt
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
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
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[0]} => #{item[1]}"
end