Word Frequency: Using Hashes and Arrays
Let’s round out this discussion of hashes and arrays with a program that calculates the number of times each word occurs in some text. (So, for example, in this sentence, the word the occurs two times.)
The problem breaks down into two parts. First, given some text as a string, return a list of words. That sounds like an array. Then, build a count for each distinct word. That sounds like a use for a hash—we can index it with the word and use the corresponding entry to keep a count.
Let’s start with the method that splits a string into words:
| def words_from_string(string) |
| string.downcase.scan(/[\w']+/) |
| end |
This method uses two useful string ...
Get Programming Ruby 3.3 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.