Question

I have a text string that I want to check for several conditions on it. I have this function in C# and I would like to port it to objective C ( I am new to it ) The checking function should return true if it contains any of (1) and none of (2)

(1) contains any of the below:

keyword1 

OR

keyword2 AND keyword3

OR

keyword4

(2) should not contain any of those keywords

keyword4

OR

keyword5

OR

keyword6

I have this function done in C# using LINQ:

  public static string ProcessText(string text, string postiveKeywords, string negativeKeywords)
        {
            //prepare the strings to lists
            var simpleTextKeywords = text.Split(' ');

            //trim any other characters
            for (int i = 0; i < simpleTextKeywords.Length; i++ )

            {
                simpleTextKeywords[i] = simpleTextKeywords[i].Trim(
                    '~','!','@','#','$','%','^','&','*','(',')','_','+','-','=','[',']',';','\'','\\',',','.','/','{','}',':','"','|','<','>','?');
            }

            string [] listOfKeywords;
            if ( !string.IsNullOrWhiteSpace(postiveKeywords))
               listOfKeywords = postiveKeywords.Split(',');
            else
                listOfKeywords = new string[] { };

            string[] listOfnegativeKeywords;
            if ( !string.IsNullOrWhiteSpace(negativeKeywords ))
                listOfnegativeKeywords = negativeKeywords.Split(',');
            else
                listOfnegativeKeywords = new string[] { };


            //parse the keywordlist
            var matches = listOfKeywords
                        .Any(OR => OR.Split('&').All(kw => simpleTextKeywords.Contains(kw)
                            && !listOfnegativeKeywords.Any(x => simpleTextKeywords.Any(y => x == y))
                            && !string.IsNullOrWhiteSpace(kw)));
            if (!matches)
            {
                return string.Empty;
            }

            //return the first match
            return listOfKeywords
                        .First(OR => OR.Split('&')
                        .All(kw => simpleTextKeywords.Contains(kw)
                            && !listOfnegativeKeywords.Any(x => simpleTextKeywords.Any(y => x == y))
                            && !string.IsNullOrWhiteSpace(kw)));
        }

Update: Below is the code so far:

+ (bool )ProcessText:(NSString*)text
  andPositiveKeyword:(NSString*)positiveKeywords
  andNegativeKeyword:(NSString*)negativeKeywords
{
    //split the text words
    NSMutableArray* simpleTextKeywords = [(NSArray*)[text componentsSeparatedByString:@" "] mutableCopy];

    //trim unwanted characters
    for (int i=0; i< simpleTextKeywords.count; i++) {
        simpleTextKeywords[i] = [simpleTextKeywords[i] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"~!@#$%^&*()_+-=[];\'\\,./{}:\"|<>?"]];
    }

    NSArray* listOfPositiveKeywords = [[NSArray alloc] init];
    if ( ![NSString stringIsNilOrEmpty:positiveKeywords])
    {
        listOfPositiveKeywords = [positiveKeywords componentsSeparatedByString:@","];
    }

    NSArray* listOfNegativeKeywords = [[NSArray alloc] init];
    if ( ![NSString stringIsNilOrEmpty:negativeKeywords])
    {
        listOfNegativeKeywords = [ negativeKeywords componentsSeparatedByString:@","];
    }

    BOOL matches = false;
    for (int i=0; i< listOfPositiveKeywords.count; i++) {
            //if the keyword has "&"
            //all matches should be true
            //split the &
            NSArray* andKeywords = [listOfPositiveKeywords[i] componentsSeparatedByString:@"&"];
             matches = true;
            for (int j=0; j<andKeywords.count; j++) {
                if ( ![simpleTextKeywords  containsObject:andKeywords[j]] )
                {
                    matches = false;
                    break;
                }
            }
        if ( matches == true )
            break;
    }

    //if there are any matches we have to check if it doesnt have any negative keyword
    if ( matches)
    {
        for ( int i =0; i < listOfNegativeKeywords.count;i++)
        {
            if ( [simpleTextKeywords containsObject:listOfNegativeKeywords[i]])
                matches = false;
        }
    }
    return matches;
}
Was it helpful?

Solution

Convert the lists of positive and negative keywords and text to be checked into instances of NSSet. Then use the NSSet containsObject: method to check for inclusion.

Use the NSString method componentsSeparatedByString: to split the text to be searched into an array of strings.

Use the NSString method stringByTrimmingCharactersInSet: and a set created with the NSCharacterSet method: characterSetWithCharactersInString: to trim unwanted characters.

Example code for trimming:

NSString *trimCharacters = @"~!@#$%^&*()_+-=[];\'\\,./{}:\"|<>?";
NSCharacterSet *trimCharacterSet = [NSCharacterSet characterSetWithCharactersInString:trimCharacters];
NSMutableArray *simpleTextKeywords = [NSMutableArray new];
for (NSString *item in textArray) {
    [simpleTextKeywords addObject:[item stringByTrimmingCharactersInSet:trimCharacterSet]];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top