数组比较和异常结果
在这段代码中,我有一个句子,我将其转换为数组,然后我有另一个数组,它是一个非索引字表,我想通过非索引字表过滤我的句子,意味着最终的句子不应包含非索引字表元素!
这似乎很容易,我自杀了才让它起作用,但它从来没有起作用!天哪!
你能告诉我有什么问题吗?
#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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,一个问题是
[finalsentence addObject:q]
中的“q”是什么?您的代码中没有定义“q”。另一个问题是,您的测试似乎与您给出的描述相反。您的描述说您希望将每个单词添加到“finalsentence”中(如果该单词不在您的停用词列表中)。但在代码中,您要检查
[stopList containsObject:[query objectAtIndex:i]]
是否为 YES。我认为你应该检查那里是否有“否”。另外,您的 for 循环可以使用快速枚举语法更清楚地表达:
我希望有所帮助。
编辑
这是复制、粘贴、运行更新后的代码后的输出。似乎按照你说的做了,不是吗?
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?