Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def frequent_words(text)
- text.downcase!
- words = text.scan(/[[:alpha:]']+/)
- frequency = words.tally
- if frequency.length >= 3
- sorted = frequency.sort_by { |key, value| -value }
- sorted.each_with_index do |element, index|
- puts "#{index + 1} most frequent word is: #{element}" if index < 3
- end
- else
- words.clear
- puts "Not enough words!\n#{words}"
- end
- end
- frequent_words "hi, there, D'ark brave silly d'Ark, HI, hI) brAvE, d'ARK -hi"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement