NTESSpellingCenter.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 "NTESSpellingCenter.h"
  9. #import "NTESPinyinConverter.h"
  10. #define SPELLING_UNIT_FULLSPELLING @"f"
  11. #define SPELLING_UNIT_SHORTSPELLING @"s"
  12. #define SPELLING_CACHE @"sc"
  13. @implementation SpellingUnit
  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 NTESSpellingCenter ()
  30. - (SpellingUnit *)calcSpellingOfString: (NSString *)source;
  31. @end
  32. @implementation NTESSpellingCenter
  33. + (NTESSpellingCenter *)sharedCenter
  34. {
  35. static NTESSpellingCenter *instance = nil;
  36. static dispatch_once_t onceToken;
  37. dispatch_once(&onceToken, ^{
  38. instance = [[NTESSpellingCenter 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. DDLogDebug(@"Spelling Cache Entries %zd", count);
  72. if (count >= kMaxEntriesCount)
  73. {
  74. DDLogDebug(@"Clear Spelling Cache %zd Entries",count);
  75. [_spellingCache removeAllObjects];
  76. }
  77. if (_spellingCache)
  78. {
  79. NSData *data = [NSKeyedArchiver archivedDataWithRootObject:_spellingCache];
  80. [data writeToFile:_filepath atomically:YES];
  81. }
  82. }
  83. }
  84. - (SpellingUnit *)spellingForString:(NSString *)source
  85. {
  86. if ([source length] == 0)
  87. {
  88. return nil;
  89. }
  90. SpellingUnit *spellingUnit = nil;
  91. @synchronized(self)
  92. {
  93. SpellingUnit *unit = [_spellingCache objectForKey:source];
  94. if (!unit)
  95. {
  96. unit = [self calcSpellingOfString:source];
  97. if ([unit.fullSpelling length] && [unit.shortSpelling length])
  98. {
  99. [_spellingCache setObject:unit forKey:source];
  100. }
  101. }
  102. spellingUnit = unit;
  103. }
  104. return spellingUnit;
  105. }
  106. - (NSString *)firstLetter:(NSString *)input
  107. {
  108. SpellingUnit *unit = [self spellingForString:input];
  109. NSString *spelling = unit.fullSpelling;
  110. return [spelling length] ? [spelling substringWithRange:NSMakeRange(0, 1)] : nil;
  111. }
  112. - (SpellingUnit *)calcSpellingOfString:(NSString *)source
  113. {
  114. NSMutableString *fullSpelling = [[NSMutableString alloc]init];
  115. NSMutableString *shortSpelling= [[NSMutableString alloc]init];
  116. for (NSInteger i = 0; i < [source length]; i++)
  117. {
  118. NSString *word = [source substringWithRange:NSMakeRange(i, 1)];
  119. NSString *pinyin = [[NTESPinyinConverter sharedInstance] toPinyin:word];
  120. if ([pinyin length])
  121. {
  122. [fullSpelling appendString:pinyin];
  123. [shortSpelling appendString:[pinyin substringToIndex:1]];
  124. }
  125. }
  126. SpellingUnit *unit = [[SpellingUnit alloc]init];
  127. unit.fullSpelling = [fullSpelling lowercaseString];
  128. unit.shortSpelling= [shortSpelling lowercaseString];
  129. return unit;
  130. }
  131. @end