NIMSpellingCenter.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // NTESSpellingCenter.m
  3. // NIM
  4. //
  5. // Created by amao on 13-1-21.
  6. // Copyright (c) 2013年 Netease. All rights reserved.
  7. //
  8. #import "NIMSpellingCenter.h"
  9. #import "NIMPinyinConverter.h"
  10. #define SPELLING_UNIT_FULLSPELLING @"f"
  11. #define SPELLING_UNIT_SHORTSPELLING @"s"
  12. #define SPELLING_CACHE @"sc"
  13. @implementation NIMSpellingUnit
  14. - (void)encodeWithCoder:(NSCoder *)aCoder
  15. {
  16. [aCoder encodeObject:_fullSpelling forKey:SPELLING_UNIT_FULLSPELLING];
  17. [aCoder encodeObject:_shortSpelling forKey:SPELLING_UNIT_SHORTSPELLING];
  18. }
  19. - (id)initWithCoder:(NSCoder *)aDecoder
  20. {
  21. if (self = [super init])
  22. {
  23. self.fullSpelling = [aDecoder decodeObjectForKey:SPELLING_UNIT_FULLSPELLING];
  24. self.shortSpelling= [aDecoder decodeObjectForKey:SPELLING_UNIT_SHORTSPELLING];
  25. }
  26. return self;
  27. }
  28. @end
  29. @interface NIMSpellingCenter ()
  30. - (NIMSpellingUnit *)calcSpellingOfString: (NSString *)source;
  31. @end
  32. @implementation NIMSpellingCenter
  33. + (NIMSpellingCenter *)sharedCenter
  34. {
  35. static NIMSpellingCenter *instance = nil;
  36. static dispatch_once_t onceToken;
  37. dispatch_once(&onceToken, ^{
  38. instance = [[NIMSpellingCenter alloc]init];
  39. });
  40. return instance;
  41. }
  42. - (id)init
  43. {
  44. if (self = [super init])
  45. {
  46. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  47. NSString *appDocumentPath= [[NSString alloc] initWithFormat:@"%@/",[paths objectAtIndex:0]];
  48. _filepath = [appDocumentPath stringByAppendingPathComponent:SPELLING_CACHE];
  49. _spellingCache = nil;
  50. if ([[NSFileManager defaultManager] fileExistsAtPath:_filepath])
  51. {
  52. NSDictionary *dict = [NSKeyedUnarchiver unarchiveObjectWithFile:_filepath];
  53. if ([dict isKindOfClass:[NSDictionary class]])
  54. {
  55. _spellingCache = [[NSMutableDictionary alloc]initWithDictionary:dict];
  56. }
  57. }
  58. if (!_spellingCache)
  59. {
  60. _spellingCache = [[NSMutableDictionary alloc]init];
  61. }
  62. }
  63. return self;
  64. }
  65. - (void)saveSpellingCache
  66. {
  67. static const NSInteger kMaxEntriesCount = 5000;
  68. @synchronized(self)
  69. {
  70. NSInteger count = [_spellingCache count];
  71. if (count >= kMaxEntriesCount)
  72. {
  73. [_spellingCache removeAllObjects];
  74. }
  75. if (_spellingCache)
  76. {
  77. NSData *data = [NSKeyedArchiver archivedDataWithRootObject:_spellingCache];
  78. [data writeToFile:_filepath atomically:YES];
  79. }
  80. }
  81. }
  82. - (NIMSpellingUnit *)spellingForString:(NSString *)source
  83. {
  84. if ([source length] == 0)
  85. {
  86. return nil;
  87. }
  88. NIMSpellingUnit *spellingUnit = nil;
  89. @synchronized(self)
  90. {
  91. NIMSpellingUnit *unit = [_spellingCache objectForKey:source];
  92. if (!unit)
  93. {
  94. unit = [self calcSpellingOfString:source];
  95. if ([unit.fullSpelling length] && [unit.shortSpelling length])
  96. {
  97. [_spellingCache setObject:unit forKey:source];
  98. }
  99. }
  100. spellingUnit = unit;
  101. }
  102. return spellingUnit;
  103. }
  104. - (NSString *)firstLetter:(NSString *)input
  105. {
  106. NIMSpellingUnit *unit = [self spellingForString:input];
  107. NSString *spelling = unit.fullSpelling;
  108. return [spelling length] ? [spelling substringWithRange:NSMakeRange(0, 1)] : nil;
  109. }
  110. - (NIMSpellingUnit *)calcSpellingOfString:(NSString *)source
  111. {
  112. NSMutableString *fullSpelling = [[NSMutableString alloc]init];
  113. NSMutableString *shortSpelling= [[NSMutableString alloc]init];
  114. for (NSInteger i = 0; i < [source length]; i++)
  115. {
  116. NSString *word = [source substringWithRange:NSMakeRange(i, 1)];
  117. NSString *pinyin = [[NIMPinyinConverter sharedInstance] toPinyin:word];
  118. if ([pinyin length])
  119. {
  120. [fullSpelling appendString:pinyin];
  121. [shortSpelling appendString:[pinyin substringToIndex:1]];
  122. }
  123. }
  124. NIMSpellingUnit *unit = [[NIMSpellingUnit alloc]init];
  125. unit.fullSpelling = [fullSpelling lowercaseString];
  126. unit.shortSpelling= [shortSpelling lowercaseString];
  127. return unit;
  128. }
  129. @end