Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,943 questions

55,787 answers

573 users

How to find the N most frequent non‑stopwords in a text in Python

1 Answer

0 votes
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

'''

 



answered 1 day ago by avibootz
...