丁云鹏 11 ماه پیش
والد
کامیت
0b987d4165

+ 9 - 5
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/filter/strategy/AccountPreDistributeStrategy.java

@@ -1,6 +1,6 @@
 package com.tzld.longarticle.recommend.server.service.filter.strategy;
 
-import com.google.common.collect.Sets;
+import com.google.common.reflect.TypeToken;
 import com.tzld.longarticle.recommend.server.model.Content;
 import com.tzld.longarticle.recommend.server.repository.ArticlePreDistributeAccount;
 import com.tzld.longarticle.recommend.server.repository.ArticlePreDistributeAccountRepository;
@@ -8,14 +8,13 @@ import com.tzld.longarticle.recommend.server.service.AccountContentPoolConfigSer
 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.util.JSONUtils;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Set;
+import java.util.*;
 
 /**
  * @author dyp
@@ -39,7 +38,12 @@ public class AccountPreDistributeStrategy implements FilterStrategy {
             ArticlePreDistributeAccount articlePreDistributeAccount =
                     articlePreDistributeAccountRepository.findByGhIdOrderByDate(param.getAccountId());
             if (StringUtils.isNotEmpty(articlePreDistributeAccount.getArticleList())) {
-                Set<String> articles = Sets.newHashSet(StringUtils.split(","));
+                List<String[]> list = JSONUtils.fromJson(articlePreDistributeAccount.getArticleList(), new TypeToken<List<String[]>>() {
+                }, Collections.emptyList());
+                Set<String> articles = new HashSet<>();
+                for (String[] s : list) {
+                    articles.add(s[0]);
+                }
                 for (Content content : contents) {
                     if (StringUtils.equals(pools[2], content.getContentPoolType())) {
                         if (articles.contains(content.getId())) {

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

@@ -2,6 +2,7 @@ package com.tzld.longarticle.recommend.server.service.rank.strategy;
 
 
 import com.tzld.longarticle.recommend.server.model.Content;
+import com.tzld.longarticle.recommend.server.service.AccountContentPoolConfigService;
 import com.tzld.longarticle.recommend.server.service.rank.RankItem;
 import com.tzld.longarticle.recommend.server.service.rank.RankParam;
 import com.tzld.longarticle.recommend.server.service.rank.RankResult;
@@ -13,15 +14,14 @@ import com.tzld.longarticle.recommend.server.service.score.strategy.SimilaritySt
 import com.tzld.longarticle.recommend.server.service.score.strategy.ViewCountStrategy;
 import com.tzld.longarticle.recommend.server.util.CommonCollectionUtils;
 import com.tzld.longarticle.recommend.server.util.JSONUtils;
+import com.tzld.longarticle.recommend.server.util.TitleSimilarCheckUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 /**
  * @author dyp
@@ -32,6 +32,8 @@ public class RankV3Strategy implements RankStrategy {
 
     @Autowired
     private ScoreService scoreService;
+    @Autowired
+    private AccountContentPoolConfigService accountContentPoolConfigService;
 
     public RankResult rank(RankParam param) {
 
@@ -62,14 +64,45 @@ public class RankV3Strategy implements RankStrategy {
                     o1.getScore(ViewCountStrategy.class.getSimpleName()),
                     o2.getScore(ViewCountStrategy.class.getSimpleName()));
         });
-        // 2 选文章
+        // 2 相似去重
+        List<Content> contents = CommonCollectionUtils.toList(items, RankItem::getContent);
+        contents = deduplication(contents);
+        log.info("Deduplication {}", JSONUtils.toJson(contents));
+
+        // 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 选文章
         List<Content> result = new ArrayList<>();
-        if (CollectionUtils.isNotEmpty(items)) {
-            for (int i = 0; i < Math.min(items.size(), param.getSize()); i++) {
-                result.add(items.get(i).getContent());
+        String[] contentPools = accountContentPoolConfigService.getContentPools(param.getAccountName());
+        // 头
+        List<Content> pool = contentMap.get(contentPools[0]);
+        if (CollectionUtils.isNotEmpty(pool)) {
+            result.add(pool.get(0));
+        }
+        // 次
+        pool = contentMap.get(contentPools[1]);
+        if (CollectionUtils.isNotEmpty(pool)) {
+            if (StringUtils.equals(contentPools[0], contentPools[1])) {
+                if (pool.size() > 2) {
+                    result.add(pool.get(2));
+                }
+            } else {
+                result.add(pool.get(0));
             }
         }
 
+
+        // 3-8
+        pool = contentMap.get(contentPools[2]);
+        if (CollectionUtils.isNotEmpty(pool)) {
+            result.addAll(pool.subList(0, Math.min(pool.size(), param.getSize() - result.size())));
+        }
+
         return new RankResult(result);
     }
 
@@ -80,4 +113,24 @@ public class RankV3Strategy implements RankStrategy {
         return scoreParam;
     }
 
+    private List<Content> deduplication(List<Content> contents) {
+        List<String> titles = new ArrayList<>();
+        List<Content> result = new ArrayList<>();
+        // 遍历所有列表
+        for (Content c : contents) {
+            if (similarity(c.getTitle(), titles)) {
+                continue;
+            } else {
+                result.add(c);
+                titles.add(c.getTitle());
+            }
+        }
+
+        return result;
+    }
+
+    private boolean similarity(String title, List<String> titles) {
+        return TitleSimilarCheckUtil.isDuplicateContent(title, titles);
+    }
+
 }

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

@@ -21,6 +21,7 @@ public class JSONUtils {
             return "";
         }
     }
+
     public static String toJson(Object obj) {
         if (obj == null) {
             return "";