数组比较和异常结果

发布于 2024-12-20 19:53:37 字数 1085 浏览 4 评论 0原文

在这段代码中,我有一个句子,我将其转换为数组,然后我有另一个数组,它是一个非索引字表,我想通过非索引字表过滤我的句子,意味着最终的句子不应包含非索引字表元素!

这似乎很容易,我自杀了才让它起作用,但它从来没有起作用!天哪!

你能告诉我有什么问题吗?

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

攒眉千度 2024-12-27 19:53:37

好吧,一个问题是 [finalsentence addObject:q] 中的“q”是什么?您的代码中没有定义“q”。

另一个问题是,您的测试似乎与您给出的描述相反。您的描述说您希望将每个单词添加到“finalsentence”中(如果该单词不在您的停用词列表中)。但在代码中,您要检查 [stopList containsObject:[query objectAtIndex:i]] 是否为 YES。我认为你应该检查那里是否有“否”。

另外,您的 for 循环可以使用快速枚举语法更清楚地表达:

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
        }
    }

我希望有所帮助。

编辑

这是复制、粘贴、运行更新后的代码后的输出。似乎按照你说的做了,不是吗?

2011-12-13 03:36:55.544 Some6[8082:707] (
i,
want,
filter,
this,
sentence,
stoplist,
array,
my,
program
)

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:

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
        }
    }

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?

2011-12-13 03:36:55.544 Some6[8082:707] (
i,
want,
filter,
this,
sentence,
stoplist,
array,
my,
program
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文