Skip to main content

A simple word filter module for Python

Project description

wordfilter2

wordfilter2 is a lightweight and flexible Python module for filtering and censoring words in text. It is designed to detect and optionally replace offensive, sensitive, or unwanted terms using customizable options such as case sensitivity, partial matching, and replacement strategies.

Features


  • ✅ Add or remove custom words to filter
  • 🔍 Detect profanity or unwanted words in a given text
  • 🧩 Optional partial or exact word matching
  • 🔠 Case-insensitive or case-sensitive filtering
  • ✏️ Replace filtered words with a character or a custom function
  • 📁 Load word lists from files and save them back

Installation


Install with PyPI:

pip install wordfilter2

Usage


from wordfilter2 import WordFilter

# Initialize the filter
wf = WordFilter(ignore_case=True, partial_match=False, replace_with="*")

# Add words to filter
wf.add_words(["badword", "offensive"])

# Filter text
filtered = wf.filter("This is a badword example.")
print(filtered)  # Output: This is a ******* example.

# Check if text contains profanity
contains = wf.contains_profanity("Something offensive here.")
print(contains)  # Output: True

Custom Replacement


from wordfilter2 import WordFilter

def censor(word: str) -> str:
    return "[" + word.upper() + "]"

wf = WordFilter(replace_with_func=censor)
wf.add_word("spoiler")
print(wf.filter("This is a spoiler."))  # Output: This is a [SPOILER].

or

from wordfilter2 import WordFilter

wf = WordFilter(replace_with_func=lambda word: "[" + word.upper() + "]")
wf.add_word("spoiler")
print(wf.filter("This is a spoiler."))  # Output: This is a [SPOILER].

Working with Files


# Load words from a .txt file (one word per line)
wf.load_from_file("banned_words.txt")

# Save the current word list
wf.save_to_file("output_words.txt")

License


This project is licensed under the MIT License. See LICENSE.md for details.


Clean content made simple with wordfilter2.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wordfilter2-0.2.0.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wordfilter2-0.2.0-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file wordfilter2-0.2.0.tar.gz.

File metadata

  • Download URL: wordfilter2-0.2.0.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for wordfilter2-0.2.0.tar.gz
Algorithm Hash digest
SHA256 101262728baf0f62ae5a99e619fb5c5320ab0478eea0aaa380ba475442a06982
MD5 d57a5aef242731685b5c5d400d5bd025
BLAKE2b-256 a893ec94cc02e224b5f325cac4189261f998582198aaed2504ba50cd2e00d9a4

See more details on using hashes here.

File details

Details for the file wordfilter2-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: wordfilter2-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for wordfilter2-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3de49f0b1787d1063c7de27c2e7e5b16508552215494c5035b2abc68df2d8303
MD5 21bebc471882f30157409e6d19fbe588
BLAKE2b-256 a0e4811cfda658da5973258727256335d6479c2ba94ac8c45497795c7d51b905

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page