1.将汉字转换成拼音
转换成不带音标的拼音
//转换成拼音
CFStringTransform((__bridge CFMutableStringRef)chinese,
0,
kCFStringTransformMandarinLatin,
NO);
//去掉音调[要音调的就不要这个]
CFStringTransform((__bridge CFMutableStringRef)chinese,
0,
kCFStringTransformStripDiacritics,
NO);
2.拼音的分割
韵母表:
a o e i u ü
ai ei ui ao ou iu ie üe er
an en in un ün
ang eng ing ong
声母表
@"b",@"p",@"m",@"f",@"d",
@"t",@"n",@"l",@"g",@"k",
@"h",@"j",@"q",@"x",
@"zh",@"ch",@"sh",@"z",@"c",@"s"
拆拼音法
1.先拆掉声母 拆声母的时候要注意 z c s
2.拆掉声母后就只剩下韵母了
3.拆掉声母后有两种情况:一是没有声母的就为单拼音,此时不需要在拆分韵母了。
二是有声母,接下来就需要继续拆分韵母了
4.拆分韵母:先看韵母的长度,首先:>=4的话就是三音节的,韵母的首字母为介母,
剩下的是后韵母,那这种情况就可以直接拆分了。如果在4个字以内,在事先准备好的
字母表里边去查找(查找和韵母部分相同长度的字母表),找到了就是双拼音节,直接
返回拆分的结果;如果没有找到,那就是三拼音节,去掉首字母后剩下的就是韵母。
下边是拆分拼音的实现
QKPinYin.h
typedef enum
{
/**
* 单拼音节
*/
QKDanPinYinJie = 1,
/**
* 双拼音节
*/
QKShuangPinYinJie = 2,
/**
* 三拼音节
*/
QKSanPinYinJie = 3,
} QKPinYinJieType;
/**
* 拼音处理
*/
@interface QKPinYin : NSObject
/**
* 音节的类型
*/
@property (nonatomic,assign)QKPinYinJieType yinJieType;
/**
* 声母
*/
@property (nonatomic,copy)NSString *shengMu;
/**
* 介母
*/
@property (nonatomic,copy)NSString *jieMu;
/**
* 韵母
*/
@property (nonatomic,copy)NSString *yunMu;
/**
* 拆分拼音
*
* @param pinYin 拼音
*/
- (void)cutPinYin:(NSString *)pinYin;
@end
- QKPinYin.m
内部需要的数据
@interface QKPinYin ()
/**
* 声母 z s c --> +h后也是声母
*/
@property (nonatomic,strong)NSArray *shengMuArr;
/**
* 1个字母的韵母
*/
@property (nonatomic,strong)NSArray *oneYunArr;
/**
* 2个字母的韵母
*/
@property (nonatomic,strong)NSArray *twoYunArr;
/**
* 3个字母的韵母
*/
@property (nonatomic,strong)NSArray *threeYunArr;
@end
初始化数据
- (instancetype)init
{
self = [super init];
if (self)
{
//声母
self.shengMuArr = @[
@"b",@"p",@"m",@"f",@"d",
@"t",@"n",@"l",@"g",@"k",
@"h",@"j",@"q",@"x",
@"zh",@"ch",@"sh",
@"z",@"c",@"s",@"y",
@"w",@"r"
];
//单韵母
self.oneYunArr = @[
@"a",@"o",@"e",@"i",@"u",@"ü",
];
//双韵母
self.twoYunArr = @[
@"ai",@"ei",@"ui",@"ao",
@"ou",@"iu",@"ie",@"üe",@"er",
@"an",@"en",@"in",@"un",@"ün"
];
//三韵母
self.threeYunArr = @[
@"ang",@"eng",@"ing",@"ong",
];
}
return self;
}
第一步拆分
- (void)cutPinYin:(NSString *)pinYin
{
if (!pinYin || [pinYin length] == 0)
{
NSLog(@"输入的拼音有问题,不能转换");
return;
}
//拼音的长度
NSUInteger length = [pinYin length];
if (length == 1)
{
//只有1个字符,只有韵母
self.shengMu = @"";
self.yunMu = pinYin;
self.jieMu = @"";
self.yinJieType = QKDanPinYinJie;
return;
}else if(length == 2)
{
//不可能是zh ch sh
//首字母
NSString *firstStr = [pinYin substringWithRange:NSMakeRange(0, 1)];
NSString *content = [NSString stringWithFormat:@"SELF = '%@'",firstStr];
NSPredicate *predicate = [NSPredicate predicateWithFormat:content];
NSArray *resultArr = [self.shengMuArr filteredArrayUsingPredicate:predicate];
if (resultArr && [resultArr count] > 0)
{
//查到声母了 并且是单声母 单韵母
self.shengMu = firstStr;
self.yunMu = [pinYin substringWithRange:NSMakeRange(1, 1)];
self.jieMu = @"";
self.yinJieType = QKShuangPinYinJie;
return;
}else
{
//没有声母 单拼音节
self.shengMu = @"";
self.yunMu = pinYin;
self.jieMu = @"";
self.yinJieType = QKDanPinYinJie;
return;
}
}else
{
//可以有zh ch sh 了
NSString *firstStr = [pinYin substringWithRange:NSMakeRange(0, 1)];
if ([firstStr isEqualToString:@"z"] ||
[firstStr isEqualToString:@"c"] ||
[firstStr isEqualToString:@"s"])
{
NSString *secondStr = [pinYin substringWithRange:NSMakeRange(1, 1)];
if ([secondStr isEqualToString:@"h"])
{
//就是zh ch sh 这一类
self.shengMu = [pinYin substringWithRange:NSMakeRange(0, 2)];
//接着在看韵母
if (length == 3)
{
//双拼音节
self.yunMu = [pinYin substringWithRange:NSMakeRange(2, 1)];
self.jieMu = @"";
self.yinJieType = QKShuangPinYinJie;
return;
}else
{
//不清楚音节
NSString *subShengMu = [pinYin substringWithRange:NSMakeRange(2, length-2)];
[self subYunMu:subShengMu
haveShengMu:YES];
return;
}
}
else
{
//声母是z c s
//不清楚音节
NSString *subShengMu = [pinYin substringWithRange:NSMakeRange(1, length-1)];
self.shengMu = [pinYin substringWithRange:NSMakeRange(0, 1)];
[self subYunMu:subShengMu
haveShengMu:YES];
return;
}
}
else
{
//不是 z c s 开头的 需要从字母表里边查找
NSString *firstStr = [pinYin substringWithRange:NSMakeRange(0, 1)];
NSString *content = [NSString stringWithFormat:@"SELF = '%@'",firstStr];
NSPredicate *predicate = [NSPredicate predicateWithFormat:content];
NSArray *resultArr = [self.shengMuArr filteredArrayUsingPredicate:predicate];
if (resultArr && [resultArr count] > 0)
{
//找到了
NSString *subShengMu = [pinYin substringWithRange:NSMakeRange(1, length-1)];
self.shengMu = [pinYin substringWithRange:NSMakeRange(0, 1)];
[self subYunMu:subShengMu
haveShengMu:YES];
return;
}else
{
//只有韵母
self.shengMu = @"";
self.yunMu = pinYin;
self.jieMu = @"";
self.yinJieType = QKDanPinYinJie;
return;
}
}
}
}
第二步拆分:拆分掉声母后拆分韵母
- (void)subYunMu:(NSString *)yunMu
haveShengMu:(BOOL)have
{
NSUInteger length = [yunMu length];
if (length == 1)
{
self.jieMu = @"";
self.yunMu = yunMu;
if (have)
{
self.yinJieType = QKShuangPinYinJie;
}else
{
self.yinJieType = QKDanPinYinJie;
}
return;
}else if(length == 2)
{
[self searchJieMu:yunMu
yunMuArr:self.twoYunArr
haveShengMu:have];
return;
}else if (length == 3)
{
[self searchJieMu:yunMu
yunMuArr:self.threeYunArr
haveShengMu:have];
return;
}else
{
//三音节
self.jieMu = [yunMu substringWithRange:NSMakeRange(0, 1)];
self.yunMu = [yunMu substringWithRange:NSMakeRange(1, length-1)];
self.yinJieType = QKSanPinYinJie;
return;
}
}
最后的拆分 看韵母种是否有介母
- (void)searchJieMu:(NSString *)yunMu
yunMuArr:(NSArray *)arr
haveShengMu:(BOOL)have
{
NSUInteger length = [yunMu length];
NSString *content = [NSString stringWithFormat:@"SELF = '%@'",yunMu];
NSPredicate *predicate = [NSPredicate predicateWithFormat:content];
NSArray *resultArr = [arr filteredArrayUsingPredicate:predicate];
if (resultArr && [resultArr count] > 0)
{
//没有介母
self.jieMu = @"";
self.yunMu = yunMu;
if (have)
{
self.yinJieType = QKShuangPinYinJie;
}else
{
self.yinJieType = QKDanPinYinJie;
}
return;
}else
{
//有介母
self.jieMu = [yunMu substringWithRange:NSMakeRange(0, 1)];
self.yunMu = [yunMu substringWithRange:NSMakeRange(1, length-1)];
if (have)
{
self.yinJieType = QKSanPinYinJie;
}else
{
self.yinJieType = QKShuangPinYinJie;
}
return;
}
}