#
# This program finds the N most frequently appearing words in a text
# after removing stopwords. It demonstrates clean structure, clear
# comments, and efficient use of Ruby arrays, hashes, and sorting.
#
# ---------------------------------------------------------------
# Tokenize text into words (simple whitespace split)
# ---------------------------------------------------------------
def tokenize(text)
words = []
# Split on whitespace
text.split(/\s+/).each do |w|
# Remove punctuation at the edges
w = w.gsub(/\A[[:punct:]]+/, "")
w = w.gsub(/[[:punct:]]+\z/, "")
words << w.downcase unless w.empty?
end
words
end
# ---------------------------------------------------------------
# Count word frequencies, skipping stopwords
# ---------------------------------------------------------------
def count_words_frequencies(words, stopwords)
freq = Hash.new(0)
words.each do |w|
freq[w] += 1 unless stopwords.include?(w)
end
freq
end
# ---------------------------------------------------------------
# Extract the top N most frequent words
# ---------------------------------------------------------------
def top_n(freq, n)
# Convert hash to array of [word, count]
items = freq.map { |word, count| [word, count] }
# Sort by frequency descending, then alphabetically
items.sort_by! { |word, count| [-count, word] }
items.first(n)
end
# ---------------------------------------------------------------
# Main
# ---------------------------------------------------------------
text =
"C is a general-purpose programming language created in 1972 by " \
"Dennis Ritchie. C gives programmers direct access to the features " \
"of CPU. It has been and continues to be used to implement " \
"operating systems (especially kernels) and device " \
"drivers. C programming language used on computers ranging from " \
"supercomputers to microcontrollers and embedded systems."
stopwords = %w[
the is a to how after but this for by in
and can content be you yes no next about used
access been continues
]
# Tokenize
words = tokenize(text)
# Count frequencies
freq = count_words_frequencies(words, stopwords)
# Get top n
n = 7
topn = top_n(freq, n)
# Print results
puts "Top #{n} most frequent non-stopwords:"
topn.each do |word, count|
puts "#{word} : #{count}"
end
#
# run:
#
# Top 7 most frequent non-stopwords:
# c : 3
# language : 2
# programming : 2
# systems : 2
# 1972 : 1
# computers : 1
# cpu : 1
#