Skip to main content

A simple word filter module for Python

Project description

WordFilter2

A powerful and customizable library for filtering profanity in Python.

PyPI version

Features


  • Case-insensitive filtering
  • Full or partial matching
  • Custom replacement functions
  • Load from local file or URL
  • Supports multiple formats (txt, json)
  • Normalize spaces in filtered output
  • Optional duplicate word detection
  • Find matched words in text
  • Configurable timeout and size limits for remote files

Installation


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.

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 you can use built-in utils:

from wordfilter2 import WordFilter
from wordfilter2.utils import (mask_full, mask_partial, mask_first_last,
                               mask_censor, mask_custom)

wf = WordFilter(replace_with_func=mask_partial("#"))
wf.add_word("spoiler")

print(wf.filter("This is a spoiler."))  # Output: This is a s#####r.

Options


# Optional parameters:
wf = WordFilter(
    ignore_case=True,
    partial_match=True,
    replace_with="*",
    replace_with_func=None,
    normalize_spaces=True
)

Check for Profanity


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

Working with Files


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

# Load words from URL with timeout and size limit
wf.load_from_url("https://your_url/goes_here.json",  timeout=5, max_size=1024 * 1024)

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

License


This project is licensed under the MIT License. See LICENSE 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-1.2.0.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

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

wordfilter2-1.2.0-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for wordfilter2-1.2.0.tar.gz
Algorithm Hash digest
SHA256 31c8393feeb431b7645b324727b9dc11311d1668f312f756e7df289705724fb7
MD5 ccee82424781b632636aa5abf5449315
BLAKE2b-256 3f020ac92046b476a66b0e8afbeb683b52d0897b7c932fa0e9fa9693816451ec

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for wordfilter2-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 754cec50a9e6c6431054486228d48edad2bcb795a359e9a76a654845fcd1be0e
MD5 7b5599a57517f3a446dd6c7415ef345d
BLAKE2b-256 25485ef117bd797119219a26d1273db2ea281ff7c7acbfb3daadca032ec528f2

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