Basic Profanity filter in Objective-C

Below is an Objective-C function which will return YES if there are no profane words in the input text string else it will return NO. You can use it to check if any profane words are entered by the user.

-(BOOL)profaneWordsFilterInText:(NSString *)inText{

NSArray * listOfProfaneWords = @[@”word1″,  @”word2″,];                  NSError *error = NULL;

NSString *profaneWord = [[NSString alloc] init];

for (profaneWord in listOfProfaneWords) {

 NSRegularExpression * regex = [NSRegularExpressionregularExpressionWithPattern:[NSStringstringWithFormat:@”\\b%@\\b”, profaneWord ] options:NSRegularExpressionCaseInsensitiveerror:&error];

NSUInteger numberOfMatches = [regex numberOfMatchesInString:inText   options:range:NSMakeRange(0, [inText length])];

if (numberOfMatches > 0) {

return NO;

}

}

returnYES;

}

You can replace the word1, word2 and so on in the array listOfProfaneWords with the list of bad words from the link below:-

http://www.bannedwordlist.com   (smearWords.txt)

Feel free to leave a comment if you need any more information.

 

Leave a Reply

Your email address will not be published. Required fields are marked *