Better Profanity
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
- breaking In version 0.3.0, the characters '&' and '^' were removed from the list of default word dividers. This means that leetspeak or disguised profanity using these characters (e.g., 'f&ck') might no longer be detected as profanity unless explicitly added to a custom wordlist or separated by other dividers.
- gotcha Calling `profanity.load_censor_words()` (or `load_censor_words_from_file()`) will *replace* the entire active wordlist by default, not append to it. If you wish to add words to an existing wordlist, use `profanity.add_censor_words()` instead.
- gotcha The default wordlist has been significantly updated in versions 0.3.4 and 0.7.0. This means that profanity detection behavior can change for applications simply by upgrading the library, potentially detecting more words or different leetspeak variations, even without any code changes.
- gotcha The `requires_python` specifier on PyPI is `==3.*`, meaning the library officially supports any Python 3.x version (e.g., 3.0, 3.1, ..., 3.12), but explicitly *not* Python 4.x or later. Historically, there have also been specific compatibility fixes for Python 3.5, suggesting subtle issues might arise with less common or very old 3.x releases.
Install
-
pip install better-profanity
Imports
- profanity
from better_profanity import profanity
Quickstart
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}")