Better Profanity

0.7.0 · active · verified Sat Apr 11

Better Profanity is a blazingly fast Python library for cleaning swear words and their leetspeak variations from strings. It provides an efficient way to detect and replace offensive language using a pre-loaded or custom wordlist. The library is actively maintained, with the current version being 0.7.0, and receives regular updates for performance and wordlist improvements.

Warnings

Install

Imports

Quickstart

Initialize the profanity filter by loading the default wordlist, then use `replace_profanity` to clean strings. You can add custom words with `add_censor_words` or check for profanity with `contains_profanity`.

from better_profanity import profanity

# Load the default wordlist
profanity.load_censor_words()

text_with_profanity = "This is a f*cking example with some sh!t words."
censored_text = profanity.replace_profanity(text_with_profanity)
print(f"Original: {text_with_profanity}")
print(f"Censored: {censored_text}")

# Adding custom words (adds to the existing list)
profanity.add_censor_words(['darn', 'heck'])
custom_text = "Oh, darn, that was a heck of a game."
censored_custom_text = profanity.replace_profanity(custom_text)
print(f"Original (custom): {custom_text}")
print(f"Censored (custom): {censored_custom_text}")

# Check if profanity is present
contains_profanity = profanity.contains_profanity("This sentence has a badword.")
print(f"Contains profanity: {contains_profanity}")

view raw JSON →