??????? Objective-C ???????????????????????????????????????????????????????????????????
#import @interface WordFrequencyCounter : NSObject @property (nonatomic, strong) NSMutableDictionary*wordCounts; @end @implementation WordFrequencyCounter - (id)initWithText:(NSString*)text { self = [super init]; self.wordCounts = [[NSMutableDictionary alloc] init]; [self analyzeText:text]; return self; } - (void)analyzeText:(NSString*)text { NSCharacterSet*whitespaces = [NSCharacterSet whitespaceAndNewlineCharacterSet]; [text componentsSeparatedBy:whitespaces].enumerated.forEach:^ NSRegularExpression*regex = [NSRegularExpression regularExpressionWithPattern:@"\\b\\w+\\b"]; NSTextCheckingResult*matchResult = [regex firstMatchIn:text options:0 range:NSMakeRange(0, text.length)]; if (matchResult { NSString*word = [matchResult match]; [self.wordCounts setValue:[NSNumber numberWithInt: [self.wordCounts valueForKey:word] ? [self.wordCounts valueForKey:word] : 0] forKey:word]; } }]; } - (NSString*)frequencyReport { NSMutableString*report = [NSMutableString new]; [self.wordCounts keys].sorted.forEach:^(id word)<{ int count = [self.wordCounts valueForKey:word] ? [self.wordCounts valueForKey:word] : 0; if (count > 0) { [report appendFormat:@"%@: %d\n", word, count]; } }]; return [report string]; } ????????????????????????????????????????????????????????????????????????????????????????????????????