diff --git a/Easydict/Feature/Service/Apple/EZAppleService.m b/Easydict/Feature/Service/Apple/EZAppleService.m index fdbc032da..2bae7aceb 100644 --- a/Easydict/Feature/Service/Apple/EZAppleService.m +++ b/Easydict/Feature/Service/Apple/EZAppleService.m @@ -581,11 +581,10 @@ - (EZLanguage)appleDetectTextLanguage:(NSString *)text printLog:(BOOL)logFlag { NSDictionary *languageProbabilityDict = [recognizer languageHypothesesWithMaximum:5]; NLLanguage dominantLanguage = recognizer.dominantLanguage; - // !!!: Numbers will be return empty dict @{}: 729 + // !!!: languageProbabilityDict will be an empty dict @{} when text is Numbers, such as 729 if (languageProbabilityDict.count == 0) { - EZLanguage firstLanguage = EZConfiguration.shared.firstLanguage; - dominantLanguage = [self appleLanguageFromLanguageEnum:firstLanguage]; - languageProbabilityDict = @{dominantLanguage : @(0)}; + dominantLanguage = [self detectUnkownText:text]; + languageProbabilityDict = @{ dominantLanguage: @(0) }; } CFAbsoluteTime endTime = CFAbsoluteTimeGetCurrent(); @@ -599,6 +598,19 @@ - (EZLanguage)appleDetectTextLanguage:(NSString *)text printLog:(BOOL)logFlag { return languageProbabilityDict; } +- (NLLanguage)detectUnkownText:(NSString *)text { + NLLanguage language = NLLanguageEnglish; + // 729 + if ([text isNumbers]) { + EZLanguage firstLanguage = EZConfiguration.shared.firstLanguage; + language = [self appleLanguageFromLanguageEnum:firstLanguage]; + } + + // 𝙘𝙝𝙚𝙖𝙥 + + return language; +} + // designatedLanguages is supportLanguagesDictionary remove some languages - (NSArray *)designatedLanguages { NSArray *supportLanguages = [[self appleLanguagesDictionary] allValues]; diff --git a/Easydict/Feature/Utility/EZCategory/NSString/NSString+EZUtils.m b/Easydict/Feature/Utility/EZCategory/NSString/NSString+EZUtils.m index 1235aa5d4..b8ad3672a 100644 --- a/Easydict/Feature/Utility/EZCategory/NSString/NSString+EZUtils.m +++ b/Easydict/Feature/Utility/EZCategory/NSString/NSString+EZUtils.m @@ -225,6 +225,13 @@ - (BOOL)isEnglishWordWithLanguage:(EZLanguage)language { return isEnglishWord; } +- (NLLanguage)detectText { + NLTagger *tagger = [[NLTagger alloc] initWithTagSchemes:@[NLTagSchemeLanguage]]; + tagger.string = self; + NLLanguage language = [tagger dominantLanguage]; + return language; +} + - (BOOL)isEnglishWord { NSString *text = [self tryToRemoveQuotes]; if (text.length > EZEnglishWordMaxLength) {