2012-11-13 37 views
12

給定一個字符串,我需要獲取出現在該字符串中的每個單詞的計數。爲此,我通過單詞將字符串提取到一個數組中,然後以這種方式進行搜索,但我有這樣的感覺,即直接搜索字符串更加優化。以下是我最初編寫的用於解決問題的代碼。儘管如此,我仍然希望獲得更好的解決方案。iOS - 在字符串中查找單詞出現次數的最有效方法

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
  • 「一個」 - 2
  • 「測試」 - 2
  • 「僅」 - 1
+0

您的方法是否正常工作?你沒有點符號的問題嗎?我想你應該有「測試」。而不是「測試」。 – Ricardo

回答

23

這正是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]) 
+0

OMG!這比我想象的還要容易!謝謝! – RyJ

+0

不客氣! – lnafziger

+0

@Inafzinger但是這裏有一個小問題,當我想要計算NSString中包含一些html標籤的單詞數量時,它會跳過它們。但我也想數一下。任何想法。 –

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

你應該小心:

  • 標點符號。 (接近其他詞)
  • 大寫單詞vs小寫單詞。
+0

我需要每個單詞的計數。所以在字符串中,「這是一個測試,這只是一個測試。」 「測試」的計數爲2,「這個」計數爲2,「只有」計數爲1等。 – RyJ

+0

檢查我的編輯.. – Peres

相關問題