Created
May 14, 2014 11:23
-
-
Save douglashill/af215c62af117f8560fa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
int main(int argc, char *argv[]) { | |
@autoreleasepool { | |
NSCountedSet *set = [NSCountedSet set]; | |
NSError *error; | |
NSString *corpus = [NSString stringWithContentsOfFile:@"some-file.txt" encoding:NSUTF8StringEncoding error:&error]; | |
[corpus enumerateSubstringsInRange:NSMakeRange(0, [corpus length]) options:NSStringEnumerationByWords usingBlock:^(NSString *word, NSRange substringRange, NSRange enclosingRange, BOOL *stop) { | |
[set addObject:word]; | |
}]; | |
NSSortDescriptor *countDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"self" ascending:NO comparator:^NSComparisonResult(id obj1, id obj2) { | |
if ([set countForObject:obj1] < [set countForObject:obj2]) { | |
return NSOrderedAscending; | |
} | |
if ([set countForObject:obj1] == [set countForObject:obj2]) { | |
return NSOrderedSame; | |
} | |
return NSOrderedDescending; | |
}]; | |
NSArray *sorted = [set sortedArrayUsingDescriptors:@[countDescriptor]]; | |
for (NSString *word in sorted) { | |
NSLog(@"%lu - %@", [set countForObject:word], word); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment