فهرست منبع

预分配 改为在计算评分时增加权重

wangyunpeng 8 ماه پیش
والد
کامیت
b35545d7a0

+ 1 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/RecommendService.java

@@ -129,6 +129,7 @@ public class RecommendService {
         rankParam.setContents(contentList);
         rankParam.setContents(contentList);
         rankParam.setBackup(backup);
         rankParam.setBackup(backup);
         rankParam.setStrategy(param.getStrategy());
         rankParam.setStrategy(param.getStrategy());
+        rankParam.setGhId(param.getGhId());
         rankParam.setAccountName(param.getAccountName());
         rankParam.setAccountName(param.getAccountName());
         rankParam.setSize(param.getPublishNum());
         rankParam.setSize(param.getPublishNum());
         return rankParam;
         return rankParam;

+ 0 - 3
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/filter/FilterService.java

@@ -101,9 +101,6 @@ public class FilterService {
         strategies.add(ServiceBeanFactory.getBean(HistoryTitleStrategy.class));
         strategies.add(ServiceBeanFactory.getBean(HistoryTitleStrategy.class));
         strategies.add(ServiceBeanFactory.getBean(BadStrategy.class));
         strategies.add(ServiceBeanFactory.getBean(BadStrategy.class));
         strategies.add(ServiceBeanFactory.getBean(SensitiveStrategy.class));
         strategies.add(ServiceBeanFactory.getBean(SensitiveStrategy.class));
-//        if (StringUtils.equals(param.getStrategy(), RankStrategyEnum.ArticleRankV3.getStrategy())) {
-//            strategies.add(ServiceBeanFactory.getBean(AccountPreDistributeStrategy.class));
-//        }
         return strategies;
         return strategies;
     }
     }
 }
 }

+ 1 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/rank/RankParam.java

