O'Reilly logo

Ruby by Example by Kevin C. Baird

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

#13 Word Histogram (most_common_words.rb)

And now for something that most word processors don’t do: finding the most commonly used words in a document. Like the previous script, it adds an additional “helper” method to an existing built-in class to simplify the job for our new main method. Let’s take a look.

The Code

  #!/usr/bin/env ruby
  #most_common_words.rb

  class Array

❶   def count_of(item)
❷     grep(item).size    The grep Method
❸     #inject(0) { |count,each_item| item == each_item ? count+1 : count } end end ❹ def most_common_words(input, limit=25) freq = Hash.new() sample = input.downcase.split(/\W/) sample.uniq.each do |word| ❺ freq[word] = sample.count_of(word) unless word == '' end ❻ words = freq.keys.sort_by do |word| freq[word] end.reverse.map do ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required