美文网首页
汉语转汉语拼音(排序分组)

汉语转汉语拼音(排序分组)

作者: 番薯大佬 | 来源:发表于2018-08-09 13:51 被阅读39次

    使用及效果

    CNPinYinModel *model = [CNPinYinObject transformCNPinYinText:@"深圳市" toneType:CNPinYinToneTypeDefalut alphabetType:CNPinYinTypeDefalut hasPrefix:NO];
        NSLog(@"深圳市 --> %@", model.valueAlphabet);
        model = [CNPinYinObject transformCNPinYinText:@"深圳市" toneType:CNPinYinToneTypeDefalut alphabetType:CNPinYinTypeUpper hasPrefix:NO];
        NSLog(@"深圳市 --> %@", model.valueAlphabet);
        model = [CNPinYinObject transformCNPinYinText:@"深圳市" toneType:CNPinYinToneTypeDefalut alphabetType:CNPinYinTypeCapitalized hasPrefix:NO];
        NSLog(@"深圳市 --> %@", model.valueAlphabet);
        model = [CNPinYinObject transformCNPinYinText:@"深圳市" toneType:CNPinYinToneTypeDefalut alphabetType:CNPinYinTypeDefalut hasPrefix:YES];
        NSLog(@"深圳市 --> %@", model.valueAlphabet);
        model = [CNPinYinObject transformCNPinYinText:@"深圳市" toneType:CNPinYinToneTypeWithout alphabetType:CNPinYinTypeDefalut hasPrefix:NO];
        NSLog(@"深圳市 --> %@", model.valueAlphabet);
        model = [CNPinYinObject transformCNPinYinText:@"深圳市" toneType:CNPinYinToneTypeWithout alphabetType:CNPinYinTypeDefalut hasPrefix:YES];
        NSLog(@"深圳市 --> %@", model.valueAlphabet);
        
        NSLog(@"----------------------------");
        
        NSArray *citys = @[@"广东", @"广西", @"湖南", @"湖北", @"福建", @"河南", @"河北", @"山西", @"山东", @"江西"];
        NSArray *result = [CNPinYinObject transformCNPinYinArray:citys toneType:CNPinYinToneTypeDefalut alphabetType:CNPinYinTypeDefalut hasPrefix:NO];
        for (CNPinYinModel *model in result) {
            NSLog(@"%@ --> %@", model.valueCN, model.valueAlphabet);
        }
        
        NSLog(@"----------------------------");
        
        result = [CNPinYinObject transformCNPinYinSeparatedArray:citys alphabetType:CNPinYinTypeUpper hasPrefix:YES group:YES];
        for (CNPinYinModel *model in result) {
            NSLog(@"key --> %@", model.sortKey);
            for (CNPinYinModel *subModel in model.sortArray) {
                NSLog(@"%@ --> %@", subModel.valueCN, subModel.valueAlphabet);
            }
        }
    
    2018-08-09 13:47:57.814702+0800 demo[2772:111288] 深圳市 --> shēn zhèn shì
    2018-08-09 13:47:57.815085+0800 demo[2772:111288] 深圳市 --> SHĒN ZHÈN SHÌ
    2018-08-09 13:47:57.815362+0800 demo[2772:111288] 深圳市 --> Shēn Zhèn Shì
    2018-08-09 13:47:57.815612+0800 demo[2772:111288] 深圳市 --> s
    2018-08-09 13:47:57.816499+0800 demo[2772:111288] 深圳市 --> shen zhen shi
    2018-08-09 13:47:57.818258+0800 demo[2772:111288] 深圳市 --> s
    2018-08-09 13:47:57.818407+0800 demo[2772:111288] ----------------------------
    2018-08-09 13:47:57.819840+0800 demo[2772:111288] 广东 --> guǎng dōng
    2018-08-09 13:47:57.820003+0800 demo[2772:111288] 广西 --> guǎng xī
    2018-08-09 13:47:57.820132+0800 demo[2772:111288] 湖南 --> hú nán
    2018-08-09 13:47:57.820235+0800 demo[2772:111288] 湖北 --> hú běi
    2018-08-09 13:47:57.820347+0800 demo[2772:111288] 福建 --> fú jiàn
    2018-08-09 13:47:57.820459+0800 demo[2772:111288] 河南 --> hé nán
    2018-08-09 13:47:57.820624+0800 demo[2772:111288] 河北 --> hé běi
    2018-08-09 13:47:57.820733+0800 demo[2772:111288] 山西 --> shān xī
    2018-08-09 13:47:57.820865+0800 demo[2772:111288] 山东 --> shān dōng
    2018-08-09 13:47:57.820975+0800 demo[2772:111288] 江西 --> jiāng xī
    2018-08-09 13:47:57.846621+0800 demo[2772:111288] ----------------------------
    2018-08-09 13:47:57.860407+0800 demo[2772:111288] key --> F
    2018-08-09 13:47:57.861055+0800 demo[2772:111288] 福建 --> F
    2018-08-09 13:47:57.861327+0800 demo[2772:111288] key --> G
    2018-08-09 13:47:57.861524+0800 demo[2772:111288] 广东 --> G
    2018-08-09 13:47:57.861688+0800 demo[2772:111288] 广西 --> G
    2018-08-09 13:47:57.863077+0800 demo[2772:111288] key --> H
    2018-08-09 13:47:57.863629+0800 demo[2772:111288] 湖南 --> H
    2018-08-09 13:47:57.865070+0800 demo[2772:111288] 湖北 --> H
    2018-08-09 13:47:57.865256+0800 demo[2772:111288] 河南 --> H
    2018-08-09 13:47:57.865387+0800 demo[2772:111288] 河北 --> H
    2018-08-09 13:47:57.865489+0800 demo[2772:111288] key --> J
    2018-08-09 13:47:57.865695+0800 demo[2772:111288] 江西 --> J
    2018-08-09 13:47:57.866019+0800 demo[2772:111288] key --> S
    2018-08-09 13:47:57.866618+0800 demo[2772:111288] 山西 --> S
    2018-08-09 13:47:57.866907+0800 demo[2772:111288] 山东 --> S
    

    实现代码

    • 接口文件
    #import <Foundation/Foundation.h>
    @class CNPinYinModel;
    
    /// 汉字转拼单音调类型
    typedef NS_ENUM(NSInteger, CNPinYinToneType) {
        /// 有音调
        CNPinYinToneTypeDefalut = 0,
        /// 无音调
        CNPinYinToneTypeWithout = 1
    };
    
    /// 汉字转拼音大小写类型
    typedef NS_ENUM(NSInteger, CNPinYinType)
    {
        /// 小写
        CNPinYinTypeDefalut = 0,
        /// 大写
        CNPinYinTypeUpper = 1,
        /// 首字母大写
        CNPinYinTypeCapitalized = 2
    };
    
    @interface CNPinYinObject : NSObject
    
    /// 汉语转汉语拼音(字符串转换),只要首拼
    + (CNPinYinModel *)transformCNPinYinText:(NSString *)chinese toneType:(CNPinYinToneType)toneType alphabetType:(CNPinYinType)alphabetType hasPrefix:(BOOL)isTrue;
    
    /// 汉语转汉语拼音(数据字符串转换)
    + (NSArray *)transformCNPinYinArray:(NSArray *)chineseArray toneType:(CNPinYinToneType)toneType alphabetType:(CNPinYinType)alphabetType hasPrefix:(BOOL)isTrue;
    
    /// 汉语转汉语拼音(数据字符串转换-按拼音排序),是否分组(若分组则只有一个首拼)
    + (NSArray *)transformCNPinYinSeparatedArray:(NSArray *)chineseArray alphabetType:(CNPinYinType)type hasPrefix:(BOOL)isTrue group:(BOOL)isGrouping;
    
    @end
    
    
    @interface CNPinYinModel : NSObject
    
    /// 分组时的分组标题
    @property (nonatomic, strong) NSString *sortKey;
    /// 分组时的分组数组
    @property (nonatomic, strong) NSArray *sortArray;
    
    @property (nonatomic, strong) NSString *valueCN;
    @property (nonatomic, strong) NSString *valueAlphabet;
    
    @end
    
    • 实现文件
    #import "CNPinYinObject.h"
    
    @implementation CNPinYinObject
    
    // 汉语转汉语拼音(字符串转换),只要首拼
    + (CNPinYinModel *)transformCNPinYinText:(NSString *)chinese toneType:(CNPinYinToneType)toneType alphabetType:(CNPinYinType)alphabetType hasPrefix:(BOOL)isTrue
    {
        NSMutableString *pinyin = [chinese mutableCopy];
        //
        // 带音标kCFStringTransformMandarinLatin
        CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformMandarinLatin, NO);
        if (toneType == CNPinYinToneTypeWithout) {
            // 无音标kCFStringTransformStripCombiningMarks
            CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformStripCombiningMarks, NO);
        }
        //
        if (alphabetType == CNPinYinTypeDefalut) {
            pinyin = (NSMutableString *)[pinyin lowercaseString];
        } else if (alphabetType == CNPinYinTypeUpper) {
            pinyin = (NSMutableString *)[pinyin uppercaseString];
        } else if (alphabetType == CNPinYinTypeCapitalized) {
            pinyin = (NSMutableString *)[pinyin capitalizedString];
        }
        //
        if (isTrue) {
            if (pinyin.length > 0) {
                pinyin = (NSMutableString *)[pinyin substringWithRange:NSMakeRange(0, 1)];
            }
        }
        CNPinYinModel *model = [CNPinYinModel new];
        model.valueCN = chinese;
        model.valueAlphabet = pinyin;
        return model;
    }
    
    // 汉语转汉语拼音(数据字符串转换)
    + (NSArray *)transformCNPinYinArray:(NSArray *)chineseArray toneType:(CNPinYinToneType)toneType alphabetType:(CNPinYinType)alphabetType hasPrefix:(BOOL)isTrue
    {
        NSMutableArray *results = [[NSMutableArray alloc] init];
        for (NSString *chinese in chineseArray) {
            CNPinYinModel *model = [self transformCNPinYinText:chinese toneType:toneType alphabetType:alphabetType hasPrefix:isTrue];
            [results addObject:model];
        }
        return results;
    }
    
    // 汉语转汉语拼音(数据字符串转换-按拼音排序),是否分组(若分组则只有一个首拼)
    + (NSArray *)transformCNPinYinSeparatedArray:(NSArray *)chineseArray alphabetType:(CNPinYinType)type hasPrefix:(BOOL)isTrue group:(BOOL)isGrouping
    {
        BOOL hasPrefix = isTrue;
        if (isGrouping) {
            hasPrefix = YES;
        }
        NSArray *results = [self transformCNPinYinArray:chineseArray toneType:CNPinYinToneTypeWithout alphabetType:type hasPrefix:hasPrefix];
        // ascending:YES 代表升序 如果为NO 代表降序
        NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"valueAlphabet" ascending:YES];
        results = [[results sortedArrayUsingDescriptors:@[sort]] mutableCopy];
        //
        if (isGrouping) {
            NSMutableArray *groups = [[NSMutableArray alloc] init];
            for (int i = 0; i < results.count; ) {
                NSMutableArray *grounpsTmp = [[NSMutableArray alloc] init];
                CNPinYinModel *model = results[i];
                [grounpsTmp addObject:model];
                int j = i + 1;
                int length = 1;
                while (j < results.count && ([[results[j] valueAlphabet] isEqualToString:model.valueAlphabet])) {
                    [grounpsTmp addObject:results[j]];
                    j++;
                    length++;
                }
                i += length;
                //
                CNPinYinModel *modelSuper = [CNPinYinModel new];
                modelSuper.sortKey = model.valueAlphabet;
                modelSuper.sortArray = grounpsTmp;
                [groups addObject:modelSuper];
            }
            results = groups;
        }
        return results;
    }
    
    @end
    
    
    @implementation CNPinYinModel
    
    @end
    

    相关文章

      网友评论

          本文标题:汉语转汉语拼音(排序分组)

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