Fine Tuning the Word Count


I successfully created my Histogram but I’d like to do a few more simple formatting options. For example. The word ‘dude’ and the word ‘dude.’ are counted as separate words. In a true word count, it would be ‘dude’ 2 times. Is there a line I can add to my code to delete periods, or rather, not include them, before they make it to my Hash? What about question marks? Exclamation points! etc etc.

I ran into this question in some earlier exercises as well, where punctuation threw off my output.

puts "Slam some poetry, broh:"
text = gets.chomp

words = text.split

frequencies =

words.each {|word| frequencies[word] += 1}

frequencies = frequencies.sort_by do |word, count| #seems to need both the key and value to to function properly, word is key, count is the value for frequency.

frequencies.each do |word, count|
  puts word.capitalize + " " + count.to_s #converts the number in the count to a string


Regexps are useful for extracting stuff out of text:

> 'dude dude.'.scan(/\w+/)
=> ["dude", "dude"]


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.