1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package com.tanpu.community.service;
import com.tanpu.common.util.JsonUtil;
import com.tanpu.community.api.beans.qo.ThemeAnalysDO;
import com.tanpu.community.api.beans.resp.PythonResponse;
import com.tanpu.community.dao.entity.community.ThemeEntity;
import com.tanpu.community.util.BizUtils;
import com.tanpu.community.util.ConvertUtil;
import com.tanpu.community.util.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
@Slf4j
@Service
public class RecommendService {
@Value("${recommend.python.enable}")
public String enablePython;
@Value("${recommend.python.url}")
public String pythonUrl;
@Value("${recommend.ratio.hot}")
public Integer hotRatio;
@Value("${recommend.ratio.new}")
public Integer newRatio;
@Value("${recommend.ratio.python}")
public Integer pythonRatio;
@Autowired
private RankService rankService;
@Autowired
private ThemeService themeService;
@Resource
private RestTemplate restTemplate;
// 最新
private List<ThemeAnalysDO> recentThemeList = new ArrayList<>();
// 推荐
private Map<String, List<String>> recommondList = new HashMap<>();
public List<String> getRecommendThemes(Integer pageStart, Integer pageSize, String userId, List<String> excludeIds, LocalDateTime timeAfter) {
// 最热话题,筛掉用户发表的 & 最近看过的 & excludeIds
List<String> hotThemeIds = rankService.getHotestThemes().stream()
.filter(theme -> {
// 暂时不过滤用户自己发的 !userId.equals(theme.getAuthorId());
return !excludeIds.contains(theme.getThemeId());
})
.map(ThemeAnalysDO::getThemeId).collect(Collectors.toList());
hotThemeIds = BizUtils.subList(hotThemeIds, 0, pageSize);
//最新话题,筛掉用户发表的 & 最近看过的 & excludeIds && 上次查询后发帖的
long margin = TimeUtils.calMinuteTillNow(timeAfter);
List<String> newThemeIds = getNewestThemes().stream()
.filter(theme -> {
// 暂时不过滤用户自己发的 !userId.equals(theme.getAuthorId());
return !excludeIds.contains(theme.getThemeId()) && theme.getMinutesTillNow() > margin;
})
.map(ThemeAnalysDO::getThemeId).collect(Collectors.toList());
newThemeIds = BizUtils.subList(newThemeIds, 0, pageSize);
//推荐话题
List<String> recThemeIds = getPythonRecommendList(userId).stream()
.filter(id -> {
return !excludeIds.contains(id);
}).collect(Collectors.toList());
recThemeIds = BizUtils.subList(recThemeIds, pageStart, pageSize);
// merge
return mergeRecommend(hotThemeIds, newThemeIds, recThemeIds);
}
// 获取最新话题
public List<ThemeAnalysDO> getNewestThemes() {
if (recentThemeList.size() == 0) {
refreshNewestThemes();
}
return recentThemeList;
}
// 从数据库查询最新主题
public void refreshNewestThemes() {
List<ThemeEntity> themeEntities = themeService.queryLatestThemes(100);
this.recentThemeList = ConvertUtil.themeEntityToAnalysDOs(themeEntities);
}
//查询python计算推荐列表
public List<String> getPythonRecommendList(String userId) {
if (recommondList.containsKey(userId)) {
return recommondList.get(userId);
} else {
return refreshPythonRecommendList(userId);
}
}
//HTTP查询python推荐主题 python返回最多50个
public List<String> refreshPythonRecommendList(String userId) {
if (!"true".equals(enablePython)) {
return Collections.emptyList();
}
HashMap<String, String> param = new HashMap<>();
param.put("user_id", userId);
try {
String response = restTemplate.getForObject(pythonUrl, String.class, param);
PythonResponse pythonResponse = JsonUtil.toBean(response, PythonResponse.class);
recommondList.put(userId, pythonResponse.getAttributes());
return pythonResponse.getAttributes();
} catch (Exception e) {
log.error("调用python失败");
return Collections.emptyList();
}
}
// 合并,去重
private List<String> mergeList(List<String> hotThemeIds, List<String> newThemeIds, List<String> recThemeIds, Set<String> set) {
ArrayList<String> result = new ArrayList<>();
// 3个集合的指针
Integer hotIdx = 0;
Integer newIdx = 0;
Integer recIdx = 0;
while (hotThemeIds.size() > hotIdx || newThemeIds.size() > newIdx || recThemeIds.size() > recIdx) {
int hotTimes = hotRatio;
int newTimes = newRatio;
int recTimes = pythonRatio;
String id;
while (newTimes > 0 && newThemeIds.size() > newIdx) {
id = newThemeIds.get(newIdx);
if (!set.contains(id)) {
result.add(id);
set.add(id);
}
newIdx++;
newTimes--;
}
while (hotTimes > 0 && hotThemeIds.size() > hotIdx) {
id = hotThemeIds.get(hotIdx);
if (!set.contains(id)) {
result.add(id);
set.add(id);
}
hotIdx++;
hotTimes--;
}
while (recTimes > 0 && recThemeIds.size() > recIdx) {
id = recThemeIds.get(recIdx);
if (!set.contains(id)) {
result.add(id);
set.add(id);
}
recIdx++;
recTimes--;
}
}
return result;
}
// 按照 6,3,1的比例
private List<String> mergeRecommend(List<String> hotIds, List<String> newestIds, List<String> recmdIds) {
List<String> retList = new ArrayList<>();
int round = 0;
while (true) {
int newestStart = round * newRatio;
int hotStart = round * hotRatio;
int recmdStart = round * pythonRatio;
if (hotStart >= hotIds.size() && newestStart >= newestIds.size() && recmdStart >= recmdIds.size()) {
break;
}
retList.addAll(BizUtils.subList(newestIds, newestStart, newRatio));
retList.addAll(BizUtils.subList(hotIds, hotStart, hotRatio));
retList.addAll(BizUtils.subList(recmdIds, recmdStart, pythonRatio));
round++;
}
return retList;
}
}