0

If there are two separate strings, we need to retrieve all words that are matching (in those strings), in an array. also this matching should be case insensitive. What will be the most efficient way to achieve this.

For Example:

NSString *s1 = @"Hello there, I want to match similar words.";  
NSString *s2 = @"Do you really want to match word, hello?";  

The resulting array should contain, "hello", "want", "to", "match".

I went through many questions and, responses over the net like Regular expression to match a line that doesn't contain a word?, but didn't, find solution needed.

Community
  • 1
  • 1
rptwsthi
  • 10,094
  • 10
  • 68
  • 109

4 Answers4

2

try this:

NSString *s1 = @"Hello there, I want to match similar words.";  
NSString *s2 = @"Do you really Want to match word, hello?";
NSCharacterSet *separatorSet = [NSCharacterSet characterSetWithCharactersInString:@" ?.,"];
NSArray *as1 = [[s1 lowercaseString] componentsSeparatedByCharactersInSet:separatorSet];
NSArray *as2 = [[s2 lowercaseString] componentsSeparatedByCharactersInSet:separatorSet];

NSMutableSet* set1 = [NSMutableSet setWithArray:as1];
NSMutableSet* set2 = [NSMutableSet setWithArray:as2];

[set1 intersectSet:set2];
[set1 removeObject:@""];
NSArray* result =[set1 allObjects];
NSLog(@"%@",result);
Hector
  • 3,909
  • 2
  • 30
  • 46
  • You were awesome with your response, actually this is where I reached fially, but Aravindhanarvi was one step ahead, as you see. thank you very much, this answers will be helpful to many ones, I belive. Thanks Again. – rptwsthi Mar 06 '12 at 08:20
1

You can use NSMutableSet for this..

    NSString *s1 = @"Hello there, I want to match similar words.";  
    NSString *s2 = @"Do you really want to match word, hello?"; 

    NSArray *components = [s1 componentsSeparatedByString:@" "];
    NSArray *components1 = [s2 componentsSeparatedByString:@" "];
    NSLog(@"%@",components);
    NSLog(@"%@",components1);

    NSMutableSet *resultSet = [NSMutableSet setWithArray:components1];
    NSSet *setA = [NSSet setWithArray:components];
    [resultSet intersectSet:setA];
    NSArray *result = [resultSet allObjects];
    NSLog(@"%@",result);

This Solution will work for you, If you don't have any punctuations in the sentences.. If you want sentences with punctuations to work just remove all the punctuations in your sentences.

Aravindhan
  • 15,608
  • 10
  • 56
  • 71
  • Wont work because @"Hello" and @"hello" are not equal so they will not be in the result array. – Alex Terente Mar 03 '12 at 07:27
  • Tanks @Aravindhanarvi for such a concern and quick response, it's worked good after adding little logic. as capital letters are been converted into small ones before comparing. – rptwsthi Mar 06 '12 at 08:24
1
NSString *s1 = @"Hello there, I want to match similar words.";  
NSString *s2 = @"Do you really want to match word, hello?";  

NSCharacterSet *separatorSet = [NSCharacterSet characterSetWithCharactersInString:@" ?.,"];
NSArray *s1Array = [s1 componentsSeparatedByCharactersInSet:separatorSet];
NSArray *s2Array = [s2 componentsSeparatedByCharactersInSet:separatorSet];

NSMutableArray *resultArray = [NSMutableArray arrayWithCapacity:0];
for(int index = 0; index < [s1Array count]; index++){
    NSString *compareString = [[s1Array objectAtIndex:index] lowercaseString];

    NSUInteger findIndex = [s2Array indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {

        NSString *currentString = (NSString *)obj;
        if([[currentString lowercaseString] isEqualToString:compareString] &&
           ![currentString isEqualToString:@""]){
            return YES;
            *stop = YES;
        }else{
            return NO;
        }

    }
                            ];
    if(findIndex !=NSNotFound){
        [resultArray addObject:compareString];
    }
}
NSLog(@"Result:%@",[resultArray description]);
Alex Terente
  • 12,006
  • 5
  • 51
  • 71
1

Try with below code

NSString *s1 = @"Hello there, I want to match similar words.";  
        NSString *s2 = @"Do you really want to match word, hello?";
        NSCharacterSet *doNotWant = [[NSCharacterSet letterCharacterSet] invertedSet];
        NSArray *array1 = [[s1 uppercaseString] componentsSeparatedByCharactersInSet:doNotWant];
        NSArray *array2 = [[s2 uppercaseString] componentsSeparatedByCharactersInSet:doNotWant];

        NSSet *set1 = [[NSSet alloc] initWithArray:array1];
        NSMutableSet *set2 = [[NSMutableSet alloc] initWithArray:array2];
        [set2 intersectSet:set1];
        NSLog(@"set2:%@",set2);
        NSLog(@"set1:%@",set1);
        NSArray *aray_res = [set2 allObjects];
        NSLog(@"aray_res:%@",aray_res);
Narayana Rao Routhu
  • 6,303
  • 27
  • 42