ISEResultTools.m 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //
  2. // ISEResultTools.m
  3. // IFlyMSCDemo
  4. //
  5. // Created by 张剑 on 15/3/6.
  6. //
  7. //
  8. #import "ISEResultTools.h"
  9. #import "ISEResultPhone.h"
  10. #import "ISEResultSyll.h"
  11. #import "ISEResultWord.h"
  12. #import "ISEResultSentence.h"
  13. @implementation ISEResultTools
  14. +(NSString*) toStdSymbol:(NSString*) symbol{
  15. if(!symbol){
  16. return symbol;
  17. }
  18. /**
  19. * The mapping table between iFlytek phonetic symbol and standard phonetic symbol(en)
  20. */
  21. static NSDictionary* _gISEResultPhoneHashDic;
  22. static dispatch_once_t onceToken;
  23. dispatch_once(&onceToken, ^{
  24. _gISEResultPhoneHashDic=@{
  25. @"aa" : @"ɑ:",
  26. @"oo" : @"ɔ",
  27. @"ae" : @"æ",
  28. @"ah" : @"ʌ",
  29. @"ao" : @"ɔ:",
  30. @"aw" : @"aʊ",
  31. @"ax" : @"ə",
  32. @"ay" : @"aɪ",
  33. @"eh" : @"e",
  34. @"er" : @"ə:",
  35. @"ey" : @"eɪ",
  36. @"ih" : @"ɪ",
  37. @"iy" : @"i:",
  38. @"ow" : @"əʊ",
  39. @"oy" : @"ɔɪ",
  40. @"uh" : @"ʊ",
  41. @"uw" : @"ʊ:",
  42. @"ch" : @"tʃ",
  43. @"dh" : @"ð",
  44. @"hh" : @"h",
  45. @"jh" : @"dʒ",
  46. @"ng" : @"ŋ",
  47. @"sh" : @"ʃ",
  48. @"th" : @"θ",
  49. @"zh" : @"ʒ",
  50. @"y" : @"j",
  51. @"d" : @"d",
  52. @"k" : @"k",
  53. @"l" : @"l",
  54. @"m" : @"m",
  55. @"n" : @"n",
  56. @"b" : @"b",
  57. @"f" : @"f",
  58. @"g" : @"g",
  59. @"p" : @"p",
  60. @"r" : @"r",
  61. @"s" : @"s",
  62. @"t" : @"t",
  63. @"v" : @"v",
  64. @"w" : @"w",
  65. @"z" : @"z",
  66. @"ar" : @"eə",
  67. @"ir" : @"iə",
  68. @"ur" : @"ʊə",
  69. @"tr" : @"tr",
  70. @"dr" : @"dr",
  71. @"ts" : @"ts",
  72. @"dz" : @"dz"
  73. };
  74. });
  75. NSString* stdsymbol=[_gISEResultPhoneHashDic objectForKey:symbol];
  76. return stdsymbol?stdsymbol:symbol;
  77. }
  78. NSString* const KCIFlyResultNormal=@"Right";
  79. NSString* const KCIFlyResultMiss=@"Skip";
  80. NSString* const KCIFlyResultAdd=@"Duplicate";
  81. NSString* const KCIFlyResultRepeat=@"Readback";
  82. NSString* const KCIFlyResultReplace=@"Replace";
  83. NSString* const KCIFlyResultNoise=@"Noise";
  84. NSString* const KCIFlyResultMute=@"Mute";
  85. + (NSString*)translateDpMessageInfo:(int)dpMessage {
  86. static NSDictionary* _gISEResultDpMessageHashDic;
  87. static dispatch_once_t onceToken;
  88. dispatch_once(&onceToken, ^{
  89. _gISEResultDpMessageHashDic=@{
  90. @0 : KCIFlyResultNormal,
  91. @16 : KCIFlyResultMiss,
  92. @32 : KCIFlyResultAdd,
  93. @64 : KCIFlyResultRepeat,
  94. @128 : KCIFlyResultReplace
  95. };
  96. });
  97. NSString* transDpMessage=[_gISEResultDpMessageHashDic objectForKey:[NSNumber numberWithInt:dpMessage]];
  98. return transDpMessage;
  99. }
  100. + (NSString*)translateContentInfo:(NSString*) content {
  101. if(!content){
  102. return nil;
  103. }
  104. static NSDictionary* _gISEResultContentHashDic;
  105. static dispatch_once_t onceToken;
  106. dispatch_once(&onceToken, ^{
  107. _gISEResultContentHashDic=@{
  108. @"sil" : KCIFlyResultMute,
  109. @"silv" : KCIFlyResultMute,
  110. @"fil" : KCIFlyResultNoise
  111. };
  112. });
  113. NSString* transContent=[_gISEResultContentHashDic objectForKey:content];
  114. return transContent?transContent:content;
  115. }
  116. /**
  117. * Get the format details from sentences in chinese
  118. *
  119. * @param sentences sentences in chinese
  120. * @return the format details
  121. */
  122. + (NSString*)formatDetailsForLanguageCN:(NSArray*) sentences {
  123. NSString* buffer =[[NSString alloc] init];
  124. if (!sentences) {
  125. return nil;
  126. }
  127. for (ISEResultSentence* sentence in sentences ) {
  128. if (nil == sentence.words) {
  129. continue;
  130. }
  131. for (ISEResultWord* word in sentence.words) {
  132. NSString* wContent=[ISEResultTools translateContentInfo:word.content];
  133. if ([KCIFlyResultNoise isEqualToString:wContent] || [KCIFlyResultMute isEqualToString:wContent]){
  134. continue;
  135. }
  136. buffer=[buffer stringByAppendingFormat:@"\nWord[%@] %@ Dur:%d",wContent,word.symbol,word.time_len];
  137. if (!word.sylls) {
  138. continue;
  139. }
  140. for (ISEResultSyll* syll in word.sylls) {
  141. NSString* syContent=[ISEResultTools translateContentInfo:[syll content]];
  142. if ([KCIFlyResultNoise isEqualToString:syContent] || [KCIFlyResultMute isEqualToString:syContent]){
  143. continue;
  144. }
  145. buffer=[buffer stringByAppendingFormat:@"\n└Syllable[%@] %@ Dur:%d",syContent,syll.symbol,syll.time_len];
  146. if (!syll.phones) {
  147. continue;
  148. }
  149. for (ISEResultPhone* phone in syll.phones) {
  150. NSString* pContent=[ISEResultTools translateContentInfo:[phone content]];
  151. NSString* pDpMessage=[ISEResultTools translateDpMessageInfo:phone.dp_message];
  152. buffer=[buffer stringByAppendingFormat:@"\n\t└Phoneme[%@] Dur:%d Msg:%@",pContent,phone.time_len,pDpMessage];
  153. }
  154. }
  155. buffer=[buffer stringByAppendingString:@"\n"];
  156. }
  157. }
  158. return buffer;
  159. }
  160. /**
  161. * Get the format details from sentences in english
  162. *
  163. * @param sentences sentences in english
  164. * @return the format details
  165. */
  166. + (NSString*)formatDetailsForLanguageEN:(NSArray*) sentences {
  167. // NSString* buffer =[[NSString alloc] init];
  168. if (!sentences) {
  169. return nil;
  170. }
  171. NSMutableArray * result = [[NSMutableArray alloc] init];
  172. for (ISEResultSentence* sentence in sentences ) {
  173. NSString* sContent=[ISEResultTools translateContentInfo:sentence.content];
  174. if ([KCIFlyResultNoise isEqualToString:sContent] || [KCIFlyResultMute isEqualToString:sContent]){
  175. continue;
  176. }
  177. if (nil == sentence.words) {
  178. continue;
  179. }
  180. for (ISEResultWord* word in sentence.words) {
  181. NSString* wContent=[ISEResultTools translateContentInfo:word.content];
  182. // NSString* wDpMessage=[ISEResultTools translateDpMessageInfo:word.dp_message];
  183. if ([KCIFlyResultNoise isEqualToString:wContent] || [KCIFlyResultMute isEqualToString:wContent]){
  184. continue;
  185. }
  186. NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
  187. [dictionary setValue:wContent forKey:@"content"];
  188. [dictionary setValue:@(word.total_score) forKey:@"score"];
  189. [result addObject: dictionary];
  190. // buffer=[buffer stringByAppendingFormat:@"\nWord[%@] Msg:%@ Score:%f",wContent,wDpMessage,word.total_score];
  191. // if (!word.sylls) {
  192. // buffer=[buffer stringByAppendingString:@"\n"];
  193. // continue;
  194. // }
  195. //
  196. // for (ISEResultSyll* syll in word.sylls) {
  197. // NSString* syContent=[ISEResultTools translateContentInfo:[syll getStdSymbol]];
  198. // buffer=[buffer stringByAppendingFormat:@"\n└Syllable[%@] ",syContent];
  199. // if (!syll.phones) {
  200. // continue;
  201. // }
  202. //
  203. // for (ISEResultPhone* phone in syll.phones) {
  204. // NSString* pContent=[ISEResultTools translateContentInfo:[phone getStdSymbol]];
  205. // NSString* pDpMessage=[ISEResultTools translateDpMessageInfo:phone.dp_message];
  206. // buffer=[buffer stringByAppendingFormat:@"\n\t└Phoneme[%@] Msg:%@",pContent,pDpMessage];
  207. // }
  208. //
  209. // }
  210. // buffer=[buffer stringByAppendingString:@"\n"];
  211. }
  212. }
  213. NSData *data = [NSJSONSerialization dataWithJSONObject:result options:NSJSONWritingPrettyPrinted error:nil];
  214. NSString *jsonStr = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
  215. return jsonStr;
  216. }
  217. @end