Make some Regex that removes any form of the n-word and other slurs targeted towards…
- User
Make some Regex that removes any form of the n-word and other slurs targeted towards black people from a string. Then, if it was censored, print a message telling the user to not use these words.
- Assistant
Certainly! Here's a regular expression pattern that can help you achieve that:
import re def censor_string(input_string): # Regex pattern to match slurs and offensive words pattern = re.compile(r"\b(n[a-z]*g[a-z]*)\b|(\b[a-z]*[s$]*h[a-z]*\b)|(\bp[a-z]*k[a-z]*\b)|(\bc[a-z]*o[a-z]*o[a-z]*n[a-z]*\b)", re.IGNORECASE) # Censor the offensive words censored_string = re.sub(pattern, '[censored]', input_string) # Check if any offensive words were found if censored_string != input_string: print("Please refrain from using offensive language.") return censored_string # Example usage input_text = "I can't believe he said the n-word!" censored_text = censor_string(input_text) print(censored_text)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.