How to Filter Blocked Words in Python

Scans input text against a moderation blocklist, returning blocked terms and their counts.

Easy Python 3.9+ Aug 9, 2026 AI & LLM integration patterns 12 views 0 copies

Python code

22 lines
Python 3.9+
MODERATION_BLOCKLIST = {"spam", "scam", "fraud", "phishing", "malware", "abuse"}

def scan_text(text: str) -> dict:
    normalized = text.lower()
    words = normalized.replace(".", " ").replace(",", " ").replace("!", " ").replace("?", " ").split()
    
    found_terms = []
    for word in words:
        if word in MODERATION_BLOCKLIST and word not in found_terms:
            found_terms.append(word)
    
    return {
        "blocked": len(found_terms) > 0,
        "blocked_terms": found_terms,
        "term_count": {term: words.count(term) for term in found_terms}
    }

if __name__ == "__main__":
    test_input = "Beware of phishing emails, this is not a scam but could be fraud."
    result = scan_text(test_input)
    print(test_input)
    print(result)

Output

stdout
Beware of phishing emails, this is not a scam but could be fraud.
{'blocked': True, 'blocked_terms': ['phishing', 'scam', 'fraud'], 'term_count': {'phishing': 1, 'scam': 1, 'fraud': 1}}

How it works

The function normalizes text to lowercase and splits on common punctuation to extract words. It checks each word against a set for O(1) lookup, collecting unique blocked terms. A dictionary is built summarizing if the text is blocked, the list of terms, and their counts. This simple approach is effective for basic moderation and can be extended with stemming or regex.

Common mistakes

  • Forgetting to normalize case, missing uppercase variants
  • Not removing punctuation, so 'scam!' is not caught
  • Counting duplicates in the output list, should use set or check membership

Variations

  1. Use regex (re.findall(r'\w+', text.lower())) to extract words more robustly
  2. Embed blocklist in a config file or environment variable for easy updates

Real-world use cases

  • Filtering user-generated content in chat apps to block spam or abusive language.
  • Reviewing AI chatbot responses for disallowed topics before sending to users.
  • Screening support tickets for scam or fraud keywords to route them to priority queues.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from AI & LLM integration patterns

Related tutorials and quizzes for this topic.