Introduction
You may have noticed in some movies or videos, obscene words are beeped. It is done manually. But in this tutorial, we're gonna learn about profanity filters in python. We will censor bad words from a text using python programs.
Profanity filters help to detect and censor profane language from user-generated content within websites, social platforms, etc. Comptroller can modify the list of words related to hate speech, sexual content, swear words, etc.
There is one python package, named better-profanity, and one python library called, profanity-filter to detect and censor bad words from a text. We'll use the second one here. Because it is more advanced than better-profanity.
Let's see the features of the profanity-filter.
1. It allows to censor a full text or single words.
2. It supports the English and Russian languages including the texts written in the mixed language.
3. Deep Analysis(Not only detects exact matches but also matches distorted and derivative words).
4. Partial word censoring, and many more.
🎥Visit Also: Create a Screen Recorder📽 using Python - Very Easy to Use
Requirements
Follow the steps given below before writing any code.
1. Install profanity-filter: pip install profanity-filter
2. Install language: python -m space download en
Now you can start coding.
Censor bad words from a text
from profanity_filter import ProfanityFilter
pf = ProfanityFilter()
print(pf.censor("You've stolen my money, you bastard!"))
# Censor individual words
print(pf.censor_word('lesbian'))
Output
You've stolen my money, you *******!
*******
Censor Russian Text
from profanity_filter import ProfanityFilter
# Select Russian Language
pf = ProfanityFilter(languages=['ru'])
#Блядь == F**k...
print(pf.censor("Блядь"))
Output
*******
Censor obscene words with custom character
from profanity_filter import ProfanityFilter
pf = ProfanityFilter()
pf.censor_char = '$'
print(pf.censor("You look like a shit"))
Output
You look like a $$$$
Check whether or not the string contains any swear words
from profanity_filter import ProfanityFilter
pf = ProfanityFilter()
bad_text = "You are a bitch!"
# Check if the string contains any bad words or not
print("Not bad words: ", pf.is_clean(bad_text))
print("Bad words: ", pf.is_profane(bad_text))
Output
Not bad words: False
Bad words: True
Censor obscene words with a custom word list
Example 1
Here we'll add our own custom word list. But there is a problem. By doing so, the program can filter only those words present in the custom list. Let's see the instance.
from profanity_filter import ProfanityFilter
pf = ProfanityFilter()
pf.custom_profane_word_dictionaries = {'en': {'want', 'marry'}}
print(pf.censor("I want to marry her., shit"))
Output
I **** to ***** her., shit
Example 2
In this example, we'll resolve the above problem. First, restore the default profane word dictionaries then add a custom word dictionary into it: ProfanityFilter.extra_profane_word_dictionaries().
from profanity_filter import ProfanityFilter
pf = ProfanityFilter()
pf.custom_profane_word_dictionaries = {'en': {'want', 'marry'}}
print(pf.censor("I want to marry her., shit"))
# Restore the default profane word dictionaries
pf.restore_profane_word_dictionaries()
# A simple change in the code, custom -> extra
pf.extra_profane_word_dictionaries = {'en': {'want', 'marry'}}
print(pf.censor("I want to marry her., shit"))
Output
I **** to ***** her., shit
I **** to ***** her., ****
🍁Visit Also: Create a Watermark on a Photo using Python - Add Your logo
Conclusion
In this tutorial, you learned to censor obscene words from a text or string using the Profanity Filter library in python. We've seen many programming examples here.
Profanity Filter also can censor distorted and derivative words along with profane words. But to get the functionality of deep analysis of the library we need to install additional libraries and dictionaries for your selected language. It's quite a sensitive and lengthy process.
If you want a separate tutorial on that, just let me know in the comment section below. I will create another topic on that.
Thanks for reading!💙
Why didn't use better profanity?
ReplyDeleteThanks!
ReplyDelete