Commit 58b6eaec authored by 刘基明's avatar 刘基明

热度算法优化

parent 77bdd06b
...@@ -49,25 +49,7 @@ public class ThemeAnalysDO { ...@@ -49,25 +49,7 @@ public class ThemeAnalysDO {
@ApiModelProperty(value = "用户质量") @ApiModelProperty(value = "用户质量")
private Double userWeight = 0.0; private Double userWeight = 0.0;
public Double getRank() { private Double score=0.0;
double viewRatio = 0.1;
double forwardRatio = 3;
double commentRatio = 2;
double likeRation = 1;
double collectRatio = 3;
double userWeightRatio = 0.8;
double initialWeight = 1.0;
double timeRation = 0.2;
// 质量=帖子质量+用户质量
double w = viewCount * viewRatio + forwardCount * forwardRatio + commentCount * commentRatio
+ likeCount * likeRation + collectCount * collectRatio
+ Math.pow(userWeight, userWeightRatio);
double i = initialWeight; // 初始权重
double t = Double.valueOf(minuteTillNow) / 60;
double g = timeRation; // 时间系数
return (w + i) / Math.pow(t + 1, g);
}
} }
...@@ -40,19 +40,5 @@ public class TopicRankQo { ...@@ -40,19 +40,5 @@ public class TopicRankQo {
private Integer hoursTillNow; private Integer hoursTillNow;
private Double score;
/**
* 热度计算算法
*
* @return
*/
public Double getRank() {
double g = 0.3;//时间系数
//顶置话题
if (isTop > 0) {
return Double.MAX_VALUE;
}
Double socre = ((disscussCount * 3 + viewCount) / Math.pow(hoursTillNow + 1, g)) + themeWeight;
return socre;
}
} }
...@@ -116,7 +116,7 @@ public class CollectionService { ...@@ -116,7 +116,7 @@ public class CollectionService {
if (StringUtils.isNotEmpty(lastId)) { if (StringUtils.isNotEmpty(lastId)) {
CollectionEntity target = queryIncludeDelete(lastId, userId, type); CollectionEntity target = queryIncludeDelete(lastId, userId, type);
if (target == null) return Collections.emptyList(); if (target == null) return Collections.emptyList();
queryWrapper.lt(CollectionEntity::getCollectionType, target.getCollectionTime()); queryWrapper.lt(CollectionEntity::getCollectionTime, target.getCollectionTime());
} }
if (pageSize != null) { if (pageSize != null) {
queryWrapper.last("limit " + pageSize); queryWrapper.last("limit " + pageSize);
......
...@@ -31,23 +31,34 @@ import static com.tanpu.community.api.constants.RedisKeyConstant.CACHE_FEIGN_USE ...@@ -31,23 +31,34 @@ import static com.tanpu.community.api.constants.RedisKeyConstant.CACHE_FEIGN_USE
@Service @Service
public class RankService { public class RankService {
@Value("${rank.theme.viewRatio:0.1}") @Value("${rank.theme.viewRate:0.1}")
public Double viewRatio; public Double viewRate;
@Value("${rank.theme.forwardRatio:3}") @Value("${rank.theme.forwardRate:3}")
public Double forwardRatio; public Double forwardRate;
@Value("${rank.theme.commentRatio:2}") @Value("${rank.theme.commentRate:2}")
public Double commentRatio; public Double commentRate;
@Value("${rank.theme.likeRation:3}") @Value("${rank.theme.likeRaten:3}")
public Double likeRation; public Double likeRaten;
@Value("${rank.theme.collectRatio:3}") @Value("${rank.theme.collectRate:3}")
public Double collectRatio; public Double collectRate;
//用户质量权重
@Value("${rank.theme.userWeightRatio:0.9}") @Value("${rank.theme.userWeightRate:0.9}")
public Double userWeightRatio; public Double userWeightRate;
//初始质量
@Value("${rank.theme.initialWeight:1.0}") @Value("${rank.theme.initialWeight:1.0}")
public Double initialWeight; public Double initialWeight;
@Value("${rank.theme.timeRation:0.2}") //时间系数
public Double timeRation; @Value("${rank.theme.timeRaten:0.2}")
public Double timeRate;
@Value("${rank.topic.viewRate:1}")
public Double viewRateTopic;
@Value("${rank.topic.discussRate:3}")
public Double discussRateTopic;
//时间系数
@Value("${rank.theme.timeRaten:1}")
public Double timeRateTopic;
@Autowired @Autowired
private ThemeService themeService; private ThemeService themeService;
...@@ -114,9 +125,11 @@ public class RankService { ...@@ -114,9 +125,11 @@ public class RankService {
theme.setUserWeight(authorInfo.getLevelGrade() * 1.0); theme.setUserWeight(authorInfo.getLevelGrade() * 1.0);
} }
//打分
this.calculateThemeScore(theme);
} }
//打分 //排序
Map<ThemeAnalysDO, Double> map = themeAnalysDOS.stream().collect(Collectors.toMap(o -> o, ThemeAnalysDO::getRank)); Map<ThemeAnalysDO, Double> map = themeAnalysDOS.stream().collect(Collectors.toMap(o -> o, ThemeAnalysDO::getScore));
//排序 //排序
hotestThemes = map.entrySet().stream() hotestThemes = map.entrySet().stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())) .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
...@@ -146,27 +159,27 @@ public class RankService { ...@@ -146,27 +159,27 @@ public class RankService {
topic.setViewCount(topicViewMap.getOrDefault(topic.getTopicId(), 0)); topic.setViewCount(topicViewMap.getOrDefault(topic.getTopicId(), 0));
topic.setDisscussCount(0); topic.setDisscussCount(0);
topic.setThemeWeight(0.0); topic.setThemeWeight(0.0);
topic.setFormatViewCount(BizUtils.formatCountNumber(topic.getViewCount())); } else {
topic.setFormatDisscussCount(BizUtils.formatCountNumber(topic.getDisscussCount())); // 浏览量
continue; Integer topicPV = topicViewMap.getOrDefault(topic.getTopicId(), 0);
Integer themePV = visitLogService.queryThemeVisit(themeIds);
topic.setViewCount(topicPV + themePV + topic.getViewCntAdjust());
//讨论数=发布主题贴数+回复总数
Integer commentCount = commentService.getTotalCountByThemeIds(themeIds);
topic.setDisscussCount(themeIds.size() + commentCount);
//帖子权重,求和
double themeSum = getHotestThemes().stream().filter(o -> topic.getTopicId().equals(o.getTopicId()))
.mapToDouble(ThemeAnalysDO::getScore)
.sum();
topic.setThemeWeight(themeSum);
} }
// 浏览量 calculateTopicScore(topic);
Integer topicPV = topicViewMap.getOrDefault(topic.getTopicId(), 0);
Integer themePV = visitLogService.queryThemeVisit(themeIds);
topic.setViewCount(topicPV + themePV + topic.getViewCntAdjust());
//讨论数=发布主题贴数+回复总数
Integer commentCount = commentService.getTotalCountByThemeIds(themeIds);
topic.setDisscussCount(themeIds.size() + commentCount);
//帖子权重,求和
double themeSum = getHotestThemes().stream().filter(o -> topic.getTopicId().equals(o.getTopicId()))
.mapToDouble(ThemeAnalysDO::getRank)
.sum();
topic.setThemeWeight(themeSum);
//格式化浏览量、讨论量 //格式化浏览量、讨论量
topic.setFormatViewCount(BizUtils.formatCountNumber(topic.getViewCount())); topic.setFormatViewCount(BizUtils.formatCountNumber(topic.getViewCount()));
topic.setFormatDisscussCount(BizUtils.formatCountNumber(topic.getDisscussCount())); topic.setFormatDisscussCount(BizUtils.formatCountNumber(topic.getDisscussCount()));
} }
Map<TopicRankQo, Double> map = topicRankQos.stream().collect(Collectors.toMap(o -> o, TopicRankQo::getRank)); Map<TopicRankQo, Double> map = topicRankQos.stream().collect(Collectors.toMap(o -> o, TopicRankQo::getScore));
List<TopicRankQo> rankList = map.entrySet().stream() List<TopicRankQo> rankList = map.entrySet().stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())) .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
.map(Map.Entry::getKey) .map(Map.Entry::getKey)
...@@ -234,4 +247,29 @@ public class RankService { ...@@ -234,4 +247,29 @@ public class RankService {
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
private void calculateThemeScore(ThemeAnalysDO theme) {
// 质量=帖子质量+用户质量
double w = theme.getViewCount() * viewRate
+ theme.getForwardCount() * forwardRate
+ theme.getCommentCount() * commentRate
+ theme.getLikeCount() * likeRaten
+ theme.getCollectCount() * collectRate
+ Math.pow(theme.getUserWeight(), userWeightRate);
double t = Double.valueOf(theme.getMinuteTillNow()) / 60;
double score = (w + initialWeight) / Math.pow(t + 1, timeRate);
theme.setScore(score);
}
public void calculateTopicScore(TopicRankQo topic) {
//顶置话题
if (topic.getIsTop() > 0) {
topic.setScore(Double.MAX_VALUE);
}
Double socre = ((topic.getDisscussCount() * discussRateTopic + topic.getViewCount() * viewRateTopic)
/ Math.pow(topic.getHoursTillNow() + 1, timeRateTopic))
+ topic.getThemeWeight();
topic.setScore(socre);
}
} }
...@@ -179,9 +179,9 @@ public class RecommendService { ...@@ -179,9 +179,9 @@ public class RecommendService {
if (hotStart >= hotIds.size() && newestStart >= newestIds.size() && recmdStart >= recmdIds.size()) { if (hotStart >= hotIds.size() && newestStart >= newestIds.size() && recmdStart >= recmdIds.size()) {
break; break;
} }
retList.addAll(BizUtils.subList(newestIds, newestStart, newestStart + 3)); retList.addAll(BizUtils.subList(newestIds, newestStart, newestStart + newRatio));
retList.addAll(BizUtils.subList(hotIds, hotStart, hotStart + 6)); retList.addAll(BizUtils.subList(hotIds, hotStart, hotStart + hotRatio));
retList.addAll(BizUtils.subList(recmdIds, recmdStart, recmdStart + 1)); retList.addAll(BizUtils.subList(recmdIds, recmdStart, recmdStart + pythonRatio));
round++; round++;
} }
......
...@@ -222,7 +222,7 @@ public class ThemeService { ...@@ -222,7 +222,7 @@ public class ThemeService {
* 查询更新节点后的用户新建主题数 * 查询更新节点后的用户新建主题数
* *
* @param userIds 用户ids * @param userIds 用户ids
* @param lastViewTime 更新时间节点 * @param lastId 更新时间节点
* @return * @return
*/ */
public Integer queryCountFromLastId(List<String> userIds, Long lastId) { public Integer queryCountFromLastId(List<String> userIds, Long lastId) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment