123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- //
- // ISEResultTools.m
- // IFlyMSCDemo
- //
- // Created by 张剑 on 15/3/6.
- //
- //
- #import "ISEResultTools.h"
- #import "ISEResultPhone.h"
- #import "ISEResultSyll.h"
- #import "ISEResultWord.h"
- #import "ISEResultSentence.h"
- @implementation ISEResultTools
- +(NSString*) toStdSymbol:(NSString*) symbol{
-
- if(!symbol){
- return symbol;
- }
-
- /**
- * The mapping table between iFlytek phonetic symbol and standard phonetic symbol(en)
- */
- static NSDictionary* _gISEResultPhoneHashDic;
-
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- _gISEResultPhoneHashDic=@{
- @"aa" : @"ɑ:",
- @"oo" : @"ɔ",
- @"ae" : @"æ",
- @"ah" : @"ʌ",
- @"ao" : @"ɔ:",
- @"aw" : @"aʊ",
- @"ax" : @"ə",
- @"ay" : @"aɪ",
- @"eh" : @"e",
- @"er" : @"ə:",
- @"ey" : @"eɪ",
- @"ih" : @"ɪ",
- @"iy" : @"i:",
- @"ow" : @"əʊ",
- @"oy" : @"ɔɪ",
- @"uh" : @"ʊ",
- @"uw" : @"ʊ:",
- @"ch" : @"tʃ",
- @"dh" : @"ð",
- @"hh" : @"h",
- @"jh" : @"dʒ",
- @"ng" : @"ŋ",
- @"sh" : @"ʃ",
- @"th" : @"θ",
- @"zh" : @"ʒ",
- @"y" : @"j",
- @"d" : @"d",
- @"k" : @"k",
- @"l" : @"l",
- @"m" : @"m",
- @"n" : @"n",
- @"b" : @"b",
- @"f" : @"f",
- @"g" : @"g",
- @"p" : @"p",
- @"r" : @"r",
- @"s" : @"s",
- @"t" : @"t",
- @"v" : @"v",
- @"w" : @"w",
- @"z" : @"z",
- @"ar" : @"eə",
- @"ir" : @"iə",
- @"ur" : @"ʊə",
- @"tr" : @"tr",
- @"dr" : @"dr",
- @"ts" : @"ts",
- @"dz" : @"dz"
- };
-
- });
-
- NSString* stdsymbol=[_gISEResultPhoneHashDic objectForKey:symbol];
- return stdsymbol?stdsymbol:symbol;
-
- }
- NSString* const KCIFlyResultNormal=@"Right";
- NSString* const KCIFlyResultMiss=@"Skip";
- NSString* const KCIFlyResultAdd=@"Duplicate";
- NSString* const KCIFlyResultRepeat=@"Readback";
- NSString* const KCIFlyResultReplace=@"Replace";
- NSString* const KCIFlyResultNoise=@"Noise";
- NSString* const KCIFlyResultMute=@"Mute";
- + (NSString*)translateDpMessageInfo:(int)dpMessage {
-
- static NSDictionary* _gISEResultDpMessageHashDic;
-
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- _gISEResultDpMessageHashDic=@{
- @0 : KCIFlyResultNormal,
- @16 : KCIFlyResultMiss,
- @32 : KCIFlyResultAdd,
- @64 : KCIFlyResultRepeat,
- @128 : KCIFlyResultReplace
- };
- });
-
- NSString* transDpMessage=[_gISEResultDpMessageHashDic objectForKey:[NSNumber numberWithInt:dpMessage]];
- return transDpMessage;
- }
- + (NSString*)translateContentInfo:(NSString*) content {
-
- if(!content){
- return nil;
- }
-
- static NSDictionary* _gISEResultContentHashDic;
-
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- _gISEResultContentHashDic=@{
- @"sil" : KCIFlyResultMute,
- @"silv" : KCIFlyResultMute,
- @"fil" : KCIFlyResultNoise
- };
- });
-
- NSString* transContent=[_gISEResultContentHashDic objectForKey:content];
- return transContent?transContent:content;
- }
- /**
- * Get the format details from sentences in chinese
- *
- * @param sentences sentences in chinese
- * @return the format details
- */
- + (NSString*)formatDetailsForLanguageCN:(NSArray*) sentences {
- NSString* buffer =[[NSString alloc] init];
- if (!sentences) {
- return nil;
- }
-
- for (ISEResultSentence* sentence in sentences ) {
-
- if (nil == sentence.words) {
- continue;
- }
-
- for (ISEResultWord* word in sentence.words) {
- NSString* wContent=[ISEResultTools translateContentInfo:word.content];
- if ([KCIFlyResultNoise isEqualToString:wContent] || [KCIFlyResultMute isEqualToString:wContent]){
- continue;
- }
- buffer=[buffer stringByAppendingFormat:@"\nWord[%@] %@ Dur:%d",wContent,word.symbol,word.time_len];
-
- if (!word.sylls) {
- continue;
- }
-
- for (ISEResultSyll* syll in word.sylls) {
- NSString* syContent=[ISEResultTools translateContentInfo:[syll content]];
- if ([KCIFlyResultNoise isEqualToString:syContent] || [KCIFlyResultMute isEqualToString:syContent]){
- continue;
- }
-
- buffer=[buffer stringByAppendingFormat:@"\n└Syllable[%@] %@ Dur:%d",syContent,syll.symbol,syll.time_len];
- if (!syll.phones) {
- continue;
- }
-
- for (ISEResultPhone* phone in syll.phones) {
- NSString* pContent=[ISEResultTools translateContentInfo:[phone content]];
- NSString* pDpMessage=[ISEResultTools translateDpMessageInfo:phone.dp_message];
- buffer=[buffer stringByAppendingFormat:@"\n\t└Phoneme[%@] Dur:%d Msg:%@",pContent,phone.time_len,pDpMessage];
- }
-
- }
- buffer=[buffer stringByAppendingString:@"\n"];
- }
- }
- return buffer;
-
- }
- /**
- * Get the format details from sentences in english
- *
- * @param sentences sentences in english
- * @return the format details
- */
- + (NSString*)formatDetailsForLanguageEN:(NSArray*) sentences {
- // NSString* buffer =[[NSString alloc] init];
- if (!sentences) {
- return nil;
- }
- NSMutableArray * result = [[NSMutableArray alloc] init];
- for (ISEResultSentence* sentence in sentences ) {
- NSString* sContent=[ISEResultTools translateContentInfo:sentence.content];
- if ([KCIFlyResultNoise isEqualToString:sContent] || [KCIFlyResultMute isEqualToString:sContent]){
- continue;
- }
-
- if (nil == sentence.words) {
- continue;
- }
- for (ISEResultWord* word in sentence.words) {
- NSString* wContent=[ISEResultTools translateContentInfo:word.content];
- // NSString* wDpMessage=[ISEResultTools translateDpMessageInfo:word.dp_message];
- if ([KCIFlyResultNoise isEqualToString:wContent] || [KCIFlyResultMute isEqualToString:wContent]){
- continue;
- }
- NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
- [dictionary setValue:wContent forKey:@"content"];
- [dictionary setValue:@(word.total_score) forKey:@"score"];
- [result addObject: dictionary];
- // buffer=[buffer stringByAppendingFormat:@"\nWord[%@] Msg:%@ Score:%f",wContent,wDpMessage,word.total_score];
-
- // if (!word.sylls) {
- // buffer=[buffer stringByAppendingString:@"\n"];
- // continue;
- // }
- //
- // for (ISEResultSyll* syll in word.sylls) {
- // NSString* syContent=[ISEResultTools translateContentInfo:[syll getStdSymbol]];
- // buffer=[buffer stringByAppendingFormat:@"\n└Syllable[%@] ",syContent];
- // if (!syll.phones) {
- // continue;
- // }
- //
- // for (ISEResultPhone* phone in syll.phones) {
- // NSString* pContent=[ISEResultTools translateContentInfo:[phone getStdSymbol]];
- // NSString* pDpMessage=[ISEResultTools translateDpMessageInfo:phone.dp_message];
- // buffer=[buffer stringByAppendingFormat:@"\n\t└Phoneme[%@] Msg:%@",pContent,pDpMessage];
- // }
- //
- // }
- // buffer=[buffer stringByAppendingString:@"\n"];
- }
- }
- NSData *data = [NSJSONSerialization dataWithJSONObject:result options:NSJSONWritingPrettyPrinted error:nil];
- NSString *jsonStr = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
- return jsonStr;
- }
- @end
|