美文网首页
iOS通讯录汉字转拼音以及模糊匹配

iOS通讯录汉字转拼音以及模糊匹配

作者: ashura_ | 来源:发表于2017-05-17 18:04 被阅读0次

    前言

    在做相关项目,使用融云作为IM模块,做通讯录模糊匹配功能

    汉字转拼音

    - (NSString*)pinyinForSort:(BOOL)isForSurname withYindiao:(BOOL)bFlag;
    {
        NSString* convertString = self;
        if (isForSurname)
        {
            //如果是姓名优先去掉“·”符号
            convertString = [self stringByReplacingOccurrencesOfString:@"·" withString:@""];
        }
        NSMutableString *mutableString = [NSMutableString stringWithString:convertString];
        CFStringTransform((CFMutableStringRef)mutableString, NULL, kCFStringTransformToLatin, false);
        if (bFlag)
        {
            NSString* pinyinString = [NSString stringWithString:mutableString];
            NSMutableString* resultString = [NSMutableString stringWithString:pinyinString];
            
            if (isForSurname)
            {
                //处理姓氏多音字
                NSArray* allPolyphoneSurname = [[NSString getDicForAllPolyphoneSurnamePinYinWithYinDiao] allKeys];
                for (NSString* tempPolyphoneSurname in allPolyphoneSurname)
                {
                    if ([convertString hasPrefix:tempPolyphoneSurname])
                    {
                        NSArray* arrayOfPolyphoneSurnamePinYinWithYinDiao = [[NSString getDicForAllPolyphoneSurnamePinYinWithYinDiao] valueForKey:tempPolyphoneSurname];
                        NSString* rightPinYin = [arrayOfPolyphoneSurnamePinYinWithYinDiao objectAtIndex:0];
                        NSString* wrongPinYin = [arrayOfPolyphoneSurnamePinYinWithYinDiao objectAtIndex:1];
                        NSRange rangeOfPinYin = NSMakeRange(0, wrongPinYin.length);
                        if ([[resultString substringWithRange:rangeOfPinYin] isEqualToString:wrongPinYin])
                        {
                            [resultString replaceCharactersInRange:rangeOfPinYin withString:rightPinYin];
                        }
                        break;
                    }
                }
            }
            return resultString;
        }
        else
        {
            NSString *pinyinString = [mutableString stringByFoldingWithOptions:NSDiacriticInsensitiveSearch locale:[NSLocale currentLocale]];
            NSMutableString* resultString = [NSMutableString stringWithString:pinyinString];
            
            if (isForSurname)
            {
                //处理姓氏多音字
                NSArray* allPolyphoneSurname = [[NSString getDicForAllPolyphoneSurnamePinYin] allKeys];
                for (NSString* tempPolyphoneSurname in allPolyphoneSurname)
                {
                    if ([convertString hasPrefix:tempPolyphoneSurname])
                    {
                        NSArray* arrayOfPolyphoneSurnamePinYin = [[NSString getDicForAllPolyphoneSurnamePinYin] valueForKey:tempPolyphoneSurname];
                        NSString* rightPinYin = [arrayOfPolyphoneSurnamePinYin objectAtIndex:0];
                        NSString* wrongPinYin = [arrayOfPolyphoneSurnamePinYin objectAtIndex:1];
                        NSRange rangeOfPinYin = NSMakeRange(0, wrongPinYin.length);
                        if ([[resultString substringWithRange:rangeOfPinYin] isEqualToString:wrongPinYin])
                        {
                            [resultString replaceCharactersInRange:rangeOfPinYin withString:rightPinYin];
                        }
                        break;
                    }
                }
            }
            return resultString;
        }
    }
    

    多音字

    处理姓氏多音字

    + (NSDictionary*)getDicForAllPolyphoneSurnamePinYin;
    {
        static NSDictionary* g_pAllPolyphoneSurnamePinYin = nil;
        static dispatch_once_t pred;
        dispatch_once(&pred, ^
                      {
                          g_pAllPolyphoneSurnamePinYin = [NSMutableDictionary dictionaryWithObjectsAndKeys
                                                          :@[@"po",@"fan"],@"繁"
                                                          ,@[@"ou",@"qu"],@"区"
                                                          ,@[@"qiu",@"chou"],@"仇"
                                                          ,@[@"chong",@"zhong"],@"种"
                                                          ,@[@"shan",@"dan"],@"单"
                                                          ,@[@"xie",@"jie"],@"解"
                                                          ,@[@"zha",@"cha"],@"查"
                                                          ,@[@"zeng",@"ceng"],@"曾"
                                                          ,@[@"bi",@"mi"],@"秘"
                                                          ,@[@"yue",@"le"],@"乐"
                                                          ,@[@"chong",@"zhong"],@"重"
                                                          ,@[@"piao",@"pu"],@"朴"
                                                          ,@[@"miao",@"mou"],@"缪"
                                                          ,@[@"zhai",@"di"],@"翟"
                                                          ,@[@"she",@"zhe"],@"折"
                                                          ,@[@"he",@"hei"],@"黑"
                                                          ,@[@"ge",@"gai"],@"盖"
                                                          ,@[@"shen",@"chen"],@"沈"
                                                          ,@[@"yu chi",@"wei chi"],@"尉迟"
                                                          ,@[@"mo qi",@"wan qi"],@"万俟"
                                                          ,nil];
                      });
        return g_pAllPolyphoneSurnamePinYin;
    }
    

    模糊匹配

    + (NSArray*)filterFuzzySearchFromArray:(NSArray*)sourceArray withWildcards:(NSString*)wildcards
    {
        NSMutableString *searchWithWildcards = [@"*" mutableCopy];
        [wildcards enumerateSubstringsInRange:NSMakeRange(0, [wildcards length])
                                       options:NSStringEnumerationByComposedCharacterSequences
                                    usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
                                        [searchWithWildcards appendString:substring];
                                        [searchWithWildcards appendString:@"*"];
                                    }];
        
        
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF LIKE[cd] %@", searchWithWildcards];
        NSArray *filteredArray = [sourceArray filteredArrayUsingPredicate:predicate];
        return filteredArray;
    }
    

    Demo

    git地址TSFuzzySearchDemo

    相关文章

      网友评论

          本文标题:iOS通讯录汉字转拼音以及模糊匹配

          本文链接:https://www.haomeiwen.com/subject/tradxxtx.html