Skip to content
The Chat Archive
openchat-3.5 Arena Human Preference 55K

Make some Regex that removes any form of the n-word and other slurs targeted towards…

  1. 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.

  2. Assistant

    import re

    def censor_slurs(text): # Define the list of slurs to censor slur_list = ["n-word", "other_slur1", "other_slur2"]

    # Create a regex pattern to match the slurs
    pattern = re.compile(r'\b(?:' + '|'.join(slur_list) + r')\b', re.IGNORECASE)
    
    # Replace the slurs with asterisks
    censored_text = pattern.sub('*'*len(slur_list[0]), text)
    
    # Check if any slurs were censored
    if pattern.search(text):
        print("The text contains slurs targeted towards black people. Please do not use these words.")
    
    return censored_text
    

    Test the function with some example text

    example_text = "The n-word is a highly offensive term that should never be used." censored_text = censor_slurs(example_text) print(censored_text)

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.

Identical first prompt