博客
关于我
Objective-C实现英文词频统计(附完整源码)
阅读量:797 次
发布时间:2023-02-22

本文共 1569 字,大约阅读时间需要 5 分钟。

Objective-C????????

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

????????????????????????????????????????????????????????????????????????????????????????????????????

转载地址:http://kfsfk.baihongyu.com/

你可能感兴趣的文章
Objective-C实现控制程控电源2306读取电流 (附完整源码)
查看>>
Objective-C实现摄氏温度和华氏温度互转(附完整源码)
查看>>
Objective-C实现播放器(附完整源码)
查看>>
Objective-C实现操作MySQL(附完整源码)
查看>>
Objective-C实现操作注册表 (附完整源码)
查看>>
Objective-C实现改变图片亮度算法(附完整源码)
查看>>
Objective-C实现数字图像处理算法(附完整源码)
查看>>
Objective-C实现数组切片(附完整源码)
查看>>
Objective-C实现数组去重(附完整源码)
查看>>
Objective-C实现数组的循环左移(附完整源码)
查看>>
Objective-C实现数除以二divideByTwo算法(附完整源码)
查看>>
Objective-C实现文件分割(附完整源码)
查看>>
Objective-C实现文件的删除、复制与重命名操作实例(附完整源码)
查看>>
Objective-C实现无序表查找算法(附完整源码)
查看>>
Objective-C实现无锁链表(附完整源码)
查看>>
Objective-C实现无锁链表(附完整源码)
查看>>
Objective-C实现时间戳转为年月日时分秒(附完整源码)
查看>>
Objective-C实现是否为 Pythagoreantriplet 毕氏三元数组算法(附完整源码)
查看>>
Objective-C实现显示响应算法(附完整源码)
查看>>
Objective-C实现晚捆绑测试实例(附完整源码)
查看>>