Преглед изворни кода

RankV3Strategy 评分增加品类得分

wangyunpeng пре 9 месеци
родитељ
комит
903114182d

+ 3 - 1
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/rank/strategy/RankV3Strategy.java

@@ -10,6 +10,7 @@ import com.tzld.longarticle.recommend.server.service.rank.RankStrategy;
 import com.tzld.longarticle.recommend.server.service.score.ScoreParam;
 import com.tzld.longarticle.recommend.server.service.score.ScoreResult;
 import com.tzld.longarticle.recommend.server.service.score.ScoreService;
+import com.tzld.longarticle.recommend.server.service.score.strategy.CategoryStrategy;
 import com.tzld.longarticle.recommend.server.service.score.strategy.SimilarityStrategy;
 import com.tzld.longarticle.recommend.server.service.score.strategy.ViewMultiplierStrategy;
 import com.tzld.longarticle.recommend.server.util.CommonCollectionUtils;
@@ -48,7 +49,8 @@ public class RankV3Strategy implements RankStrategy {
             item.setContent(c);
             item.setScoreMap(scoreMap.get(c.getId()));
             double score = 2 * item.getScore(SimilarityStrategy.class.getSimpleName())
-                    + item.getScore(ViewMultiplierStrategy.class.getSimpleName());
+                    + item.getScore(ViewMultiplierStrategy.class.getSimpleName())
+                    + item.getScore(CategoryStrategy.class.getSimpleName());
             item.setScore(score);
             return item;
         });

+ 20 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/score/AccountCategoryWeightConfig.java

@@ -0,0 +1,20 @@
+package com.tzld.longarticle.recommend.server.service.score;
+
+import lombok.Getter;
+import lombok.Setter;
+
+import java.util.List;
+
+@Getter
+@Setter
+public class AccountCategoryWeightConfig {
+    private Integer index;
+    private List<CategoryWeight> categoryWeightList;
+
+    @Getter
+    @Setter
+    public static class CategoryWeight {
+        private String category;
+        private Integer weight;
+    }
+}

+ 2 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/score/ScoreService.java

@@ -3,6 +3,7 @@ package com.tzld.longarticle.recommend.server.service.score;
 
 import com.tzld.longarticle.recommend.server.common.ThreadPoolFactory;
 import com.tzld.longarticle.recommend.server.common.enums.RankStrategyEnum;
+import com.tzld.longarticle.recommend.server.service.score.strategy.CategoryStrategy;
 import com.tzld.longarticle.recommend.server.service.score.strategy.SimilarityStrategy;
 import com.tzld.longarticle.recommend.server.service.score.strategy.ViewCountStrategy;
 import com.tzld.longarticle.recommend.server.service.score.strategy.ViewMultiplierStrategy;
@@ -90,6 +91,7 @@ public class ScoreService implements ApplicationContextAware {
         strategies.add(strategyMap.get(ViewCountStrategy.class.getSimpleName()));
         if (StringUtils.equals(param.getStrategy(), RankStrategyEnum.ArticleRankV3.getStrategy())) {
             strategies.add(strategyMap.get(ViewMultiplierStrategy.class.getSimpleName()));
+            strategies.add(strategyMap.get(CategoryStrategy.class.getSimpleName()));
         }
 
         return strategies;

+ 67 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/score/strategy/CategoryStrategy.java

@@ -0,0 +1,67 @@
+package com.tzld.longarticle.recommend.server.service.score.strategy;
+
+import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValue;
+import com.tzld.longarticle.recommend.server.model.Content;
+import com.tzld.longarticle.recommend.server.repository.aigc.CrawlerMetaArticleRepository;
+import com.tzld.longarticle.recommend.server.service.AccountContentPoolConfigService;
+import com.tzld.longarticle.recommend.server.service.score.AccountCategoryWeightConfig;
+import com.tzld.longarticle.recommend.server.service.score.Score;
+import com.tzld.longarticle.recommend.server.service.score.ScoreParam;
+import com.tzld.longarticle.recommend.server.service.score.ScoreStrategy;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+import org.springframework.util.CollectionUtils;
+
+import java.util.*;
+import java.util.stream.Collectors;
+
+@Component
+@Slf4j
+public class CategoryStrategy implements ScoreStrategy {
+
+    @Autowired
+    CrawlerMetaArticleRepository crawlerMetaArticleRepository;
+    @Autowired
+    AccountContentPoolConfigService accountContentPoolConfigService;
+
+    @ApolloJsonValue("${accountCategoryWeightConfig:{}}")
+    private Map<String, AccountCategoryWeightConfig[]> accountCategoryWeightConfigMap;
+
+
+    @Override
+    public List<Score> score(ScoreParam param) {
+        List<Score> scores = new ArrayList<>();
+        if (CollectionUtils.isEmpty(param.getContents())) {
+            return scores;
+        }
+        AccountCategoryWeightConfig[] categoryWeightConfigList = accountCategoryWeightConfigMap.get(param.getAccountName());
+        if (Objects.isNull(categoryWeightConfigList) || categoryWeightConfigList.length == 0) {
+            return scores;
+        }
+        Map<Integer, AccountCategoryWeightConfig> categoryWeightConfigMap = Arrays.stream(categoryWeightConfigList).collect(Collectors.toMap(AccountCategoryWeightConfig::getIndex, o -> o));
+        String[] contentPools = accountContentPoolConfigService.getContentPools(param.getAccountName());
+        for (Content content : param.getContents()) {
+            Score score = new Score();
+            score.setStrategy(this);
+            score.setContentId(content.getId());
+            for (int i = 0; i < contentPools.length; i++) {
+                if (contentPools[i].equals(content.getContentPoolType())) {
+                    AccountCategoryWeightConfig categoryWeightConfig = categoryWeightConfigMap.get(i+1);
+                    if (Objects.nonNull(categoryWeightConfig)) {
+                        List<AccountCategoryWeightConfig.CategoryWeight> categoryWeightList = categoryWeightConfig.getCategoryWeightList();
+                        for (AccountCategoryWeightConfig.CategoryWeight categoryWeight : categoryWeightList) {
+                            if (categoryWeight.getCategory().equals(content.getCategory())) {
+                                score.setScore(categoryWeight.getWeight());
+                            }
+                        }
+                    }
+                }
+            }
+            scores.add(score);
+        }
+
+        return scores;
+    }
+
+}