iOS - 在字符串中查找单词出现次数的最有效方法

时间:2012-11-13 18:08:44

标签: objective-c ios nsstring nsset

给定一个字符串,我需要获取该字符串中出现的每个单词的计数。为此,我通过单词将字符串提取为数组,然后以这种方式进行搜索,但我觉得直接搜索字符串更为理想。下面是我最初编写的用于解决问题的代码。我想要提供更好的解决方案的建议。

NSMutableDictionary *sets = [[NSMutableDictionary alloc] init];

NSString *paragraph = [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"text" ofType:@"txt"] encoding:NSUTF8StringEncoding error:NULL];

NSMutableArray *words = [[[paragraph lowercaseString] componentsSeparatedByString:@" "] mutableCopy];

while (words.count) {
    NSMutableIndexSet *indexSet = [[NSMutableIndexSet alloc] init];
    NSString *search = [words objectAtIndex:0];
    for (unsigned i = 0; i < words.count; i++) {
        if ([[words objectAtIndex:i] isEqualToString:search]) {
            [indexSet addIndex:i];
        }
    }
    [sets setObject:[NSNumber numberWithInt:indexSet.count] forKey:search];
    [words removeObjectsAtIndexes:indexSet];
}

NSLog(@"%@", sets);

示例:

起始字符串:
“这是一个测试。这只是一个测试。”

结果:

  • “这” - 2
  • “是” - 2
  • “a” - 2
  • “test” - 2
  • “only” - 1

3 个答案:

答案 0 :(得分:24)

这正是NSCountedSet的用途。

你需要将字符串拆分成单词(iOS足够好以便为我们提供一个函数,以便我们不必担心标点符号)并将它们中的每一个添加到计数集中,从而跟踪每个对象出现在集合中的次数:

NSString     *string     = @"This is a test. This is only a test.";
NSCountedSet *countedSet = [NSCountedSet new];

[string enumerateSubstringsInRange:NSMakeRange(0, [string length])
                           options:NSStringEnumerationByWords | NSStringEnumerationLocalized
                        usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop){

                            // This block is called once for each word in the string.
                            [countedSet addObject:substring];

                            // If you want to ignore case, so that "this" and "This" 
                            // are counted the same, use this line instead to convert
                            // each word to lowercase first:
                            // [countedSet addObject:[substring lowercaseString]];
                        }];

NSLog(@"%@", countedSet);

// Results:  2012-11-13 14:01:10.567 Testing App[35767:fb03] 
// <NSCountedSet: 0x885df70> (a [2], only [1], test [2], This [2], is [2])

答案 1 :(得分:2)

如果我不得不猜测,我会说NSRegularExpression。像这样:

NSUInteger numberOfMatches = [regex numberOfMatchesInString:string
                                                    options:0
                                                      range:NSMakeRange(0, [string length])];

该片段取自here


修改1.0:

根据蒂尔爵士的说法:

NSString *string = @"This is a test, so it is a test";

NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
NSArray *arrayOfWords = [string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
for (NSString *word in arrayOfWords)
{
    if ([dictionary objectForKey:word])
    {
        NSNumber *numberOfOccurences = [dictionary objectForKey:word];
        NSNumber *increment = [NSNumber numberWithInt:(1 + [numberOfOccurences intValue])];
        [dictionary setValue:increment forKey:word];
    }
    else
    {
        [dictionary setValue:[NSNumber numberWithInt:1] forKey:word];
    }
}

你应该小心:

  • 标点符号。 (附近的其他词语)
  • UpperCase单词与lowerCase单词。

答案 2 :(得分:1)

我认为你试图用循环搜索长段落中的单词是非常糟糕的。你应该使用正则表达式来做到这一点!我知道第一次学习它并不容易,但它真的值得知道!看一下这个案例Use regular expression to find/replace substring in NSString