|
@@ -1,19 +1,20 @@
|
|
|
package com.tzld.longarticle.recommend.server.service.rank;
|
|
|
|
|
|
|
|
|
-import com.google.common.collect.Lists;
|
|
|
-import com.tzld.longarticle.recommend.server.model.Content;
|
|
|
import com.tzld.longarticle.recommend.server.service.score.ScoreParam;
|
|
|
import com.tzld.longarticle.recommend.server.service.score.ScoreResult;
|
|
|
import com.tzld.longarticle.recommend.server.service.score.ScoreService;
|
|
|
import com.tzld.longarticle.recommend.server.service.score.ScoreStrategy;
|
|
|
import com.tzld.longarticle.recommend.server.service.score.strategy.ContentPoolStrategy;
|
|
|
import com.tzld.longarticle.recommend.server.service.score.strategy.SimilarityStrategy;
|
|
|
+import com.tzld.longarticle.recommend.server.service.score.strategy.ViewCountStrategy;
|
|
|
+import com.tzld.longarticle.recommend.server.util.CommonCollectionUtils;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.Collections;
|
|
|
+import java.util.Comparator;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
@@ -32,41 +33,20 @@ public class RankService {
|
|
|
ScoreResult result = scoreService.score(convertToScoreParam(param));
|
|
|
Map<String, Map<Class<? extends ScoreStrategy>, Double>> scoreMap = result.getScoreMap();
|
|
|
|
|
|
- List<Content> contents = Lists.newArrayList(param.getContents());
|
|
|
+ List<RankItem> items = CommonCollectionUtils.toList(param.getContents(), c -> {
|
|
|
+ Map<Class<? extends ScoreStrategy>, Double> map = scoreMap.get(c.getId());
|
|
|
+ double score = 1 * map.getOrDefault(SimilarityStrategy.class, 0.0)
|
|
|
+ + 10 * map.getOrDefault(ViewCountStrategy.class, 0.0)
|
|
|
+ + 100 * map.getOrDefault(ContentPoolStrategy.class, 0.0);
|
|
|
|
|
|
- Collections.sort(contents, (o1, o2) -> {
|
|
|
- String o1Id = o1.getId();
|
|
|
- String o2Id = o2.getId();
|
|
|
+ RankItem item = new RankItem();
|
|
|
+ item.setContent(c);
|
|
|
+ item.setScore(score);
|
|
|
+ return item;
|
|
|
|
|
|
- double o1SimilarityScore = scoreMap.get(o1Id) != null
|
|
|
- ? scoreMap.get(o1Id).get(SimilarityStrategy.class)
|
|
|
- : 0.0;
|
|
|
- double o2SimilarityScore = scoreMap.get(o2Id) != null
|
|
|
- ? scoreMap.get(o2Id).get(SimilarityStrategy.class)
|
|
|
- : 0.0;
|
|
|
- if (o1SimilarityScore < o2SimilarityScore) {
|
|
|
- return -1;
|
|
|
- }
|
|
|
- if (o1SimilarityScore > o2SimilarityScore) {
|
|
|
- return 1;
|
|
|
- }
|
|
|
-
|
|
|
- double o1ContentPoolScore = scoreMap.get(o1Id) != null
|
|
|
- ? scoreMap.get(o1Id).get(ContentPoolStrategy.class)
|
|
|
- : 0.0;
|
|
|
- double o2ContentPoolScore = scoreMap.get(o2Id) != null
|
|
|
- ? scoreMap.get(o2Id).get(ContentPoolStrategy.class)
|
|
|
- : 0.0;
|
|
|
- if (o1ContentPoolScore < o2ContentPoolScore) {
|
|
|
- return -1;
|
|
|
- }
|
|
|
- if (o1ContentPoolScore > o2ContentPoolScore) {
|
|
|
- return 1;
|
|
|
- }
|
|
|
- return 0;
|
|
|
});
|
|
|
-
|
|
|
- return new RankResult(contents);
|
|
|
+ Collections.sort(items, Comparator.comparingDouble(o -> -o.getScore()));
|
|
|
+ return new RankResult(items);
|
|
|
}
|
|
|
|
|
|
private ScoreParam convertToScoreParam(RankParam param) {
|