wangyunpeng 9 часов назад
Родитель
Сommit
b709bb4de7

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

@@ -3,6 +3,7 @@ package com.tzld.longarticle.recommend.server.common.enums.recommend;
 public enum ScoreStrategyEnum {
     ACCOUNT_PRE_DISTRIBUTE("AccountPreDistributeStrategy"),
     CATEGORY("CategoryStrategy"),
+    ACCOUNT_USER_CATEGORY("AccountUserCategoryStrategy"),
     CONTENT_POOL("ContentPoolStrategy"),
     FLOW_CTL_DECREASE("FlowCtlDecreaseStrategy"),
     HIS_FISSION_AVG_READ_RATE_CORRELATION_RATE("HisFissionAvgReadRateCorrelationRateStrategy"),

+ 55 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/model/entity/longArticle/AccountUserCategory.java

@@ -0,0 +1,55 @@
+package com.tzld.longarticle.recommend.server.model.entity.longArticle;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.IdClass;
+import javax.persistence.Table;
+import java.io.Serializable;
+
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+@Entity
+@Table(name = "account_user_category")
+@IdClass(AccountUserCategory.PK.class)
+public class AccountUserCategory {
+
+    @Id
+    private String dt;
+    @Id
+    private String ghId;
+
+    @Column(name = "category_map")
+    private String categoryMap;
+
+    @Column(name = "status")
+    private Integer status;
+
+    @Column(name = "version")
+    private Integer version;
+
+    @Column(name = "create_timestamp")
+    private Long createTimestamp;
+
+    @Column(name = "update_timestamp")
+    private Long updateTimestamp;
+
+
+    @Data
+    public static class PK implements Serializable {
+
+        @Column(name = "dt")
+        private String dt;
+        @Column(name = "gh_id")
+        private String ghId;
+
+        public PK() {
+        }
+
+    }
+}

+ 16 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/repository/longArticle/AccountUserCategoryRepository.java

@@ -0,0 +1,16 @@
+package com.tzld.longarticle.recommend.server.repository.longArticle;
+
+import com.tzld.longarticle.recommend.server.model.entity.longArticle.AccountUserCategory;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.stereotype.Repository;
+
+import java.util.List;
+
+@Repository
+public interface AccountUserCategoryRepository extends JpaRepository<AccountUserCategory, AccountUserCategory.PK> {
+
+    List<AccountUserCategory> getByGhIdAndStatusAndVersion(String ghId, Integer status, Integer version);
+
+    List<AccountUserCategory> getByStatusAndVersion(Integer status, Integer version);
+
+}

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

@@ -68,6 +68,9 @@ public class RankV18Strategy implements RankStrategy {
                         + item.getScore(ScoreStrategyEnum.CATEGORY.value())
                         * weightService.getWeight(param.getStrategy(), param.getGhId(), index,
                         ScoreStrategyEnum.CATEGORY.value())
+                        + item.getScore(ScoreStrategyEnum.ACCOUNT_USER_CATEGORY.value())
+                        * weightService.getWeight(param.getStrategy(), param.getGhId(), index,
+                        ScoreStrategyEnum.ACCOUNT_USER_CATEGORY.value())
                         + item.getScore(ScoreStrategyEnum.HIS_FISSION_OPEN_RATE.value())
                         * weightService.getWeight(param.getStrategy(), param.getGhId(), index,
                         ScoreStrategyEnum.HIS_FISSION_OPEN_RATE.value())
@@ -87,6 +90,9 @@ public class RankV18Strategy implements RankStrategy {
                         + item.getScore(ScoreStrategyEnum.CATEGORY.value())
                         * weightService.getWeight(param.getStrategy(), param.getGhId(), index,
                         ScoreStrategyEnum.CATEGORY.value())
+                        + item.getScore(ScoreStrategyEnum.ACCOUNT_USER_CATEGORY.value())
+                        * weightService.getWeight(param.getStrategy(), param.getGhId(), index,
+                        ScoreStrategyEnum.ACCOUNT_USER_CATEGORY.value())
                         + item.getScore(ScoreStrategyEnum.ACCOUNT_PRE_DISTRIBUTE.value())
                         + item.getScore(ScoreStrategyEnum.PUBLISH_TIMES.value())
                         + item.getScore(ScoreStrategyEnum.CRAWLER_DAYS_DECREASE_STRATEGY.value())

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

@@ -70,6 +70,9 @@ public class RankV19Strategy implements RankStrategy {
                         + item.getScore(ScoreStrategyEnum.CATEGORY.value())
                         * weightService.getWeight(param.getStrategy(), param.getGhId(), index,
                         ScoreStrategyEnum.CATEGORY.value())
+                        + item.getScore(ScoreStrategyEnum.ACCOUNT_USER_CATEGORY.value())
+                        * weightService.getWeight(param.getStrategy(), param.getGhId(), index,
+                        ScoreStrategyEnum.ACCOUNT_USER_CATEGORY.value())
                         + item.getScore(ScoreStrategyEnum.HIS_FISSION_OPEN_RATE.value())
                         * weightService.getWeight(param.getStrategy(), param.getGhId(), index,
                         ScoreStrategyEnum.HIS_FISSION_OPEN_RATE.value())
@@ -93,6 +96,9 @@ public class RankV19Strategy implements RankStrategy {
                         + item.getScore(ScoreStrategyEnum.CATEGORY.value())
                         * weightService.getWeight(param.getStrategy(), param.getGhId(), index,
                         ScoreStrategyEnum.CATEGORY.value())
+                        + item.getScore(ScoreStrategyEnum.ACCOUNT_USER_CATEGORY.value())
+                        * weightService.getWeight(param.getStrategy(), param.getGhId(), index,
+                        ScoreStrategyEnum.ACCOUNT_USER_CATEGORY.value())
                         + item.getScore(ScoreStrategyEnum.ACCOUNT_PRE_DISTRIBUTE.value())
                         + item.getScore(ScoreStrategyEnum.PUBLISH_TIMES.value())
                         + item.getScore(ScoreStrategyEnum.CRAWLER_DAYS_DECREASE_STRATEGY.value())

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

@@ -75,6 +75,9 @@ public class RankV20Strategy implements RankStrategy {
                             + item.getScore(ScoreStrategyEnum.CATEGORY.value())
                             * weightService.getWeight(param.getStrategy(), param.getGhId(), index,
                             ScoreStrategyEnum.CATEGORY.value())
+                            + item.getScore(ScoreStrategyEnum.ACCOUNT_USER_CATEGORY.value())
+                            * weightService.getWeight(param.getStrategy(), param.getGhId(), index,
+                            ScoreStrategyEnum.ACCOUNT_USER_CATEGORY.value())
                             + item.getScore(ScoreStrategyEnum.HIS_FISSION_OPEN_RATE.value())
                             * weightService.getWeight(param.getStrategy(), param.getGhId(), index,
                             ScoreStrategyEnum.HIS_FISSION_OPEN_RATE.value())
@@ -99,6 +102,9 @@ public class RankV20Strategy implements RankStrategy {
                         + item.getScore(ScoreStrategyEnum.CATEGORY.value())
                         * weightService.getWeight(param.getStrategy(), param.getGhId(), index,
                         ScoreStrategyEnum.CATEGORY.value())
+                        + item.getScore(ScoreStrategyEnum.ACCOUNT_USER_CATEGORY.value())
+                        * weightService.getWeight(param.getStrategy(), param.getGhId(), index,
+                        ScoreStrategyEnum.ACCOUNT_USER_CATEGORY.value())
                         + item.getScore(ScoreStrategyEnum.HIS_FISSION_OPEN_RATE.value())
                         * weightService.getWeight(param.getStrategy(), param.getGhId(), index,
                         ScoreStrategyEnum.HIS_FISSION_OPEN_RATE.value())
@@ -122,6 +128,9 @@ public class RankV20Strategy implements RankStrategy {
                         + item.getScore(ScoreStrategyEnum.CATEGORY.value())
                         * weightService.getWeight(param.getStrategy(), param.getGhId(), index,
                         ScoreStrategyEnum.CATEGORY.value())
+                        + item.getScore(ScoreStrategyEnum.ACCOUNT_USER_CATEGORY.value())
+                        * weightService.getWeight(param.getStrategy(), param.getGhId(), index,
+                        ScoreStrategyEnum.ACCOUNT_USER_CATEGORY.value())
                         + item.getScore(ScoreStrategyEnum.ACCOUNT_PRE_DISTRIBUTE.value())
                         + item.getScore(ScoreStrategyEnum.PUBLISH_TIMES.value())
                         + item.getScore(ScoreStrategyEnum.CRAWLER_DAYS_DECREASE_STRATEGY.value())

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

@@ -69,6 +69,9 @@ public class RankV21Strategy implements RankStrategy {
                         + item.getScore(ScoreStrategyEnum.CATEGORY.value())
                         * weightService.getWeight(param.getStrategy(), param.getGhId(), index,
                         ScoreStrategyEnum.CATEGORY.value())
+                        + item.getScore(ScoreStrategyEnum.ACCOUNT_USER_CATEGORY.value())
+                        * weightService.getWeight(param.getStrategy(), param.getGhId(), index,
+                        ScoreStrategyEnum.ACCOUNT_USER_CATEGORY.value())
                         + item.getScore(ScoreStrategyEnum.HIS_FISSION_OPEN_RATE.value())
                         * weightService.getWeight(param.getStrategy(), param.getGhId(), index,
                         ScoreStrategyEnum.HIS_FISSION_OPEN_RATE.value())
@@ -92,6 +95,9 @@ public class RankV21Strategy implements RankStrategy {
                         + item.getScore(ScoreStrategyEnum.CATEGORY.value())
                         * weightService.getWeight(param.getStrategy(), param.getGhId(), index,
                         ScoreStrategyEnum.CATEGORY.value())
+                        + item.getScore(ScoreStrategyEnum.ACCOUNT_USER_CATEGORY.value())
+                        * weightService.getWeight(param.getStrategy(), param.getGhId(), index,
+                        ScoreStrategyEnum.ACCOUNT_USER_CATEGORY.value())
                         + item.getScore(ScoreStrategyEnum.HIS_FISSION_OPEN_RATE.value())
                         * weightService.getWeight(param.getStrategy(), param.getGhId(), index,
                         ScoreStrategyEnum.HIS_FISSION_OPEN_RATE.value())
@@ -115,6 +121,9 @@ public class RankV21Strategy implements RankStrategy {
                         + item.getScore(ScoreStrategyEnum.CATEGORY.value())
                         * weightService.getWeight(param.getStrategy(), param.getGhId(), index,
                         ScoreStrategyEnum.CATEGORY.value())
+                        + item.getScore(ScoreStrategyEnum.ACCOUNT_USER_CATEGORY.value())
+                        * weightService.getWeight(param.getStrategy(), param.getGhId(), index,
+                        ScoreStrategyEnum.ACCOUNT_USER_CATEGORY.value())
                         + item.getScore(ScoreStrategyEnum.ACCOUNT_PRE_DISTRIBUTE.value())
                         + item.getScore(ScoreStrategyEnum.PUBLISH_TIMES.value())
                         + item.getScore(ScoreStrategyEnum.CRAWLER_DAYS_DECREASE_STRATEGY.value())

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

@@ -109,6 +109,7 @@ public class ScoreService implements ApplicationContextAware {
         if (StringUtils.equals(param.getStrategy(), RankStrategyEnum.LATE_STRATEGY.getStrategy())
                 || StringUtils.equals(param.getStrategy(), RankStrategyEnum.INFINITE_STRATEGY.getStrategy())) {
             strategies.add(strategyMap.get(ScoreStrategyEnum.CATEGORY.value()));
+            strategies.add(strategyMap.get(ScoreStrategyEnum.ACCOUNT_USER_CATEGORY.value()));
             strategies.add(strategyMap.get(ScoreStrategyEnum.ACCOUNT_PRE_DISTRIBUTE.value()));
             strategies.add(strategyMap.get(ScoreStrategyEnum.PUBLISH_TIMES.value()));
             strategies.add(strategyMap.get(ScoreStrategyEnum.FLOW_CTL_DECREASE.value()));
@@ -135,6 +136,7 @@ public class ScoreService implements ApplicationContextAware {
                 || StringUtils.equals(param.getStrategy(), RankStrategyEnum.ArticleRankV20.getStrategy())
                 || StringUtils.equals(param.getStrategy(), RankStrategyEnum.ArticleRankV21.getStrategy())) {
             strategies.add(strategyMap.get(ScoreStrategyEnum.CATEGORY.value()));
+            strategies.add(strategyMap.get(ScoreStrategyEnum.ACCOUNT_USER_CATEGORY.value()));
             //strategies.add(strategyMap.get(ScoreStrategyEnum.ACCOUNT_PRE_DISTRIBUTE.value()));
             strategies.add(strategyMap.get(ScoreStrategyEnum.FLOW_CTL_DECREASE.value()));
             strategies.add(strategyMap.get(ScoreStrategyEnum.VIEW_COUNT_RATE.value()));

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

@@ -0,0 +1,81 @@
+package com.tzld.longarticle.recommend.server.service.recommend.score.strategy;
+
+import com.alibaba.fastjson.JSONObject;
+import com.tzld.longarticle.recommend.server.common.enums.StatusEnum;
+import com.tzld.longarticle.recommend.server.model.dto.Content;
+import com.tzld.longarticle.recommend.server.model.entity.longArticle.AccountUserCategory;
+import com.tzld.longarticle.recommend.server.repository.longArticle.AccountUserCategoryRepository;
+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.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+import org.springframework.util.StringUtils;
+
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * 用户分类评分策略
+ * 基于 account_user_category 表中的用户分类偏好数据进行评分
+ */
+@Component
+@Slf4j
+public class AccountUserCategoryStrategy implements ScoreStrategy {
+
+    @Autowired
+    private AccountUserCategoryRepository accountUserCategoryRepository;
+
+    @Value("${account.user.category.active.version:1}")
+    private Integer activeVersion;
+
+    @Override
+    public List<Score> score(ScoreParam param) {
+        long start = System.currentTimeMillis();
+        List<Score> scores = new ArrayList<>();
+        if (CollectionUtils.isEmpty(param.getContents())) {
+            return scores;
+        }
+
+        // 获取账号的用户分类配置
+        List<AccountUserCategory> accountUserCategoryList = accountUserCategoryRepository.getByGhIdAndStatusAndVersion(
+                param.getGhId(), StatusEnum.ONE.getCode(), activeVersion);
+        if (CollectionUtils.isEmpty(accountUserCategoryList)) {
+            return scores;
+        }
+        AccountUserCategory accountUserCategory = null;
+        if (CollectionUtils.isNotEmpty(accountUserCategoryList)) {
+            accountUserCategory = accountUserCategoryList.stream()
+                    .sorted(Comparator.comparing(AccountUserCategory::getDt, Comparator.reverseOrder()))
+                    .findFirst().orElse(null);
+        }
+
+        for (Content content : param.getContents()) {
+            if (CollectionUtils.isEmpty(content.getCategory())) {
+                continue;
+            }
+            Score score = new Score();
+            score.setStrategy(this);
+            score.setContentId(content.getId());
+            double scoreValue = 0.0;
+
+            for (String category : content.getCategory()) {
+                if (Objects.nonNull(accountUserCategory) && StringUtils.hasText(accountUserCategory.getCategoryMap())) {
+                    JSONObject categoryWeightMap = JSONObject.parseObject(accountUserCategory.getCategoryMap());
+                    double categoryWeight = categoryWeightMap.getDoubleValue(category);
+                    scoreValue += categoryWeight;
+                }
+            }
+            score.setScore(scoreValue);
+            scores.add(score);
+        }
+        log.info("AccountUserCategoryStrategy cost:{}", System.currentTimeMillis() - start);
+        return scores;
+    }
+
+}