汉字转拼音,处理数字与其他符号到#

本文详细阐述了将汉字列表转化为对应英文首字母与拼音映射的算法,通过正则表达式匹配英文首字母并利用HanyuPinyin库计算汉字拼音,实现了高效的数据关联与组织。

+(NSDictionary*)HanziList2Pin:(NSArray*)HanziList{
// NSMutableArray* real=[NSMutableArray new];
NSMutableDictionary* realdic=[NSMutableDictionary new];
NSArray* array = [[NSMutableArray alloc] initWithObjects:@”A”,@”B”,@”C”,@”D”,@”E”,@”F”,@”G”,@”H”,@”I”,@”J”,@”K”,@”L”,@”M”,@”N”,@”O”,@”P”,@”Q”,@”R”,@”S”,@”T”,@”U”,@”V”,@”W”,@”X”,@”Y”,@”Z”,@”#”,nil];

for (int i=0; i<[HanziList count]; i++) {
    NSString* str=[HanziList objectAtIndex:i];

    //先截取字符串,拿到第一个字符
    NSString *firstStr = [[str substringToIndex:1] uppercaseString];

    //判断是不是中文开头的
    BOOL isFirst = [self isChineseFirst:firstStr];
    if (isFirst)
        NSLog(@"第一个字符是中文开头的--%@", firstStr);
    HanyuPinyinOutputFormat *outputFormat=[[HanyuPinyinOutputFormat alloc] init];
    [outputFormat setToneType:ToneTypeWithoutTone];
    [outputFormat setVCharType:VCharTypeWithV];
    [outputFormat setCaseType:CaseTypeUppercase];
    NSString *outputPinyin=[PinyinHelper toHanyuPinyinStringWithNSString:[HanziList objectAtIndex:i] withHanyuPinyinOutputFormat:outputFormat withNSString:@" "];
    char letter=[outputPinyin characterAtIndex:0];
    if ([array containsObject:[NSString stringWithFormat:@"%c",letter]]) {
        NSMutableArray* array=[realdic objectForKey:[NSString stringWithFormat:@"%c",letter]];
        if (array==nil) {
            array=[NSMutableArray new];
        }
        [array addObject:[HanziList objectAtIndex:i]];
        NSString* key=[NSString stringWithFormat:@"%c",letter];
        [realdic setObject:array forKey:key];

    }


    else
    {
        //判断是不是字母开头的
        NSLog(@"第一个字符不是中文开头的--%@", firstStr);
        BOOL isA = [self MatchLetter:firstStr];
        if (isA){
            if ([array containsObject:[NSString stringWithFormat:@"%@",firstStr]]) {
                NSMutableArray* array=[realdic objectForKey:[NSString stringWithFormat:@"%@",firstStr]];
                if (array==nil) {
                    array=[NSMutableArray new];
                }
                [array addObject:[HanziList objectAtIndex:i]];
                NSString* key=[NSString stringWithFormat:@"%@",firstStr];
                [realdic setObject:array forKey:key];

            }

        }
        else{
            //数字或者其他符号
            NSLog(@"第一个字符数字或者其他符号--%@", firstStr);
            if ([array containsObject:[NSString stringWithFormat:@"%@",@"#"]]) {
                NSMutableArray* array=[realdic objectForKey:[NSString stringWithFormat:@"%@",@"#"]];
                if (array==nil) {
                    array=[NSMutableArray new];
                }
                [array addObject:[HanziList objectAtIndex:i]];
                NSString* key=[NSString stringWithFormat:@"%@",@"#"];
                [realdic setObject:array forKey:key];

            }


        }

    }



}
return realdic;

}

pragma mark 正则表达式

+(BOOL)MatchLetter:(NSString *)str
{
//判断是否以字母开头
NSString *ZIMU = @”^[A-Za-z]+$”;
NSPredicate *regextestA = [NSPredicate predicateWithFormat:@”SELF MATCHES %@”, ZIMU];

if ([regextestA evaluateWithObject:str] == YES)
    return YES;
else
    return NO;

}

+(BOOL)isChineseFirst:(NSString *)firstStr
{
//是否以中文开头(unicode中文编码范围是0x4e00~0x9fa5)
int utfCode = 0;
void *buffer = &utfCode;
NSRange range = NSMakeRange(0, 1);
//判断是不是中文开头的,buffer->获取字符的字节数据 maxLength->buffer的最大长度 usedLength->实际写入的长度,不需要的话可以传递NULL encoding->字符编码常数,不同编码方式转换后的字节长是不一样的,这里我用了UTF16 Little-Endian,maxLength为2字节,如果使用Unicode,则需要4字节 options->编码转换的选项,有两个值,分别是NSStringEncodingConversionAllowLossy和NSStringEncodingConversionExternalRepresentation range->获取的字符串中的字符范围,这里设置的第一个字符 remainingRange->建议获取的范围,可以传递NULL
BOOL b = [firstStr getBytes:buffer maxLength:2 usedLength:NULL encoding:NSUTF16LittleEndianStringEncoding options:NSStringEncodingConversionExternalRepresentation range:range remainingRange:NULL];
if (b && (utfCode >= 0x4e00 && utfCode <= 0x9fa5))
return YES;
else
return NO;
}
+(NSMutableArray*)Dic2Array:(NSDictionary*)dic{
NSArray *keys = [dic allKeys];
NSArray *sortedArray = [keys sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2){
return [obj1 compare:obj2 options:NSNumericSearch];
}];
NSMutableArray* array=[NSMutableArray new];
for (int i=0; i<[sortedArray count]; i++) {
[array addObject:[dic objectForKey:[sortedArray objectAtIndex:i]]];
}
return array;

}
+(NSArray*)Dic2PinYinArray:(NSDictionary *)dic{
NSArray *keys = [dic allKeys];
NSArray *sortedArray = [keys sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2){
return [obj1 compare:obj2 options:NSNumericSearch];
}];

return sortedArray;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值