import string
from collections import Counter
"""
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 Python lists, dictionaries, and sorting.
"""
# ---------------------------------------------------------------
# Tokenize text into words (simple whitespace split)
# ---------------------------------------------------------------
def tokenize(text: str) -> list[str]:
words = []
# Split on whitespace
for w in text.split():
# Remove punctuation at the edges
w = w.strip(string.punctuation)
if w:
words.append(w.lower())
return words
# ---------------------------------------------------------------
# Count word frequencies, skipping stopwords
# ---------------------------------------------------------------
def count_words_frequencies(words: list[str], stopwords: set[str]) -> dict[str, int]:
freq = Counter()
for w in words:
if w not in stopwords:
freq[w] += 1
return dict(freq)
# ---------------------------------------------------------------
# Extract the top N most frequent words
# ---------------------------------------------------------------
def top_n(freq: dict[str, int], n: int) -> list[tuple[str, int]]:
# Convert dict to list of (word, count)
items = list(freq.items())
# Sort by frequency descending, then alphabetically
items.sort(key=lambda x: (-x[1], x[0]))
return items[:n]
# ---------------------------------------------------------------
# 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 = {
"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
print(f"Top {n} most frequent non-stopwords:")
for word, count in topn:
print(f"{word} : {count}")
'''
run:
Top 7 most frequent non-stopwords:
c : 3
language : 2
programming : 2
systems : 2
1972 : 1
computers : 1
cpu : 1
'''