From a253b48e3478a1dada1eaef2aa6cf9f0f8ff04ba Mon Sep 17 00:00:00 2001 From: tisfeng Date: Sat, 9 Dec 2023 17:51:10 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9D=99=98=F0=9D=99=9D=F0=9D=99=9A?= =?UTF-8?q?=F0=9D=99=96=F0=9D=99=A5=20was=20detected=20as=20Simplified=20C?= =?UTF-8?q?hinese?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Feature/Service/Apple/EZAppleService.m | 20 +++++++++++++++---- .../EZCategory/NSString/NSString+EZUtils.m | 7 +++++++ 2 files changed, 23 insertions(+), 4 deletions(-) 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) {