from better_profanity import profanity # profanity means dirty words
# Ignore case when reviewing elements
# profanity.censor function
""" Replace dirty words 1. profanity.censor You can review the dirty words in the text , By default, each dirty word will be replaced with four asterisk characters (****) The reviewed text censored_text_1 in : - dirty language Fuck and jerk Will be replaced by **** 2. profanity.censor Don't worry about words ( dirty language ) Separator between , Whether it's (,/./_), But in addition to (@/*/'/"/$/) The reviewed text censored_text_2 in : - dirty language Fuck Separated from other characters by commas , dirty language jerk Separated from other characters by dots and underscores , But they can still be examined by this method 3. profanity.censor The custom review of can change the replacement character ( Replace the asterisk with other characters ) The observation function profanity.censor(self, text, censor_char="*") You can know the keyword parameters censor_char You can replace the default style The reviewed text censored_text_3 in : - dirty language Fuck and jerk Will be replaced by ---- """
censored_text_1 = profanity.censor('Fuck You’re a jerk!')
print(censored_text_1) # ****,You’re a ****!
censored_text_2 = profanity.censor('Fuck,You’re a_jerk.!')
print(censored_text_2) # ****,You’re a_****.!
censored_text_3 = profanity.censor('Fuck,You’re a jerk!', '-')
print(censored_text_3) # ----,You’re a ----!
# profanity.contains_profanity() function
""" Judge whether the string contains dirty words You can check whether the string contains dirty words , Return if it contains True, Otherwise return to False - Fuck You’re a jerk! return True - You are a good boy. return False """
print(profanity.contains_profanity('Fuck You’re a jerk!')) # True
print(profanity.contains_profanity('You are a good boy.')) # False
# profanity.load_censor_words() function
# profanity.load_censor_words_from_file() function
""" Load a single set of review words ( No matter which of the following methods can only be loaded once ) load_censor_words(custom_bad_words_list) You can load the words in the current list into the review Library - You are a good boy. Obviously, it will not be censored out of dirty words , But when we're going to boy and good Add to the dirty words library , Then it will be censored as dirty words profanity.load_censor_words_from_file(my_bad_words_file) - You are a good boy. Obviously, it will not be censored out of dirty words , But when we're going to You and are Add to the dirty words library , Then it will be censored as dirty words Cancel loading review words - Call directly profanity.load_censor_words() """
custom_bad_words_list = ['good', 'boy'] # custom_bad_words_list( Custom dirty words list )
profanity.load_censor_words(custom_bad_words_list)
print(profanity.contains_profanity('You are a good boy.')) # True
censored_text_4 = profanity.censor('You are a good boy.')
print(censored_text_4) # You are a **** ****.
profanity.load_censor_words_from_file('my_bad_words.txt')
censored_text_5 = profanity.censor('You are a good boy.')
print(censored_text_5) # **** **** a good boy.
# profanity.load_censor_words() function
# profanity.load_censor_words_from_file() function
""" Whitelist words ( Make it not become dirty words for the time being ) --- keyword whitelist_words """
custom_bad_words_list = ['good', 'boy']
profanity.load_censor_words(custom_bad_words_list, whitelist_words=['good'])
censored_text_6 = profanity.censor('You are a good boy.')
print(censored_text_6) # You are a good ****.
profanity.load_censor_words_from_file('my_bad_words.txt', whitelist_words=['are'])
censored_text_7 = profanity.censor('You are a good boy.')
print(censored_text_7) # **** are a good boy.
# profanity.add_censor_words() function
""" Add more review words ( It can be increased many times ) """
profanity.load_censor_words_from_file('my_bad_words.txt', whitelist_words=['are'])
profanity.add_censor_words(custom_bad_words_list)
censored_text_8 = profanity.censor('You are a good boy.')
print(censored_text_8) # **** are a **** ****.
# Limitations
""" The examination of words is carried out according to a single character , Then it's easy to bypass censorship by adding a single character """
profanity.load_censor_words()
censored_text_9 = profanity.censor('Fuck,You’re a jerk!')
print(censored_text_9) # ****,You’re a ****!
censored_text_10 = profanity.censor('Fuckk,You’re a jerkk!')
print(censored_text_10) # Fuckk,You’re a jerkk!