123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- //
- // WFCUPinyinUtility.m
- // WFChatUIKit
- //
- // Created by dali on 2021/1/28.
- // Copyright © 2020 WildFireChat. All rights reserved.
- //
- #import "WFCUPinyinUtility.h"
- @implementation WFCUPinyinModel
- @end
- @interface WFCUPinyinUtility ()
- @property(nonatomic, strong)NSMutableDictionary *pinyinDict;
- @end
- @implementation WFCUPinyinUtility
- -(BOOL)isMatch:(NSString *)name ofPinYin:(NSString *)pinyin {
- if(!name.length)
- return NO;
- pinyin = [pinyin lowercaseString];
-
- if(!self.pinyinDict) {
- [self loadPinyin];
- }
-
- WFCUPinyinModel *model = [self getModelByName:name];
- for (NSString *jp in model.jianpin) {
- if([jp containsString:pinyin]) {
- return YES;
- }
- }
- for (NSString *qp in model.quanpin) {
- if([qp rangeOfString:pinyin].location == 0) {
- return YES;
- }
- }
-
- return NO;
- }
- - (WFCUPinyinModel *)getModelByName:(NSString *)name {
- WFCUPinyinModel *model = [[WFCUPinyinModel alloc] init];
- model.jianpin = [[NSMutableArray alloc] init];
- model.quanpin = [[NSMutableArray alloc] init];
-
- for (int j = 0; j < name.length; j++) {
- NSString *ch = [name substringWithRange:NSMakeRange(j, 1)];
-
- NSString *codepointHexStr =[[NSString stringWithFormat:@"%x", [name characterAtIndex:j]] uppercaseString];
-
- if ([self isChinese:ch]) {
- NSArray *pinyins = self.pinyinDict[codepointHexStr];
-
- NSMutableArray *temp = [[NSMutableArray alloc] init];
- if(!model.jianpin.count) {
- for (NSString *str in pinyins) {
- [temp addObject:[str substringToIndex:1]];
- }
- } else {
- for (NSString *str in pinyins) {
- NSString *j = [str substringToIndex:1];
- for (NSString *jp in model.jianpin) {
- NSString *newJp = [jp stringByAppendingString:j];
- [temp addObject:newJp];
- }
- }
- }
- model.jianpin = temp;
-
- temp = [[NSMutableArray alloc] init];
- for (NSString *str in pinyins) {
- for (NSString *qp in model.quanpin) {
- NSString *newqp = [qp stringByAppendingString:str];
- [temp addObject:newqp];
- }
- [temp addObject:str];
- }
- model.quanpin = temp;
- }else{
- ch = ch.lowercaseString;
- NSMutableArray *temp = [[NSMutableArray alloc] init];
- if(model.jianpin.count == 0) {
- [temp addObject:ch];
- } else {
- for (NSString *jp in model.jianpin) {
- NSString *newjp = [jp stringByAppendingString:ch];
- [temp addObject:newjp];
- }
- }
- model.jianpin = temp;
-
- temp = [[NSMutableArray alloc] init];
- for (NSString *qp in model.quanpin) {
- NSString *newqp = [qp stringByAppendingString:ch];
- [temp addObject:newqp];
- }
- [temp addObject:ch];
- model.quanpin = temp;
- }
- }
- return model;
- }
- - (void)loadPinyin {
- self.pinyinDict = [[NSMutableDictionary alloc] init];
- NSString *resourcePath = [[NSBundle bundleForClass:[self class]] resourcePath];
- NSString *bundlePath = [resourcePath stringByAppendingPathComponent:@"unicode_to_hanyu_pinyin.txt"];
-
-
- NSString* fileContents = [NSString stringWithContentsOfFile:bundlePath encoding:NSUTF8StringEncoding error:nil];
- // first, separate by new line
- NSArray* allLinedStrings = [fileContents componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
- for (NSString *oneLine in allLinedStrings) {
- NSArray *lineComponents=[oneLine componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
- if(lineComponents.count == 2) {
- NSString *str = lineComponents[1];
- str = [str substringWithRange:NSMakeRange(1, str.length-2)];
- self.pinyinDict[lineComponents[0]] = [str componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]];
- }
- }
- }
- - (BOOL)isChinese:(NSString *)text {
- NSString *match = @"(^[\u4e00-\u9fa5]+$)";
- NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match];
- return [predicate evaluateWithObject:text];
- }
- @end
|