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

热度算法优化

parent 77bdd06b
......@@ -49,25 +49,7 @@ public class ThemeAnalysDO {
@ApiModelProperty(value = "用户质量")
private Double userWeight = 0.0;
public Double getRank() {
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);
}
private Double score=0.0;
}
......@@ -40,19 +40,5 @@ public class TopicRankQo {
private Integer hoursTillNow;
/**
* 热度计算算法
*
* @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;
}
private Double score;
}
......@@ -116,7 +116,7 @@ public class CollectionService {
if (StringUtils.isNotEmpty(lastId)) {
CollectionEntity target = queryIncludeDelete(lastId, userId, type);
if (target == null) return Collections.emptyList();
queryWrapper.lt(CollectionEntity::getCollectionType, target.getCollectionTime());
queryWrapper.lt(CollectionEntity::getCollectionTime, target.getCollectionTime());
}
if (pageSize != null) {
queryWrapper.last("limit " + pageSize);
......
......@@ -31,23 +31,34 @@ import static com.tanpu.community.api.constants.RedisKeyConstant.CACHE_FEIGN_USE
@Service
public class RankService {
@Value("${rank.theme.viewRatio:0.1}")
public Double viewRatio;
@Value("${rank.theme.forwardRatio:3}")
public Double forwardRatio;
@Value("${rank.theme.commentRatio:2}")
public Double commentRatio;
@Value("${rank.theme.likeRation:3}")
public Double likeRation;
@Value("${rank.theme.collectRatio:3}")
public Double collectRatio;
@Value("${rank.theme.userWeightRatio:0.9}")
public Double userWeightRatio;
@Value("${rank.theme.viewRate:0.1}")
public Double viewRate;
@Value("${rank.theme.forwardRate:3}")
public Double forwardRate;
@Value("${rank.theme.commentRate:2}")
public Double commentRate;
@Value("${rank.theme.likeRaten:3}")
public Double likeRaten;
@Value("${rank.theme.collectRate:3}")
public Double collectRate;
//用户质量权重
@Value("${rank.theme.userWeightRate:0.9}")
public Double userWeightRate;
//初始质量
@Value("${rank.theme.initialWeight:1.0}")
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
private ThemeService themeService;
......@@ -114,9 +125,11 @@ public class RankService {
theme.setUserWeight(authorInfo.getLevelGrade() * 1.0);
}
}
//打分
Map<ThemeAnalysDO, Double> map = themeAnalysDOS.stream().collect(Collectors.toMap(o -> o, ThemeAnalysDO::getRank));
this.calculateThemeScore(theme);
}
//排序
Map<ThemeAnalysDO, Double> map = themeAnalysDOS.stream().collect(Collectors.toMap(o -> o, ThemeAnalysDO::getScore));
//排序
hotestThemes = map.entrySet().stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
......@@ -146,10 +159,7 @@ public class RankService {
topic.setViewCount(topicViewMap.getOrDefault(topic.getTopicId(), 0));
topic.setDisscussCount(0);
topic.setThemeWeight(0.0);
topic.setFormatViewCount(BizUtils.formatCountNumber(topic.getViewCount()));
topic.setFormatDisscussCount(BizUtils.formatCountNumber(topic.getDisscussCount()));
continue;
}
} else {
// 浏览量
Integer topicPV = topicViewMap.getOrDefault(topic.getTopicId(), 0);
Integer themePV = visitLogService.queryThemeVisit(themeIds);
......@@ -159,14 +169,17 @@ public class RankService {
topic.setDisscussCount(themeIds.size() + commentCount);
//帖子权重,求和
double themeSum = getHotestThemes().stream().filter(o -> topic.getTopicId().equals(o.getTopicId()))
.mapToDouble(ThemeAnalysDO::getRank)
.mapToDouble(ThemeAnalysDO::getScore)
.sum();
topic.setThemeWeight(themeSum);
}
calculateTopicScore(topic);
//格式化浏览量、讨论量
topic.setFormatViewCount(BizUtils.formatCountNumber(topic.getViewCount()));
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()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
.map(Map.Entry::getKey)
......@@ -234,4 +247,29 @@ public class RankService {
.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 {
if (hotStart >= hotIds.size() && newestStart >= newestIds.size() && recmdStart >= recmdIds.size()) {
break;
}
retList.addAll(BizUtils.subList(newestIds, newestStart, newestStart + 3));
retList.addAll(BizUtils.subList(hotIds, hotStart, hotStart + 6));
retList.addAll(BizUtils.subList(recmdIds, recmdStart, recmdStart + 1));
retList.addAll(BizUtils.subList(newestIds, newestStart, newestStart + newRatio));
retList.addAll(BizUtils.subList(hotIds, hotStart, hotStart + hotRatio));
retList.addAll(BizUtils.subList(recmdIds, recmdStart, recmdStart + pythonRatio));
round++;
}
......
......@@ -222,7 +222,7 @@ public class ThemeService {
* 查询更新节点后的用户新建主题数
*
* @param userIds 用户ids
* @param lastViewTime 更新时间节点
* @param lastId 更新时间节点
* @return
*/
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