Create a function to detect profanities in texts. The function should be robust from…
- User
Create a function to detect profanities in texts. The function should be robust from misspellings.
Assistant
Here is a function that checks for profanities in a text in a robust way to handle misspellings:
import re import nltk from nltk.corpus import stopwords from nltk.stem.porter import PorterStemmer profanity_list = ["fuck", "shit", "damn", "bitch", "asshole"] porter = PorterStemmer() def check_profanity(text): text = text.lower() # Remove stopwords and stem words words = [porter.stem(word) for word in text.split() if word not in stopwords.words('english')] # Check if any word is in profanity list for word in words: if word in profanity_list: return True # Check for misspellings using regex for profanity in profanity_list: if re.search(r'\b'+profanity+r'\w*', word): return True return False
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.