How to Filter Blocked Words in Python
Scans input text against a moderation blocklist, returning blocked terms and their counts.
Python code
22 linesMODERATION_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
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
- Use regex (re.findall(r'\w+', text.lower())) to extract words more robustly
- 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
More from AI & LLM integration patterns
- Cache LLM Completions by Hashing the Prompt in Python easy
- Chain of Thought Prompting in Python: Step-by-Step Reasoning Demo easy
- Circuit Breaker Pattern in Python for LLM API Calls medium
- Cosine Similarity to Retrieve Top K Chunks in Python easy
- Demonstrate Prompt Injection Bypass in Python easy
- How to Accumulate Streamed Tokens into a Final String in Python easy
Keep learning
Related tutorials and quizzes for this topic.