WFCUPinyinUtility.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //
  2. // WFCUPinyinUtility.m
  3. // WFChatUIKit
  4. //
  5. // Created by dali on 2021/1/28.
  6. // Copyright © 2020 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCUPinyinUtility.h"
  9. @implementation WFCUPinyinModel
  10. @end
  11. @interface WFCUPinyinUtility ()
  12. @property(nonatomic, strong)NSMutableDictionary *pinyinDict;
  13. @end
  14. @implementation WFCUPinyinUtility
  15. -(BOOL)isMatch:(NSString *)name ofPinYin:(NSString *)pinyin {
  16. if(!name.length)
  17. return NO;
  18. pinyin = [pinyin lowercaseString];
  19. if(!self.pinyinDict) {
  20. [self loadPinyin];
  21. }
  22. WFCUPinyinModel *model = [self getModelByName:name];
  23. for (NSString *jp in model.jianpin) {
  24. if([jp containsString:pinyin]) {
  25. return YES;
  26. }
  27. }
  28. for (NSString *qp in model.quanpin) {
  29. if([qp rangeOfString:pinyin].location == 0) {
  30. return YES;
  31. }
  32. }
  33. return NO;
  34. }
  35. - (WFCUPinyinModel *)getModelByName:(NSString *)name {
  36. WFCUPinyinModel *model = [[WFCUPinyinModel alloc] init];
  37. model.jianpin = [[NSMutableArray alloc] init];
  38. model.quanpin = [[NSMutableArray alloc] init];
  39. for (int j = 0; j < name.length; j++) {
  40. NSString *ch = [name substringWithRange:NSMakeRange(j, 1)];
  41. NSString *codepointHexStr =[[NSString stringWithFormat:@"%x", [name characterAtIndex:j]] uppercaseString];
  42. if ([self isChinese:ch]) {
  43. NSArray *pinyins = self.pinyinDict[codepointHexStr];
  44. NSMutableArray *temp = [[NSMutableArray alloc] init];
  45. if(!model.jianpin.count) {
  46. for (NSString *str in pinyins) {
  47. [temp addObject:[str substringToIndex:1]];
  48. }
  49. } else {
  50. for (NSString *str in pinyins) {
  51. NSString *j = [str substringToIndex:1];
  52. for (NSString *jp in model.jianpin) {
  53. NSString *newJp = [jp stringByAppendingString:j];
  54. [temp addObject:newJp];
  55. }
  56. }
  57. }
  58. model.jianpin = temp;
  59. temp = [[NSMutableArray alloc] init];
  60. for (NSString *str in pinyins) {
  61. for (NSString *qp in model.quanpin) {
  62. NSString *newqp = [qp stringByAppendingString:str];
  63. [temp addObject:newqp];
  64. }
  65. [temp addObject:str];
  66. }
  67. model.quanpin = temp;
  68. }else{
  69. ch = ch.lowercaseString;
  70. NSMutableArray *temp = [[NSMutableArray alloc] init];
  71. if(model.jianpin.count == 0) {
  72. [temp addObject:ch];
  73. } else {
  74. for (NSString *jp in model.jianpin) {
  75. NSString *newjp = [jp stringByAppendingString:ch];
  76. [temp addObject:newjp];
  77. }
  78. }
  79. model.jianpin = temp;
  80. temp = [[NSMutableArray alloc] init];
  81. for (NSString *qp in model.quanpin) {
  82. NSString *newqp = [qp stringByAppendingString:ch];
  83. [temp addObject:newqp];
  84. }
  85. [temp addObject:ch];
  86. model.quanpin = temp;
  87. }
  88. }
  89. return model;
  90. }
  91. - (void)loadPinyin {
  92. self.pinyinDict = [[NSMutableDictionary alloc] init];
  93. NSString *resourcePath = [[NSBundle bundleForClass:[self class]] resourcePath];
  94. NSString *bundlePath = [resourcePath stringByAppendingPathComponent:@"unicode_to_hanyu_pinyin.txt"];
  95. NSString* fileContents = [NSString stringWithContentsOfFile:bundlePath encoding:NSUTF8StringEncoding error:nil];
  96. // first, separate by new line
  97. NSArray* allLinedStrings = [fileContents componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
  98. for (NSString *oneLine in allLinedStrings) {
  99. NSArray *lineComponents=[oneLine componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
  100. if(lineComponents.count == 2) {
  101. NSString *str = lineComponents[1];
  102. str = [str substringWithRange:NSMakeRange(1, str.length-2)];
  103. self.pinyinDict[lineComponents[0]] = [str componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]];
  104. }
  105. }
  106. }
  107. - (BOOL)isChinese:(NSString *)text {
  108. NSString *match = @"(^[\u4e00-\u9fa5]+$)";
  109. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match];
  110. return [predicate evaluateWithObject:text];
  111. }
  112. @end