0

I am developing a iOS Chat Application, Here i have a scenario where i have to identify user entered text contains any Abusing words on my submit button action.I have a big list of abuse words to identify.

I have used the below code to identify the abusing words ,but iam facing a problem here for example. my list of abusing words Array contains "Hell". if i enter "Hello" in textfield its showing as abusing word ,because Hello contains Hell.

Here is my code

   NSArray *arrayOfStrings = [[rateQuery findObjects] mutableCopy];
arrayOfStrings=[arrayOfStrings valueForKey:@"Words"];//List of Abusing words

NSString *stringToSearchWithin = nameFld.text;//user entered text
__block NSString *result = nil;
[arrayOfStrings indexOfObjectWithOptions:NSEnumerationConcurrent
                             passingTest:^(NSString *obj, NSUInteger idx, BOOL *stop)
 {
     if ([stringToSearchWithin rangeOfString:obj].location != NSNotFound)
     {
         result = obj;
         *stop = YES;
         //return YES;
     }
     return NO;
 }];
if (!result){
    NSLog(@"The string does not contain any of the strings from the arrayOfStrings");

}
else
{
    NSLog(@"The string contains Abusing words");
}

 
Community
  • 1
  • 1
  • I'd suggest you use a `NSRegularExpression`. – Larme Jun 01 '15 at 14:45
  • You should test if a space, questionmark etc is in front of or/and behind that word. If something is in front of it and something behind it, it should work. Regex would be nice like @Larme mentioned. –  Jun 01 '15 at 14:46
  • take a look at this https://github.com/IslandOfDoom/IODProfanityFilter – rob180 Jun 02 '15 at 10:23
  • @rob180 The sample which you have sent is not working for multiple words .for example if i have to search for more than one word its not working. – S Murali krishna Jun 04 '15 at 07:38

3 Answers3

1

Break each message into an array of words, and then just loop through each word.

Ideally, though, you should look into regular expression (regex). Cocoa supports regex through the use of NSRegularExpression.

emma ray
  • 13,336
  • 1
  • 24
  • 50
  • Its working fine if i Keep like this NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\b(apple)\\w++\\b"options:NSRegularExpressionCaseInsensitive error:&errRegex]; But i am not able to understand where i have to pass my array of string – S Murali krishna Jun 04 '15 at 07:42
0

You could modify this

if ([stringToSearchWithin rangeOfString:obj].location != NSNotFound)

to the following:

if ([stringToSearchWithin rangeOfString:obj].location != NSNotFound && stringToSearchWithing.length == obj.length)

Another approach could be to split the text by spaces or other separators and then check to see if those words are contained by your words array.

Teo
  • 3,394
  • 11
  • 43
  • 73
0

Eventually i solved my problem with below code .

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

     NSArray * listOfProfaneWords = [[self wordSet] allObjects];
     NSError *error = NULL;

     NSString *profaneWord ;

     for (profaneWord in listOfProfaneWords) 
     {
       NSString *as=[NSString stringWithFormat:@"\\b%@\\b", profaneWord ];

    NSRegularExpression * regex = [NSRegularExpression regularExpressionWithPattern:as options:NSRegularExpressionCaseInsensitive  error:&error];

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

    if (numberOfMatches > 0)
    {
        return YES;
    }
}
return NO;
}