Word frequency in Ruby
A complete script: scan text, count words, print a ranked table.
#!/usr/bin/env ruby
# Count word frequencies in a small corpus and print a top-five table.
TEXT = <<~PROSE
the quick brown fox jumps over the lazy dog
the dog barks and the fox runs
quick thinking wins the day
PROSE
counts = Hash.new(0)
TEXT.scan(/[a-z]+/) { |word| counts[word] += 1 }
widest = counts.keys.map(&:length).max
top = counts.sort_by { |word, n| [-n, word] }.first(5)
top.each do |word, n|
bar = "#" * n
puts format("%-#{widest}s %2d %s", word, n, bar)
end
puts "#{counts.size} distinct words, #{counts.values.sum} total"
How it works
scanwalks every word match and feeds aHash.new(0)counter.sort_bywith[-n, word]ranks by count, then alphabetically.formatpads columns so the histogram bars line up.
Keywords and builtins used here
doendformatputs
The run, in numbers
- Lines
- 21
- Characters to type
- 538
- Tokens
- 124
- Three-star pace
- 80 tpm
At the three-star pace of 80 tokens a minute, this run takes about 93 seconds.
Step 1 of 2 in Encore, step 28 of 29 in Language basics.