From 694c9073aa132fabecfe08da085735bbd542a59e Mon Sep 17 00:00:00 2001 From: choykarl <253440030@qq.com> Date: Wed, 29 Nov 2023 23:17:09 +0800 Subject: [PATCH] add word collocation and change ui position --- Easydict/App/Localizable.xcstrings | 17 ++++++++++++ Easydict/Feature/Service/Bing/EZBingService.m | 15 +++++++++++ .../Feature/Service/Model/EZQueryResult.h | 2 ++ .../View/WordResultView/EZWordResultView.m | 26 +++++++++++-------- 4 files changed, 49 insertions(+), 11 deletions(-) diff --git a/Easydict/App/Localizable.xcstrings b/Easydict/App/Localizable.xcstrings index 3641d1da0..f330a85c9 100644 --- a/Easydict/App/Localizable.xcstrings +++ b/Easydict/App/Localizable.xcstrings @@ -560,6 +560,23 @@ } } }, + "collocation" : { + "extractionState" : "manual", + "localizations" : { + "en" : { + "stringUnit" : { + "state" : "translated", + "value" : "Collocation" + } + }, + "zh-Hans" : { + "stringUnit" : { + "state" : "translated", + "value" : "搭配" + } + } + } + }, "comparative" : { "localizations" : { "en" : { diff --git a/Easydict/Feature/Service/Bing/EZBingService.m b/Easydict/Feature/Service/Bing/EZBingService.m index bbf4db9e6..f354de44a 100644 --- a/Easydict/Feature/Service/Bing/EZBingService.m +++ b/Easydict/Feature/Service/Bing/EZBingService.m @@ -358,6 +358,7 @@ - (void)parseBingDictTranslate:(NSDictionary *)json word:(NSString *)word comple NSMutableArray *phonetics = [NSMutableArray array]; NSMutableArray *synonyms = [NSMutableArray array]; NSMutableArray *antonyms = [NSMutableArray array]; + NSMutableArray *collocation = [NSMutableArray array]; for (NSDictionary *meaningGroup in meaningGroups) { NSArray *partOfSpeech = meaningGroup[@"partsOfSpeech"]; @@ -429,6 +430,17 @@ - (void)parseBingDictTranslate:(NSDictionary *)json word:(NSString *)word comple antonymsPart.means = antonymMeans; [antonyms addObject:antonymsPart]; } + } else if ([description isEqualToString:@"搭配"]) { + NSArray *means = [fragments mm_map:^id _Nullable(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { + return obj[@"text"]; + }]; + if (means.count > 5) { + means = [means subarrayWithRange:NSMakeRange(0, 5)]; + } + EZTranslatePart *collocationPart = [EZTranslatePart new]; + collocationPart.part = name; + collocationPart.means = means; + [collocation addObject:collocationPart]; } if ([name isEqualToString:@"变形"]) { @@ -468,6 +480,9 @@ - (void)parseBingDictTranslate:(NSDictionary *)json word:(NSString *)word comple if (antonyms.count) { wordResult.antonyms = antonyms; } + if (collocation.count) { + wordResult.collocation = collocation; + } self.result.from = EZLanguageEnglish; self.result.to = EZLanguageSimplifiedChinese; diff --git a/Easydict/Feature/Service/Model/EZQueryResult.h b/Easydict/Feature/Service/Model/EZQueryResult.h index 754777734..ef50d831c 100644 --- a/Easydict/Feature/Service/Model/EZQueryResult.h +++ b/Easydict/Feature/Service/Model/EZQueryResult.h @@ -91,6 +91,8 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic, copy, nullable) NSArray *synonyms; /// 反义词 @property (nonatomic, copy, nullable) NSArray *antonyms; +/// 搭配 +@property (nonatomic, copy, nullable) NSArray *collocation; @end diff --git a/Easydict/Feature/ViewController/View/WordResultView/EZWordResultView.m b/Easydict/Feature/ViewController/View/WordResultView/EZWordResultView.m index 41528a360..58702293f 100644 --- a/Easydict/Feature/ViewController/View/WordResultView/EZWordResultView.m +++ b/Easydict/Feature/ViewController/View/WordResultView/EZWordResultView.m @@ -531,17 +531,6 @@ - (void)refreshWithResult:(EZQueryResult *)result { lastView = meanLabel; }]; - //同义词 - if (result.wordResult.synonyms.count) { - lastView = [self buildSynonymsAndAntonymsView:NSLocalizedString(@"synonyms", nil) parts:result.wordResult.synonyms textColor:typeTextColor typeTextFont:typeTextFont height:&height lastView:lastView]; - } - - //反义词 - if (result.wordResult.antonyms.count) { - lastView = [self buildSynonymsAndAntonymsView:NSLocalizedString(@"antonyms", nil) parts:result.wordResult.antonyms textColor:typeTextColor typeTextFont:typeTextFont height:&height lastView:lastView]; - } - - [wordResult.exchanges enumerateObjectsUsingBlock:^(EZTranslateExchange *_Nonnull obj, NSUInteger idx, BOOL *_Nonnull stop) { EZLabel *exchangeLabel = [[EZLabel alloc] init]; [self addSubview:exchangeLabel]; @@ -597,6 +586,21 @@ - (void)refreshWithResult:(EZQueryResult *)result { }]; }]; + // 同义词 + if (result.wordResult.synonyms.count) { + lastView = [self buildSynonymsAndAntonymsView:NSLocalizedString(@"synonyms", nil) parts:result.wordResult.synonyms textColor:typeTextColor typeTextFont:typeTextFont height:&height lastView:lastView]; + } + + // 反义词 + if (result.wordResult.antonyms.count) { + lastView = [self buildSynonymsAndAntonymsView:NSLocalizedString(@"antonyms", nil) parts:result.wordResult.antonyms textColor:typeTextColor typeTextFont:typeTextFont height:&height lastView:lastView]; + } + + // 搭配 + if (result.wordResult.collocation.count) { + lastView = [self buildSynonymsAndAntonymsView:NSLocalizedString(@"collocation", nil) parts:result.wordResult.collocation textColor:typeTextColor typeTextFont:typeTextFont height:&height lastView:lastView]; + } + __block NSString *lastSimpleWordPart = nil; [wordResult.simpleWords enumerateObjectsUsingBlock:^(EZTranslateSimpleWord *_Nonnull obj, NSUInteger idx, BOOL *_Nonnull stop) { EZLabel *partLabel = nil;