添加西班牙词库
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
@property (nonatomic, copy) NSArray<NSString *> *simplifiedChineseWords;
|
||||
@property (nonatomic, strong) NSDictionary<NSString *, NSArray<NSString *> *> *pinyinToTraditionalMap;
|
||||
@property (nonatomic, strong) NSDictionary<NSString *, NSArray<NSString *> *> *bopomofoToChineseMap;
|
||||
@property (nonatomic, copy) NSArray<NSString *> *spanishWords;
|
||||
@end
|
||||
|
||||
@implementation KBSuggestionEngine
|
||||
@@ -38,6 +39,7 @@
|
||||
_simplifiedChineseWords = [self kb_loadSimplifiedChineseWords];
|
||||
_pinyinToTraditionalMap = [self kb_loadPinyinToTraditionalMap];
|
||||
_bopomofoToChineseMap = [self kb_loadBopomofoToChineseMap];
|
||||
_spanishWords = [self kb_loadSpanishWords];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
@@ -45,8 +47,9 @@
|
||||
- (NSArray<NSString *> *)suggestionsForPrefix:(NSString *)prefix limit:(NSUInteger)limit {
|
||||
if (prefix.length == 0 || limit == 0) { return @[]; }
|
||||
|
||||
// 根据引擎类型选择不同的联想逻辑
|
||||
switch (self.engineType) {
|
||||
case KBSuggestionEngineTypeSpanish:
|
||||
return [self kb_spanishSuggestionsForPrefix:prefix limit:limit];
|
||||
case KBSuggestionEngineTypePinyinTraditional:
|
||||
return [self kb_traditionalPinyinSuggestionsForPrefix:prefix limit:limit];
|
||||
case KBSuggestionEngineTypePinyinSimplified:
|
||||
@@ -158,6 +161,8 @@
|
||||
- (void)setEngineTypeFromString:(NSString *)engineTypeString {
|
||||
if ([engineTypeString isEqualToString:@"latin"]) {
|
||||
self.engineType = KBSuggestionEngineTypeLatin;
|
||||
} else if ([engineTypeString isEqualToString:@"spanish"]) {
|
||||
self.engineType = KBSuggestionEngineTypeSpanish;
|
||||
} else if ([engineTypeString isEqualToString:@"pinyin_traditional"]) {
|
||||
self.engineType = KBSuggestionEngineTypePinyinTraditional;
|
||||
} else if ([engineTypeString isEqualToString:@"pinyin_simplified"]) {
|
||||
@@ -531,4 +536,75 @@
|
||||
return [result copy];
|
||||
}
|
||||
|
||||
#pragma mark - Spanish Suggestions
|
||||
|
||||
- (NSArray<NSString *> *)kb_spanishSuggestionsForPrefix:(NSString *)prefix limit:(NSUInteger)limit {
|
||||
NSString *lower = prefix.lowercaseString;
|
||||
NSMutableArray<NSString *> *matches = [NSMutableArray array];
|
||||
|
||||
for (NSString *word in self.spanishWords) {
|
||||
if ([word hasPrefix:lower]) {
|
||||
[matches addObject:word];
|
||||
if (matches.count >= limit * 2) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (matches.count == 0) {
|
||||
return [self kb_latinSuggestionsForPrefix:prefix limit:limit];
|
||||
}
|
||||
|
||||
[matches sortUsingComparator:^NSComparisonResult(NSString *a, NSString *b) {
|
||||
NSInteger ca = self.selectionCounts[a].integerValue;
|
||||
NSInteger cb = self.selectionCounts[b].integerValue;
|
||||
if (ca != cb) {
|
||||
return (cb > ca) ? NSOrderedAscending : NSOrderedDescending;
|
||||
}
|
||||
return [a compare:b];
|
||||
}];
|
||||
|
||||
if (matches.count > limit) {
|
||||
return [matches subarrayWithRange:NSMakeRange(0, limit)];
|
||||
}
|
||||
return matches.copy;
|
||||
}
|
||||
|
||||
- (NSArray<NSString *> *)kb_loadSpanishWords {
|
||||
NSString *path = [[NSBundle mainBundle] pathForResource:@"spanish_words" ofType:@"json"];
|
||||
if (!path) {
|
||||
NSLog(@"[KBSuggestionEngine] spanish_words.json not found, using default words");
|
||||
return [self.class kb_defaultWords];
|
||||
}
|
||||
|
||||
NSData *data = [NSData dataWithContentsOfFile:path];
|
||||
if (!data) {
|
||||
NSLog(@"[KBSuggestionEngine] Failed to read spanish_words.json");
|
||||
return [self.class kb_defaultWords];
|
||||
}
|
||||
|
||||
NSError *error = nil;
|
||||
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
|
||||
if (error || ![json isKindOfClass:NSDictionary.class]) {
|
||||
NSLog(@"[KBSuggestionEngine] Failed to parse spanish_words.json: %@", error);
|
||||
return [self.class kb_defaultWords];
|
||||
}
|
||||
|
||||
NSArray *wordsArray = json[@"words"];
|
||||
if (![wordsArray isKindOfClass:NSArray.class]) {
|
||||
NSLog(@"[KBSuggestionEngine] Invalid words array in spanish_words.json");
|
||||
return [self.class kb_defaultWords];
|
||||
}
|
||||
|
||||
NSMutableArray<NSString *> *result = [NSMutableArray array];
|
||||
for (id item in wordsArray) {
|
||||
if ([item isKindOfClass:NSString.class]) {
|
||||
[result addObject:item];
|
||||
}
|
||||
}
|
||||
|
||||
NSLog(@"[KBSuggestionEngine] Loaded %lu Spanish words", (unsigned long)result.count);
|
||||
return result.count > 0 ? [result copy] : [self.class kb_defaultWords];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user