Mega Code Archive

 
Categories / Ruby / String
 

A pretty good heuristic for matching English words

class String   def word_count     frequencies = Hash.new(0)     downcase.scan(/(\w+([-'.]\w+)*)/) { |word, ignore| frequencies[word] += 1 }     return frequencies   end end %{"this is a test."}.word_count