@@ -10,6 +10,7 @@ import java.util.List;
  */
  */
 @Data
 @Data
 public class RankParam {
 public class RankParam {
+    private String ghId;
     private String accountName;
     private String accountName;
     private List<Content> contents;
     private List<Content> contents;
     private List<Content> backup;
     private List<Content> backup;

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

@@ -123,6 +123,7 @@ public class DefaultRankStrategy implements RankStrategy {
 
 
     private ScoreParam convertToScoreParam(RankParam param) {
     private ScoreParam convertToScoreParam(RankParam param) {
         ScoreParam scoreParam = new ScoreParam();
         ScoreParam scoreParam = new ScoreParam();
+        scoreParam.setGhId(param.getGhId());
         scoreParam.setAccountName(param.getAccountName());
         scoreParam.setAccountName(param.getAccountName());
         scoreParam.setContents(param.getContents());
         scoreParam.setContents(param.getContents());
         return scoreParam;
         return scoreParam;

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

@@ -75,6 +75,7 @@ public class RankV2Strategy implements RankStrategy {
 
 
     private ScoreParam convertToScoreParam(RankParam param) {
     private ScoreParam convertToScoreParam(RankParam param) {
         ScoreParam scoreParam = new ScoreParam();
         ScoreParam scoreParam = new ScoreParam();
+        scoreParam.setGhId(param.getGhId());
         scoreParam.setAccountName(param.getAccountName());
         scoreParam.setAccountName(param.getAccountName());
         scoreParam.setContents(param.getContents());
         scoreParam.setContents(param.getContents());
         return scoreParam;
         return scoreParam;

+ 4 - 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.ScoreParam;
 import com.tzld.longarticle.recommend.server.service.score.ScoreResult;
 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.ScoreService;
+import com.tzld.longarticle.recommend.server.service.score.strategy.AccountPreDistributeStrategy;
 import com.tzld.longarticle.recommend.server.service.score.strategy.CategoryStrategy;
 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.SimilarityStrategy;
 import com.tzld.longarticle.recommend.server.service.score.strategy.ViewMultiplierStrategy;
 import com.tzld.longarticle.recommend.server.service.score.strategy.ViewMultiplierStrategy;
@@ -50,7 +51,8 @@ public class RankV3Strategy implements RankStrategy {
             item.setScoreMap(scoreMap.get(c.getId()));
             item.setScoreMap(scoreMap.get(c.getId()));
             double score = 2 * item.getScore(SimilarityStrategy.class.getSimpleName())
             double score = 2 * item.getScore(SimilarityStrategy.class.getSimpleName())
                     + item.getScore(ViewMultiplierStrategy.class.getSimpleName())
                     + item.getScore(ViewMultiplierStrategy.class.getSimpleName())
-                    + item.getScore(CategoryStrategy.class.getSimpleName());
+                    + item.getScore(CategoryStrategy.class.getSimpleName())
+                    + item.getScore(AccountPreDistributeStrategy.class.getSimpleName());
             item.setScore(score);
             item.setScore(score);
             return item;
             return item;
         });
         });
@@ -143,6 +145,7 @@ public class RankV3Strategy implements RankStrategy {
 
 
     private ScoreParam convertToScoreParam(RankParam param) {
     private ScoreParam convertToScoreParam(RankParam param) {
         ScoreParam scoreParam = new ScoreParam();
         ScoreParam scoreParam = new ScoreParam();
+        scoreParam.setGhId(param.getGhId());
         scoreParam.setAccountName(param.getAccountName());
         scoreParam.setAccountName(param.getAccountName());
         scoreParam.setContents(param.getContents());
         scoreParam.setContents(param.getContents());
         scoreParam.setStrategy(param.getStrategy());
         scoreParam.setStrategy(param.getStrategy());

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

@@ -16,6 +16,7 @@ import java.util.List;
 @AllArgsConstructor
 @AllArgsConstructor
 @NoArgsConstructor
 @NoArgsConstructor
 public class ScoreParam {
 public class ScoreParam {
+    private String ghId;
     private String accountName;
     private String accountName;
     private List<Content> contents;
     private List<Content> contents;
     private String strategy;
     private String strategy;

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

@@ -3,10 +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.ThreadPoolFactory;
 import com.tzld.longarticle.recommend.server.common.enums.RankStrategyEnum;
 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;
+import com.tzld.longarticle.recommend.server.service.score.strategy.*;
 import com.tzld.longarticle.recommend.server.util.CommonCollectionUtils;
 import com.tzld.longarticle.recommend.server.util.CommonCollectionUtils;
 import com.tzld.longarticle.recommend.server.util.JSONUtils;
 import com.tzld.longarticle.recommend.server.util.JSONUtils;
 import lombok.extern.slf4j.Slf4j;
 import lombok.extern.slf4j.Slf4j;
@@ -92,6 +89,7 @@ public class ScoreService implements ApplicationContextAware {
         if (StringUtils.equals(param.getStrategy(), RankStrategyEnum.ArticleRankV3.getStrategy())) {
         if (StringUtils.equals(param.getStrategy(), RankStrategyEnum.ArticleRankV3.getStrategy())) {
             strategies.add(strategyMap.get(ViewMultiplierStrategy.class.getSimpleName()));
             strategies.add(strategyMap.get(ViewMultiplierStrategy.class.getSimpleName()));
             strategies.add(strategyMap.get(CategoryStrategy.class.getSimpleName()));
             strategies.add(strategyMap.get(CategoryStrategy.class.getSimpleName()));
+            strategies.add(strategyMap.get(AccountPreDistributeStrategy.class.getSimpleName()));
         }
         }
 
 
         return strategies;
         return strategies;

+ 34 - 19
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/filter/strategy/AccountPreDistributeStrategy.java → long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/score/strategy/AccountPreDistributeStrategy.java

@@ -1,18 +1,19 @@
-package com.tzld.longarticle.recommend.server.service.filter.strategy;
+package com.tzld.longarticle.recommend.server.service.score.strategy;
 
 
+import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValue;
 import com.google.common.reflect.TypeToken;
 import com.google.common.reflect.TypeToken;
 import com.tzld.longarticle.recommend.server.model.Content;
 import com.tzld.longarticle.recommend.server.model.Content;
-import com.tzld.longarticle.recommend.server.repository.entity.crawler.ArticlePreDistributeAccount;
 import com.tzld.longarticle.recommend.server.repository.crawler.ArticlePreDistributeAccountRepository;
 import com.tzld.longarticle.recommend.server.repository.crawler.ArticlePreDistributeAccountRepository;
+import com.tzld.longarticle.recommend.server.repository.entity.crawler.ArticlePreDistributeAccount;
 import com.tzld.longarticle.recommend.server.service.AccountContentPoolConfigService;
 import com.tzld.longarticle.recommend.server.service.AccountContentPoolConfigService;
-import com.tzld.longarticle.recommend.server.service.filter.FilterParam;
-import com.tzld.longarticle.recommend.server.service.filter.FilterResult;
-import com.tzld.longarticle.recommend.server.service.filter.FilterStrategy;
+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 com.tzld.longarticle.recommend.server.util.JSONUtils;
 import com.tzld.longarticle.recommend.server.util.JSONUtils;
 import lombok.extern.slf4j.Slf4j;
 import lombok.extern.slf4j.Slf4j;
-import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 import org.springframework.stereotype.Component;
+import org.springframework.util.StringUtils;
 
 
 import java.util.*;
 import java.util.*;
 
 
@@ -21,25 +22,27 @@ import java.util.*;
  */
  */
 @Component
 @Component
 @Slf4j
 @Slf4j
-public class AccountPreDistributeStrategy implements FilterStrategy {
+public class AccountPreDistributeStrategy implements ScoreStrategy {
+
     @Autowired
     @Autowired
     private AccountContentPoolConfigService accountContentPoolConfigService;
     private AccountContentPoolConfigService accountContentPoolConfigService;
     @Autowired
     @Autowired
     private ArticlePreDistributeAccountRepository articlePreDistributeAccountRepository;
     private ArticlePreDistributeAccountRepository articlePreDistributeAccountRepository;
 
 
+    @ApolloJsonValue("${accountPreDistributeWeightConfig:{}}")
+    private Map<String, Integer> accountPreDistributeWeightMap;
+
     @Override
     @Override
-    public FilterResult filter(FilterParam param) {
+    public List<Score> score(ScoreParam param) {
         long start = System.currentTimeMillis();
         long start = System.currentTimeMillis();
-        FilterResult filterResult = new FilterResult();
-        List<String> result = new ArrayList<>();
+        List<Score> scores = new ArrayList<>();
         List<Content> contents = param.getContents();
         List<Content> contents = param.getContents();
-        List<Content> filterContents = new ArrayList<>();
         Set<String> articles = new HashSet<>();
         Set<String> articles = new HashSet<>();
         String[] pools = accountContentPoolConfigService.getContentPools(param.getAccountName());
         String[] pools = accountContentPoolConfigService.getContentPools(param.getAccountName());
         if (pools.length >= 3) {
         if (pools.length >= 3) {
             try {
             try {
                 ArticlePreDistributeAccount optional = articlePreDistributeAccountRepository.findFirstByGhIdOrderByDateDesc(param.getGhId());
                 ArticlePreDistributeAccount optional = articlePreDistributeAccountRepository.findFirstByGhIdOrderByDateDesc(param.getGhId());
-                if (optional != null && StringUtils.isNotEmpty(optional.getArticleList())) {
+                if (optional != null && StringUtils.hasText(optional.getArticleList())) {
                     List<String[]> list = JSONUtils.fromJson(optional.getArticleList(), new TypeToken<List<String[]>>() {
                     List<String[]> list = JSONUtils.fromJson(optional.getArticleList(), new TypeToken<List<String[]>>() {
                     }, Collections.emptyList());
                     }, Collections.emptyList());
                     for (String[] s : list) {
                     for (String[] s : list) {
@@ -50,20 +53,32 @@ public class AccountPreDistributeStrategy implements FilterStrategy {
                 e.printStackTrace();
                 e.printStackTrace();
             }
             }
         }
         }
-
+        Integer weight = getPreDistributeWeight(param.getAccountName());
         for (Content content : contents) {
         for (Content content : contents) {
-            if (StringUtils.equals(pools[2], content.getContentPoolType())) {
+            Score score = new Score();
+            score.setStrategy(this);
+            if (pools[2].equals(content.getContentPoolType())) {
                 if (articles.contains(content.getCrawlerChannelContentId())) {
                 if (articles.contains(content.getCrawlerChannelContentId())) {
-                    result.add(content.getId());
+                    score.setContentId(content.getId());
+                    score.setScore(weight);
                 }
                 }
             } else {
             } else {
-                result.add(content.getId());
+                score.setContentId(content.getId());
+                score.setScore(weight);
+            }
+            if (StringUtils.hasText(score.getContentId())) {
+                scores.add(score);
             }
             }
         }
         }
-        filterResult.setContentIds(result);
-        filterResult.setFilterContent(filterContents);
         log.info("AccountPreDistributeStrategy cost:{}", System.currentTimeMillis() - start);
         log.info("AccountPreDistributeStrategy cost:{}", System.currentTimeMillis() - start);
-        return filterResult;
+        return scores;
+    }
+
+    public Integer getPreDistributeWeight(String accountName) {
+        if (accountPreDistributeWeightMap.containsKey(accountName)) {
+            return accountPreDistributeWeightMap.get(accountName);
+        }
+        return accountPreDistributeWeightMap.get("default");
     }
     }
 
 
 }
 }