Explorar o código

Merge branch 'wyp/0527-rankV18' of Server/long-article-recommend into master

wangyunpeng hai 1 mes
pai
achega
de93b9b92d

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

@@ -23,6 +23,7 @@ public enum RankStrategyEnum {
     ArticleRankV15("ArticleRankV15", "ArticleRankV15", "rankV15Strategy"),
     ArticleRankV16("ArticleRankV16", "ArticleRankV16", "rankV16Strategy"),
     ArticleRankV17("ArticleRankV17", "ArticleRankV17", "rankV17Strategy"),
+    ArticleRankV18("ArticleRankV18", "ArticleRankV18", "rankV18Strategy"),
 
     HIS_JUMP_STRATEGY("ArticleRankHisJump", "历史表现跳过相似度策略", "hisJumpRankStrategy"),
     INFINITE_STRATEGY("ArticleRankInfinite", "无限发表", "infiniteRankStrategy"),

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

@@ -11,6 +11,7 @@ public enum ScoreStrategyEnum {
     HIS_FISSION_DE_WEIGHT_AVG_READ_SUM_RATE("HisFissionDeWeightAvgReadSumRateStrategy"),
     HIS_FISSION_FANS_RATE_RATE("HisFissionFansRateRateStrategy"),
     HIS_FISSION_FANS_SUM_RATE("HisFissionFansSumRateStrategy"),
+    HIS_FISSION_OPEN_RATE("HisFissionOpenRateStrategy"),
     PUBLISH_TIMES("PublishTimesStrategy"),
     SIMILARITY("SimilarityStrategy"),
     VIEW_COUNT_RATE_CORRELATION("ViewCountRateCorrelationStrategy"),

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

@@ -49,6 +49,7 @@ public class Content {
     private Double t0FissionDeWeightByReadAvgSumAvg;
     private Double t0FissionHeadDeWeightByReadAvgSumAvg;
     private Double t0FissionRecommendDeWeightByReadAvgSumAvg;
+    private Double t0FissionOpenRateSumAvg;
 
     private Map<String, Double> scoreMap;
     private double score;

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

@@ -20,6 +20,7 @@ public class ContentHisPublishArticle {
     private Long updateTime;
     private Long publishTimestamp;
     private Integer avgViewCount;
+    private Double avgOpenRate;
     private Double readAvgCiUpper;
     private Double viewCountRate;
     private Integer firstViewCount;

+ 2 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/model/entity/crawler/AccountAvgInfo.java

@@ -30,6 +30,8 @@ public class AccountAvgInfo implements Serializable {
     private Integer fans;
     @Column(name = "read_avg")
     private Double readAvg;
+    @Column(name = "open_rate_avg")
+    private Double openRateAvg;
     @Column(name = "like_avg")
     private Double likeAvg;
     @Column(name = "status")

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

@@ -0,0 +1,175 @@
+package com.tzld.longarticle.recommend.server.service.recommend.rank.strategy;
+
+
+import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValue;
+import com.tzld.longarticle.recommend.server.common.enums.aigc.PublishPlanInputSourceTypesEnum;
+import com.tzld.longarticle.recommend.server.common.enums.recommend.ScoreStrategyEnum;
+import com.tzld.longarticle.recommend.server.model.dto.Content;
+import com.tzld.longarticle.recommend.server.model.entity.crawler.Article;
+import com.tzld.longarticle.recommend.server.repository.crawler.ArticleRepository;
+import com.tzld.longarticle.recommend.server.service.recommend.config.AccountContentPoolConfigService;
+import com.tzld.longarticle.recommend.server.service.recommend.config.StrategyIndexScoreWeightService;
+import com.tzld.longarticle.recommend.server.service.recommend.rank.*;
+import com.tzld.longarticle.recommend.server.service.recommend.score.AccountIndexReplacePoolConfig;
+import com.tzld.longarticle.recommend.server.service.recommend.score.ScoreResult;
+import com.tzld.longarticle.recommend.server.service.recommend.score.ScoreService;
+import com.tzld.longarticle.recommend.server.util.CommonCollectionUtils;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.lang3.RandomUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+
+import java.util.*;
+import java.util.stream.Collectors;
+
+/**
+ * @author dyp
+ */
+@Service
+@Slf4j
+public class RankV18Strategy implements RankStrategy {
+
+    @Autowired
+    private ScoreService scoreService;
+    @Autowired
+    private AccountContentPoolConfigService accountContentPoolConfigService;
+    @Autowired
+    private ArticleRepository articleRepository;
+    @Autowired
+    private StrategyIndexScoreWeightService weightService;
+
+    @ApolloJsonValue("${touliu.account.ghIds:[\"gh_93e00e187787\", \"gh_ac43e43b253b\", \"gh_68e7fdc09fe4\",\"gh_77f36c109fb1\", \"gh_b181786a6c8c\", \"gh_1ee2e1b39ccf\"]}")
+    private List<String> touliuAccountGhIds;
+    @Value("${topProducePlanId:}")
+    private String topProducePlanId;
+
+    public RankResult rank(RankParam param) {
+        List<Content> result = new ArrayList<>();
+
+        ScoreResult scoreResult = scoreService.score(RankStrategy.convertToScoreParam(param));
+
+        Map<String, Map<String, Double>> scoreMap = scoreResult.getScoreMap();
+        String[] contentPools = accountContentPoolConfigService.getContentPools(param.getAccountName());
+        Map<Integer, AccountIndexReplacePoolConfig> indexReplacePoolConfigMap = accountContentPoolConfigService.getContentReplacePools(param.getAccountName());
+
+        List<RankItem> items = CommonCollectionUtils.toList(param.getContents(), c -> {
+            RankItem item = new RankItem();
+            item.setContent(c);
+            c.setScoreMap(scoreMap.get(c.getId()));
+            item.setScoreMap(scoreMap.get(c.getId()));
+            double score;
+            int index = weightService.getIndex(item.getContent().getContentPoolType(), contentPools);
+            if (contentPools[0].equals(item.getContent().getContentPoolType())
+                    || contentPools[1].equals(item.getContent().getContentPoolType())) {
+                score = item.getScore(ScoreStrategyEnum.SIMILARITY.value())
+                        * weightService.getWeight(param.getStrategy(), param.getGhId(), index,
+                        ScoreStrategyEnum.SIMILARITY.value())
+                        + item.getScore(ScoreStrategyEnum.CATEGORY.value())
+                        * weightService.getWeight(param.getStrategy(), param.getGhId(), index,
+                        ScoreStrategyEnum.CATEGORY.value())
+                        + item.getScore(ScoreStrategyEnum.HIS_FISSION_OPEN_RATE.value())
+                        * weightService.getWeight(param.getStrategy(), param.getGhId(), index,
+                        ScoreStrategyEnum.HIS_FISSION_OPEN_RATE.value())
+                        + item.getScore(ScoreStrategyEnum.FLOW_CTL_DECREASE.value())
+                        + item.getScore(ScoreStrategyEnum.CRAWLER_DAYS_DECREASE_STRATEGY.value());
+                if (item.getScore(ScoreStrategyEnum.PUBLISH_TIMES.value()) >= 0) {
+                    score += item.getScore(ScoreStrategyEnum.VIEW_COUNT_RATE_V2.value())
+                            * weightService.getWeight(param.getStrategy(), param.getGhId(), index,
+                            ScoreStrategyEnum.VIEW_COUNT_RATE_V2.value());
+                }
+            } else {
+                score = item.getScore(ScoreStrategyEnum.SIMILARITY.value())
+                        * weightService.getWeight(param.getStrategy(), param.getGhId(), index,
+                        ScoreStrategyEnum.SIMILARITY.value())
+                        + item.getScore(ScoreStrategyEnum.CATEGORY.value())
+                        * weightService.getWeight(param.getStrategy(), param.getGhId(), index,
+                        ScoreStrategyEnum.CATEGORY.value())
+                        + item.getScore(ScoreStrategyEnum.ACCOUNT_PRE_DISTRIBUTE.value())
+                        + item.getScore(ScoreStrategyEnum.PUBLISH_TIMES.value())
+                        + item.getScore(ScoreStrategyEnum.CRAWLER_DAYS_DECREASE_STRATEGY.value())
+                        + item.getScore(ScoreStrategyEnum.FLOW_CTL_DECREASE.value());
+            }
+            c.setScore(score);
+            item.setScore(score);
+            return item;
+        });
+        // 相似度评分为0 报警返回
+        List<Article> hisPublishFirstArticleList = articleRepository.getByGhIdAndItemIndexAndTypeEqualsAndStatusEquals(
+                param.getGhId(), 1, param.getType(), 1);
+        if (RankStrategy.SimilarityScoreZero(items, param, hisPublishFirstArticleList)) {
+            return new RankResult(result);
+        }
+        // 安全分降权
+        RankService.safeScoreDecrease(items);
+
+        // 1 排序
+        Collections.sort(items, (o1, o2) -> -Double.compare(o1.getScore(), o2.getScore()));
+        // 2 相似去重
+        List<Content> contents = CommonCollectionUtils.toList(items, RankItem::getContent);
+
+        // 3 文章按照内容池分组
+        Map<String, List<Content>> contentMap = new HashMap<>();
+        for (Content c : contents) {
+            List<Content> data = contentMap.computeIfAbsent(c.getContentPoolType(), k -> new ArrayList<>());
+            data.add(c);
+        }
+        // 4 选文章
+        String[] publishPool = Arrays.copyOf(contentPools, contentPools.length);
+
+        // 头
+        List<Content> pool1 = contentMap.get(contentPools[0]);
+        if (CollectionUtils.isNotEmpty(pool1)) {
+            pool1 = RankService.contentSourceTypeFilter(param.getStrategy(), pool1, 1);
+        }
+        RankService.printSortLog(param.getStrategy(), param.getAccountName(), "头条", pool1);
+        if (CollectionUtils.isNotEmpty(pool1)) {
+            if (topProducePlanId.equals(pool1.get(0).getProducePlanId())) {
+                int i = RandomUtils.nextInt(0, 2);
+                if (i == 0) {
+                    for (Content content : pool1) {
+                        if (!topProducePlanId.equals(content.getProducePlanId())) {
+                            result.add(content);
+                            break;
+                        }
+                    }
+                }
+            }
+            if (CollectionUtils.isEmpty(result)) {
+                result.add(pool1.get(0));
+            }
+        } else {
+            RankStrategy.sendFeishuFirstPoolEmpty(param, contentPools[0]);
+            return new RankResult(result);
+        }
+
+        // 次
+        RankService.commonAddSecondContent(param, result, publishPool, contentPools, contentMap,
+                indexReplacePoolConfigMap, param.getStrategy());
+
+        // 3-8
+        // RankService.commonAdd38Content(param, result, contentPools, contentMap, param.getStrategy());
+        List<Content> pool = contentMap.get(contentPools[2]);
+        if (CollectionUtils.isNotEmpty(pool)) {
+            Integer videoSourceType = PublishPlanInputSourceTypesEnum.longArticleVideoPoolSource.getVal();
+            Queue<Content> videoPoolQueue = pool.stream().filter(o -> Objects.equals(o.getSourceType(), videoSourceType))
+                    .collect(Collectors.toCollection(LinkedList::new));
+            Queue<Content> otherPoolQueue = pool.stream().filter(o -> !Objects.equals(o.getSourceType(), videoSourceType))
+                    .collect(Collectors.toCollection(LinkedList::new));
+            for (int i = 3; i < param.getSize() + 1; i++) {
+                Integer sourceType = RankService.getStrategyPoolSourceType(param.getStrategy(), i);
+                if (Objects.equals(sourceType, videoSourceType) && !videoPoolQueue.isEmpty()) {
+                    result.add(videoPoolQueue.poll());
+                } else if (!otherPoolQueue.isEmpty()) {
+                    result.add(otherPoolQueue.poll());
+                }
+            }
+        }
+
+        RankStrategy.deduplication(result, contentMap, publishPool);
+
+        return new RankResult(result);
+    }
+
+}

+ 19 - 1
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/recommend/recall/RecallService.java

@@ -593,6 +593,7 @@ public class RecallService implements ApplicationContextAware {
                 article.setArticleDetailInfoList(articleDetailInfoMap.get(hisArticle.getWxSn()));
                 // 设置账号位置阅读均值
                 int avgViewCount = 0;
+                double avgOpenRate = 0.0;
                 double readAvgCiUpper = 0.0;
                 Map<String, Map<String, AccountAvgInfo>> dateAvgMap = accountAvgInfoIndexMap.get(hisArticle.getGhId());
                 String hisPublishDate = DateUtils.timestampToYMDStr(article.getPublishTimestamp(), "yyyy-MM-dd");
@@ -604,6 +605,8 @@ public class RecallService implements ApplicationContextAware {
                         article.setInnerAccount(true);
                         avgViewCount = Optional.ofNullable(indexMap.get(hisArticle.getItemIndex().toString()).getReadAvg())
                                 .orElse(0.0).intValue();
+                        avgOpenRate = Optional.ofNullable(indexMap.get(hisArticle.getItemIndex().toString()).getOpenRateAvg())
+                                .orElse(0.0).intValue();
                         readAvgCiUpper = Optional.ofNullable(indexMap.get(hisArticle.getItemIndex().toString()).getReadAvgCiUpper())
                                 .orElse(0.0).intValue();
                     } else {
@@ -621,6 +624,7 @@ public class RecallService implements ApplicationContextAware {
                     }
                 }
                 article.setAvgViewCount(avgViewCount);
+                article.setAvgOpenRate(avgOpenRate);
                 article.setReadAvgCiUpper(readAvgCiUpper);
                 if (Objects.nonNull(article.getAvgViewCount()) && article.getAvgViewCount() > 0
                         && Objects.nonNull(article.getViewCount())) {
@@ -659,6 +663,7 @@ public class RecallService implements ApplicationContextAware {
         Account account = accountRepository.getByGhId(ghId);
         int firstLevelSize = 0;
         int fissionSum = 0;
+        int firstLevelSum = 0;
         double fissionWeightSum = 0;
         double fissionHeadWeightSum = 0;
         double fissionRecommendWeightSum = 0;
@@ -667,6 +672,7 @@ public class RecallService implements ApplicationContextAware {
         Double t0FissionByFansSum = 0.0;
         Double t0FissionByReadAvgSum = 0.0;
         Double t0FissionByReadAvgCorrelationSum = 0.0;
+        Double openRateAvgRate = 0.0;
         for (ContentHisPublishArticle article : content.getHisPublishArticleList()) {
             if (article.getItemIndex() != 1 || !article.isInnerAccount()) {
                 continue;
@@ -691,6 +697,7 @@ public class RecallService implements ApplicationContextAware {
                 }
                 continue;
             }
+            int sumFirstLevel = 0;
             int sumFission0 = 0;
             int sumFission0Head = 0;
             int sumFission0Recommend = 0;
@@ -701,6 +708,9 @@ public class RecallService implements ApplicationContextAware {
                     if (Objects.nonNull(articleDetailInfo.getFission0())) {
                         sumFission0 += articleDetailInfo.getFission0();
                     }
+                    if (Objects.nonNull(articleDetailInfo.getFirstLevel())) {
+                        sumFirstLevel += articleDetailInfo.getFirstLevel();
+                    }
                     if (Objects.nonNull(articleDetailInfo.getFission0Head())) {
                         sumFission0Head += articleDetailInfo.getFission0Head();
                     }
@@ -714,6 +724,8 @@ public class RecallService implements ApplicationContextAware {
             }
             Double correlation = (article.getCorrelation() + 1) / 2;
             article.setT0FissionSum(sumFission0);
+            fissionSum += sumFission0;
+            firstLevelSum += sumFirstLevel;
             if (article.getFans() > 0) {
                 article.setT0FissionByFans(sumFission0 * 1.0 / article.getFans());
                 fansSum += (int) article.getFans();
@@ -725,7 +737,9 @@ public class RecallService implements ApplicationContextAware {
                 t0FissionByReadAvgSum += article.getT0FissionByReadAvg();
                 t0FissionByReadAvgCorrelationSum += article.getT0FissionByReadAvg() * correlation;
             }
-            fissionSum += sumFission0;
+            if (Objects.nonNull(article.getAvgOpenRate()) && article.getAvgOpenRate() > 0) {
+                openRateAvgRate += (sumFirstLevel * 1.0 / article.getViewCount()) / article.getAvgOpenRate();
+            }
             int hour = DateUtils.getHourByTimestamp(article.getPublishTimestamp());
             if (hour >= 12) {
                 fissionWeightSum += sumFission0 / morningNoonFissionRate;
@@ -753,6 +767,10 @@ public class RecallService implements ApplicationContextAware {
                 content.setT0FissionHeadDeWeightByReadAvgSumAvg(fissionHeadWeightSum / (avgReadCountSum + 500));
                 content.setT0FissionRecommendDeWeightByReadAvgSumAvg(fissionRecommendWeightSum / (avgReadCountSum + 500));
             }
+            if (firstLevelSum > 0) {
+                double sumFissionRate = fissionSum * 1.0 / firstLevelSum;
+                content.setT0FissionOpenRateSumAvg(sumFissionRate * (openRateAvgRate / firstLevelSize) * Math.log10(firstLevelSum) / 5);
+            }
         }
     }
 

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

@@ -129,7 +129,8 @@ public class ScoreService implements ApplicationContextAware {
                 || StringUtils.equals(param.getStrategy(), RankStrategyEnum.ArticleRankV14.getStrategy())
                 || StringUtils.equals(param.getStrategy(), RankStrategyEnum.ArticleRankV15.getStrategy())
                 || StringUtils.equals(param.getStrategy(), RankStrategyEnum.ArticleRankV16.getStrategy())
-                || StringUtils.equals(param.getStrategy(), RankStrategyEnum.ArticleRankV17.getStrategy())) {
+                || StringUtils.equals(param.getStrategy(), RankStrategyEnum.ArticleRankV17.getStrategy())
+                || StringUtils.equals(param.getStrategy(), RankStrategyEnum.ArticleRankV18.getStrategy())) {
             strategies.add(strategyMap.get(ScoreStrategyEnum.CATEGORY.value()));
             strategies.add(strategyMap.get(ScoreStrategyEnum.ACCOUNT_PRE_DISTRIBUTE.value()));
             strategies.add(strategyMap.get(ScoreStrategyEnum.FLOW_CTL_DECREASE.value()));
@@ -143,6 +144,7 @@ public class ScoreService implements ApplicationContextAware {
             strategies.add(strategyMap.get(ScoreStrategyEnum.HIS_FISSION_AVG_READ_RATE_CORRELATION_RATE.value()));
             strategies.add(strategyMap.get(ScoreStrategyEnum.HIS_FISSION_AVG_READ_SUM_RATE.value()));
             strategies.add(strategyMap.get(ScoreStrategyEnum.HIS_FISSION_DE_WEIGHT_AVG_READ_SUM_RATE.value()));
+            strategies.add(strategyMap.get(ScoreStrategyEnum.HIS_FISSION_OPEN_RATE.value()));
         }
 
         return strategies;

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

@@ -0,0 +1,38 @@
+package com.tzld.longarticle.recommend.server.service.recommend.score.strategy;
+
+import com.tzld.longarticle.recommend.server.model.dto.Content;
+import com.tzld.longarticle.recommend.server.service.recommend.config.AccountIndexAvgViewCountService;
+import com.tzld.longarticle.recommend.server.service.recommend.score.Score;
+import com.tzld.longarticle.recommend.server.service.recommend.score.ScoreParam;
+import com.tzld.longarticle.recommend.server.service.recommend.score.ScoreStrategy;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.collections4.CollectionUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import java.util.ArrayList;
+import java.util.List;
+
+@Component
+@Slf4j
+public class HisFissionOpenRateStrategy implements ScoreStrategy {
+
+    @Autowired
+    AccountIndexAvgViewCountService accountIndexAvgViewCountService;
+
+    @Override
+    public List<Score> score(ScoreParam param) {
+        List<Score> scores = new ArrayList<>();
+        for (Content content : param.getContents()) {
+            if (CollectionUtils.isEmpty(content.getHisPublishArticleList())) {
+                continue;
+            }
+            Score score = new Score();
+            score.setStrategy(this);
+            score.setContentId(content.getId());
+            score.setScore(content.getT0FissionOpenRateSumAvg());
+            scores.add(score);
+        }
+        return scores;
+    }
+}