Skip to content

Commit

Permalink
perf: improve querying Apple Dictioanry, knives --> knives, knife (#252)
Browse files Browse the repository at this point in the history
  • Loading branch information
tisfeng committed Dec 17, 2023
1 parent d77a4d7 commit 29e4ce0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
1 change: 0 additions & 1 deletion Easydict/Feature/Libraries/DictionaryKit/TTTDictionary.m
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,6 @@ - (BOOL)isUserDictionary {

- (NSArray<TTTDictionaryEntry *> *)entriesForSearchTerm:(NSString *)term {
return [self entriesForSearchTerm:term searchType:TTTDictionarySearchTypeExactMatch];
;
}

- (NSArray<TTTDictionaryEntry *> *)entriesForSearchTerm:(NSString *)term searchType:(TTTDictionarySearchType)searchType {
Expand Down
35 changes: 18 additions & 17 deletions Easydict/Feature/Service/Apple/AppleDictionary/EZAppleDictionary.m
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ - (nullable NSString *)queryAllIframeHTMLResultOfWord:(NSString *)word
NSString *headword = entry.headword;

// LOG --> log, 根据 genju--> 根据 gēnjù
BOOL isTheSameHeadword = [self containsSubstring:word inString:headword];
if (html.length && isTheSameHeadword) {
BOOL isValid = [self isValidHeadword:headword queryWord:word];
if (html.length && isValid) {
[entryHTMLs addObject:html];
}
}
Expand Down Expand Up @@ -550,31 +550,32 @@ - (void)removeOriginBorderBottomCssStyle:(NSMutableString *)htmlString {
}
}

- (BOOL)containsSubstring:(NSString *)substring inString:(NSString *)string {
- (BOOL)isValidHeadword:(NSString *)headword queryWord:(NSString *)word {
NSStringCompareOptions options = NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch;

// 将文本和子字符串转换为不区分大小写和重音的标准化字符串
NSString *normalizedString = [string stringByFoldingWithOptions:options locale:[NSLocale currentLocale]];
NSString *normalizedSubstring = [substring stringByFoldingWithOptions:options locale:[NSLocale currentLocale]];

BOOL isContained = [normalizedString containsString:normalizedSubstring];
// isContain = [normalizedString isEqualToString:normalizedSubstring];

NSString *normalizedWord = [word stringByFoldingWithOptions:options locale:[NSLocale currentLocale]];
NSString *normalizedHeadword = [headword stringByFoldingWithOptions:options locale:[NSLocale currentLocale]];

/**
hoped --> hope
knives --> knives, knife
Fix: https://github.com/tisfeng/Easydict/issues/252
*/
BOOL isValid = YES;

/**
Since some user dict word result is too redundant, we need to remove some useless words.
Such as 简明英汉词典, when look up "log", the results are: -log, log-, log, we should filter the first two.
*/

if (isContained) {
// remove substring
NSString *remainedText = [normalizedString stringByReplacingOccurrencesOfString:normalizedSubstring withString:@""];
if ([remainedText isEqualToString:@"-"]) {
isContained = NO;
}
NSString *remainedText = [normalizedHeadword stringByReplacingOccurrencesOfString:normalizedWord withString:@""];
if ([remainedText isEqualToString:@"-"]) {
isValid = NO;
}

return isContained;
return isValid;
}

@end

0 comments on commit 29e4ce0

Please sign in to comment.