美文网首页
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通讯录汉字转拼音以及模糊匹配

    前言 在做相关项目,使用融云作为IM模块,做通讯录模糊匹配功能 汉字转拼音 多音字 处理姓氏多音字 模糊匹配 De...

  • Python 拼音汉字互转

    汉字转拼音:pypinyin拼音转汉字:Pinyin2Hanzi Python汉字转拼音-拼音转汉字的效率测评

  • iOS将汉字转成拼音

    在ios开发中经常碰到做通讯录需要将汉字转成拼音的情况,以下就是我把汉字转成拼音的方法 用kCFStringTra...

  • iOS 汉字转拼音

    在ios开发中经常碰到做通讯录需要将汉字转成拼音的情况,以下就是我把汉字转成拼音的方法 用kCFStringTra...

  • IOS 汉字转拼音

    网上找到的一些小知识,记录一下。 //获取拼音字母(传入汉字字符串, 返回大写拼音字母) - (NSString ...

  • iOS汉字转拼音

    汉字转拼音 系统库实现:

  • iOS汉字转拼音

    1.核心方法 参数一*:需要传入CFMutableStringRef类型的字符串, 这个字符串就是这里的主角, 想...

  • iOS汉字转拼音

    汉字转拼音之前有很多人用的都是一个拼音库,pinyin.h和pinyin.m,用着还算方便吧。 后来发现苹果的fr...

  • iOS 汉字转拼音

    在oc 之中的字符串为汉字的时候,我们常常要进行字符串比较,尤其是类似通讯录的项目,但是汉字不能比较,所以就要将汉...

  • IOS 汉字转拼音

    //汉字转拼音 + (NSString *)transform:(NSString *)chinese{NSMut...

网友评论

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

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