PinyinFormatter.m 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //
  2. //
  3. //
  4. // Created by kimziv on 13-9-14.
  5. //
  6. #include "HanyuPinyinOutputFormat.h"
  7. #include "PinyinFormatter.h"
  8. #import "NSString+PinYin4Cocoa.h"
  9. @interface PinyinFormatter ()
  10. +(NSInteger)getNumericValue:(unichar)c;
  11. +(NSInteger)indexOfChar:(int*) table ch:(unichar)c;
  12. @end
  13. @implementation PinyinFormatter
  14. static int numericKeys[] = {
  15. 0x0030, 0x0041, 0x0061, 0x00b2, 0x00b9, 0x00bc, 0x0660, 0x06f0,
  16. 0x0966, 0x09e6, 0x09f4, 0x09f9, 0x0a66, 0x0ae6, 0x0b66, 0x0be7,
  17. 0x0bf1, 0x0bf2, 0x0c66, 0x0ce6, 0x0d66, 0x0e50, 0x0ed0, 0x0f20,
  18. 0x1040, 0x1369, 0x1373, 0x1374, 0x1375, 0x1376, 0x1377, 0x1378,
  19. 0x1379, 0x137a, 0x137b, 0x137c, 0x16ee, 0x17e0, 0x1810, 0x2070,
  20. 0x2074, 0x2080, 0x2153, 0x215f, 0x2160, 0x216c, 0x216d, 0x216e,
  21. 0x216f, 0x2170, 0x217c, 0x217d, 0x217e, 0x217f, 0x2180, 0x2181,
  22. 0x2182, 0x2460, 0x2474, 0x2488, 0x24ea, 0x2776, 0x2780, 0x278a,
  23. 0x3007, 0x3021, 0x3038, 0x3039, 0x303a, 0x3280, 0xff10, 0xff21,
  24. 0xff41,
  25. };
  26. static unichar numericValues[] = {
  27. 0x0039, 0x0030, 0x005a, 0x0037, 0x007a, 0x0057, 0x00b3, 0x00b0,
  28. 0x00b9, 0x00b8, 0x00be, 0x0000, 0x0669, 0x0660, 0x06f9, 0x06f0,
  29. 0x096f, 0x0966, 0x09ef, 0x09e6, 0x09f7, 0x09f3, 0x09f9, 0x09e9,
  30. 0x0a6f, 0x0a66, 0x0aef, 0x0ae6, 0x0b6f, 0x0b66, 0x0bf0, 0x0be6,
  31. 0x0bf1, 0x0b8d, 0x0bf2, 0x080a, 0x0c6f, 0x0c66, 0x0cef, 0x0ce6,
  32. 0x0d6f, 0x0d66, 0x0e59, 0x0e50, 0x0ed9, 0x0ed0, 0x0f29, 0x0f20,
  33. 0x1049, 0x1040, 0x1372, 0x1368, 0x1373, 0x135f, 0x1374, 0x1356,
  34. 0x1375, 0x134d, 0x1376, 0x1344, 0x1377, 0x133b, 0x1378, 0x1332,
  35. 0x1379, 0x1329, 0x137a, 0x1320, 0x137b, 0x1317, 0x137c, 0xec6c,
  36. 0x16f0, 0x16dd, 0x17e9, 0x17e0, 0x1819, 0x1810, 0x2070, 0x2070,
  37. 0x2079, 0x2070, 0x2089, 0x2080, 0x215e, 0x0000, 0x215f, 0x215e,
  38. 0x216b, 0x215f, 0x216c, 0x213a, 0x216d, 0x2109, 0x216e, 0x1f7a,
  39. 0x216f, 0x1d87, 0x217b, 0x216f, 0x217c, 0x214a, 0x217d, 0x2119,
  40. 0x217e, 0x1f8a, 0x217f, 0x1d97, 0x2180, 0x1d98, 0x2181, 0x0df9,
  41. 0x2182, 0xfa72, 0x2473, 0x245f, 0x2487, 0x2473, 0x249b, 0x2487,
  42. 0x24ea, 0x24ea, 0x277f, 0x2775, 0x2789, 0x277f, 0x2793, 0x2789,
  43. 0x3007, 0x3007, 0x3029, 0x3020, 0x3038, 0x302e, 0x3039, 0x3025,
  44. 0x303a, 0x301c, 0x3289, 0x327f, 0xff19, 0xff10, 0xff3a, 0xff17,
  45. 0xff5a, 0xff37,
  46. };
  47. + (NSString *)formatHanyuPinyinWithNSString:(NSString *)pinyinStr
  48. withHanyuPinyinOutputFormat:(HanyuPinyinOutputFormat *)outputFormat {
  49. if ((ToneTypeWithToneMark == [outputFormat toneType]) && ((VCharTypeWithV == [outputFormat vCharType]) || (VCharTypeWithUAndColon == [outputFormat vCharType]))) {
  50. @throw [NSException exceptionWithName:@"Throwing a BadHanyuPinyinOutputFormatCombination exception" reason:@"tone marks cannot be added to v or u:." userInfo:nil];
  51. }
  52. if (ToneTypeWithoutTone == [outputFormat toneType]) {
  53. pinyinStr =[pinyinStr stringByReplacingOccurrencesOfString:@"[1-5]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, pinyinStr.length)];
  54. }
  55. else if (ToneTypeWithToneMark == [outputFormat toneType]) {
  56. pinyinStr =[pinyinStr stringByReplacingOccurrencesOfString:@"u:" withString:@"v"];
  57. pinyinStr = [PinyinFormatter convertToneNumber2ToneMarkWithNSString:pinyinStr];
  58. }
  59. if (VCharTypeWithV == [outputFormat vCharType]) {
  60. pinyinStr =[pinyinStr stringByReplacingOccurrencesOfString:@"u:" withString:@"v"];
  61. }
  62. else if (VCharTypeWithUUnicode == [outputFormat vCharType]) {
  63. pinyinStr =[pinyinStr stringByReplacingOccurrencesOfString:@"u:" withString:@"ü"];
  64. }
  65. if (CaseTypeUppercase == [outputFormat caseType]) {
  66. pinyinStr = [pinyinStr uppercaseString];
  67. }
  68. return pinyinStr;
  69. }
  70. + (NSString *)convertToneNumber2ToneMarkWithNSString:(NSString *)pinyinStr {
  71. NSString *lowerCasePinyinStr = [pinyinStr lowercaseString];
  72. if ([lowerCasePinyinStr matchesPatternRegexPattern:@"[a-z]*[1-5]?"]) {
  73. unichar defautlCharValue = '$';
  74. int defautlIndexValue = -1;
  75. unichar unmarkedVowel = defautlCharValue;
  76. int indexOfUnmarkedVowel = defautlIndexValue;
  77. unichar charA = 'a';
  78. unichar charE = 'e';
  79. NSString *ouStr = @"ou";
  80. NSString *allUnmarkedVowelStr = @"aeiouv";
  81. NSString *allMarkedVowelStr = @"āáăàaēéĕèeīíĭìiōóŏòoūúŭùuǖǘǚǜü";
  82. if ([lowerCasePinyinStr matchesPatternRegexPattern:@"[a-z]*[1-5]"]) {
  83. int tuneNumber = [PinyinFormatter getNumericValue:[lowerCasePinyinStr characterAtIndex:lowerCasePinyinStr.length -1]];
  84. int indexOfA = [lowerCasePinyinStr indexOf:charA];
  85. int indexOfE = [lowerCasePinyinStr indexOf:charE];
  86. int ouIndex = [lowerCasePinyinStr indexOfString:ouStr];
  87. if (-1 != indexOfA) {
  88. indexOfUnmarkedVowel = indexOfA;
  89. unmarkedVowel = charA;
  90. }
  91. else if (-1 != indexOfE) {
  92. indexOfUnmarkedVowel = indexOfE;
  93. unmarkedVowel = charE;
  94. }
  95. else if (-1 != ouIndex) {
  96. indexOfUnmarkedVowel = ouIndex;
  97. unmarkedVowel = [ouStr characterAtIndex:0];
  98. }
  99. else {
  100. for (int i = [lowerCasePinyinStr length] - 1; i >= 0; i--) {
  101. if ([[NSString valueOfChar:[lowerCasePinyinStr characterAtIndex:i]] matchesPatternRegexPattern:@"[aeiouv]"]) {
  102. indexOfUnmarkedVowel = i;
  103. unmarkedVowel = [lowerCasePinyinStr characterAtIndex:i];
  104. break;
  105. }
  106. }
  107. }
  108. if ((defautlCharValue != unmarkedVowel) && (defautlIndexValue != indexOfUnmarkedVowel)) {
  109. int rowIndex = [allUnmarkedVowelStr indexOf:unmarkedVowel];
  110. int columnIndex = tuneNumber - 1;
  111. int vowelLocation = rowIndex * 5 + columnIndex;
  112. unichar markedVowel = [allMarkedVowelStr characterAtIndex:vowelLocation];
  113. NSMutableString *resultBuffer = [[NSMutableString alloc] init];
  114. [resultBuffer appendString:[[lowerCasePinyinStr substringToIndex:indexOfUnmarkedVowel+1] stringByReplacingOccurrencesOfString:@"v" withString:@"ü"]];
  115. [resultBuffer appendFormat:@"%C",markedVowel];
  116. [resultBuffer appendString:[[lowerCasePinyinStr substringWithRange:NSMakeRange(indexOfUnmarkedVowel + 1, lowerCasePinyinStr.length-indexOfUnmarkedVowel)] stringByReplacingOccurrencesOfString:@"v" withString:@"ü"]];
  117. return [resultBuffer description];
  118. }
  119. else {
  120. return lowerCasePinyinStr;
  121. }
  122. }
  123. else {
  124. return [lowerCasePinyinStr stringByReplacingOccurrencesOfString:@"v" withString:@"ü"];
  125. }
  126. }
  127. else {
  128. return lowerCasePinyinStr;
  129. }
  130. }
  131. +(NSInteger)getNumericValue:(unichar)c
  132. {
  133. if (c < 128) {
  134. // Optimized for ASCII
  135. if (c >= '0' && c <= '9') {
  136. return c - '0';
  137. }
  138. if (c >= 'a' && c <= 'z') {
  139. return c - ('a' - 10);
  140. }
  141. if (c >= 'A' && c <= 'Z') {
  142. return c - ('A' - 10);
  143. }
  144. return -1;
  145. }
  146. NSInteger result = [self indexOfChar:numericKeys ch:c];
  147. if (result >= 0 && c <= numericValues[result * 2]) {
  148. unichar difference = numericValues[result * 2 + 1];
  149. if (difference == 0) {
  150. return -2;
  151. }
  152. // Value is always positive, must be negative value
  153. if (difference > c) {
  154. return c - (short) difference;
  155. }
  156. return c - difference;
  157. }
  158. return -1;
  159. }
  160. +(NSInteger)indexOfChar:(int*) table ch:(unichar)c{
  161. NSInteger len=sizeof(table)/sizeof(table[0]);
  162. for (int i = 0; i < len; i++) {
  163. if (table[i] == (int) c) {
  164. return i;
  165. }
  166. }
  167. return -1;
  168. }
  169. - (id)init {
  170. return [super init];
  171. }
  172. @end