here in this code i have a sentence that i convert it to an array, then i have another array which is a stoplist, i want to filter my sentence by stoplist, means the final sentence should not contain the stoplist elements!
it seems to be so easy, i killed myself to got it worked, but it never worked! gosh !
could you plz tell me what is the problem ?
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *sentence=[NSString stringWithString:@"i want to filter this sentence by stoplist array for my program"];
NSArray *stopList=[[NSArray alloc]initWithObjects:@"an”,@“and”,@“by”,@“for”,@“from”,@“of”,@“the”,@“to”,@“with",nil];
NSArray *query = [sentence componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSMutableArray *finalsentence=[NSMutableArray array];
for (NSString *word in query) { // for each word in the query...
if (![stopList containsObject:word]) {
// ... if the stopList does not contain the word...
[finalsentence addObject:word]; // ...add it to the final sentence
}
}
NSLog(@"%@",finalsentence);
[pool drain];
return 0;
}
Well, one issue is what is “q” in
[finalsentence addObject:q]? You have no ‘q’ defined in your code.The other issue is it seems you have your test backwards from the description you gave. Your description says you want to add each word to ‘finalsentence’ if that word in NOT in your stopList. But in code, you’re checking to see if
[stopList containsObject:[query objectAtIndex:i]]is YES. I think you should be checking for NO there.Also, your for loop can be expressed probably a bit more clearly using the fast enumeration syntax:
I hope that helps.
EDIT
Here is the output after copying, pasting, running your updated code. Seems to do what you said you wanted, no?