June 2007
Intermediate to advanced
294 pages
8h 6m
English
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.
#!/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 ...Read now
Unlock full access