|
@@ -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);
|
|
|
+ }
|
|
|
+
|
|
|
}
|