typestar

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

  1. scan walks every word match and feeds a Hash.new(0) counter.
  2. sort_by with [-n, word] ranks by count, then alphabetically.
  3. format pads columns so the histogram bars line up.

Keywords and builtins used here

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.

Type this snippet

Step 1 of 2 in Encore, step 28 of 29 in Language basics.

← Previous Next →

Word frequency in other languages