丁云鹏 hace 11 meses
padre
commit
184ab2f31d

+ 1 - 1
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/remote/NLPRemoteService.java

@@ -66,7 +66,7 @@ public class NLPRemoteService {
             log.error("score error", e);
         }
         log.info("score耗时:{}", System.currentTimeMillis() - start);
-        return null;
+        return Collections.emptyMap();
     }
 
 }

+ 4 - 1
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/filter/strategy/CategoryStrategy.java

@@ -1,7 +1,9 @@
 package com.tzld.longarticle.recommend.server.service.filter.strategy;
 
+import com.tzld.longarticle.recommend.server.model.Content;
 import com.tzld.longarticle.recommend.server.service.filter.FilterParam;
 import com.tzld.longarticle.recommend.server.service.filter.FilterStrategy;
+import com.tzld.longarticle.recommend.server.util.CommonCollectionUtils;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Component;
@@ -19,7 +21,8 @@ public class CategoryStrategy implements FilterStrategy {
 
     @Override
     public List<String> filter(FilterParam param) {
-        return null;
+
+        return CommonCollectionUtils.toList(param.getContents(), Content::getId);
     }
 
 }

+ 3 - 1
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/filter/strategy/DuplicateStrategy.java

@@ -1,7 +1,9 @@
 package com.tzld.longarticle.recommend.server.service.filter.strategy;
 
+import com.tzld.longarticle.recommend.server.model.Content;
 import com.tzld.longarticle.recommend.server.service.filter.FilterParam;
 import com.tzld.longarticle.recommend.server.service.filter.FilterStrategy;
+import com.tzld.longarticle.recommend.server.util.CommonCollectionUtils;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Component;
 
@@ -16,7 +18,7 @@ public class DuplicateStrategy implements FilterStrategy {
 
     @Override
     public List<String> filter(FilterParam param) {
-        return null;
+        return CommonCollectionUtils.toList(param.getContents(), Content::getId);
     }
 
 }

+ 14 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/rank/RankItem.java

@@ -0,0 +1,14 @@
+package com.tzld.longarticle.recommend.server.service.rank;
+
+import com.tzld.longarticle.recommend.server.model.Content;
+import lombok.Data;
+
+/**
+ * @author dyp
+ */
+@Data
+public class RankItem {
+    private Content content;
+    private double score;
+
+}

+ 1 - 2
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/rank/RankResult.java

@@ -1,6 +1,5 @@
 package com.tzld.longarticle.recommend.server.service.rank;
 
-import com.tzld.longarticle.recommend.server.model.Content;
 import lombok.AllArgsConstructor;
 import lombok.Data;
 import lombok.NoArgsConstructor;
@@ -14,5 +13,5 @@ import java.util.List;
 @NoArgsConstructor
 @AllArgsConstructor
 public class RankResult {
-    private List<Content> contents;
+    private List<RankItem> contents;
 }

+ 14 - 34
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/rank/RankService.java

@@ -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) {

+ 16 - 1
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/score/strategy/ContentPoolStrategy.java

@@ -5,8 +5,11 @@ import com.tzld.longarticle.recommend.server.service.score.Score;
 import com.tzld.longarticle.recommend.server.service.score.ScoreParam;
 import com.tzld.longarticle.recommend.server.service.score.ScoreStrategy;
 import com.tzld.longarticle.recommend.server.util.CommonCollectionUtils;
+import com.tzld.longarticle.recommend.server.util.NormalizationUtils;
 import org.springframework.stereotype.Component;
+import org.springframework.util.CollectionUtils;
 
+import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 
@@ -22,10 +25,22 @@ public class ContentPoolStrategy implements ScoreStrategy {
     @Override
     public List<Score> score(ScoreParam param) {
 
+        if (CollectionUtils.isEmpty(param.getContents())) {
+            return Collections.emptyList();
+        }
+
+        double min = contentPoolScore.values().stream()
+                .min(Double::compareTo)
+                .orElse(0.0);
+        double max = contentPoolScore.values().stream()
+                .max(Double::compareTo)
+                .orElse(0.0);
+
         List<Score> scores = CommonCollectionUtils.toList(param.getContents(), c -> {
             Score score = new Score();
             score.setContentId(c.getId());
-            score.setScore(contentPoolScore.getOrDefault(c.getContentPoolType(), 0.0));
+            double val = contentPoolScore.getOrDefault(c.getContentPoolType(), 0.0);
+            score.setScore(NormalizationUtils.minMax(val, min, max));
             score.setStrategy(this.getClass());
             return score;
         });

+ 15 - 1
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/score/strategy/SimilarityStrategy.java

@@ -5,9 +5,12 @@ import com.tzld.longarticle.recommend.server.service.score.Score;
 import com.tzld.longarticle.recommend.server.service.score.ScoreParam;
 import com.tzld.longarticle.recommend.server.service.score.ScoreStrategy;
 import com.tzld.longarticle.recommend.server.util.CommonCollectionUtils;
+import com.tzld.longarticle.recommend.server.util.NormalizationUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
+import org.springframework.util.CollectionUtils;
 
+import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 
@@ -23,12 +26,23 @@ public class SimilarityStrategy implements ScoreStrategy {
     @Override
     public List<Score> score(ScoreParam param) {
 
+        if (CollectionUtils.isEmpty(param.getContents())) {
+            return Collections.emptyList();
+        }
         Map<String, Double> scoreMap = nlpRemoteService.score(param.getAccountName(), param.getContents());
 
+        double min = scoreMap.values().stream()
+                .min(Double::compareTo)
+                .orElse(0.0);
+        double max = scoreMap.values().stream()
+                .max(Double::compareTo)
+                .orElse(0.0);
+
         List<Score> scores = CommonCollectionUtils.toList(param.getContents(), c -> {
             Score score = new Score();
             score.setContentId(c.getId());
-            score.setScore(scoreMap.get(c.getId()));
+            double val = scoreMap.get(c.getId()) == null ? 0.0 : scoreMap.get(c.getId());
+            score.setScore(NormalizationUtils.minMax(val, min, max));
             score.setStrategy(this.getClass());
             return score;
         });

+ 47 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/score/strategy/ViewCountStrategy.java

@@ -0,0 +1,47 @@
+package com.tzld.longarticle.recommend.server.service.score.strategy;
+
+import com.tzld.longarticle.recommend.server.service.score.Score;
+import com.tzld.longarticle.recommend.server.service.score.ScoreParam;
+import com.tzld.longarticle.recommend.server.service.score.ScoreStrategy;
+import com.tzld.longarticle.recommend.server.util.CommonCollectionUtils;
+import com.tzld.longarticle.recommend.server.util.NormalizationUtils;
+import org.springframework.stereotype.Component;
+import org.springframework.util.CollectionUtils;
+
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * @author dyp
+ */
+@Component
+public class ViewCountStrategy implements ScoreStrategy {
+
+    @Override
+    public List<Score> score(ScoreParam param) {
+
+        if (CollectionUtils.isEmpty(param.getContents())) {
+            return Collections.emptyList();
+        }
+
+        double min = param.getContents().stream()
+                .map(c -> c.getCrawlerViewCount() == null ? 0.0 : c.getCrawlerViewCount())
+                .min(Double::compareTo)
+                .orElse(0.0);
+        double max = param.getContents().stream()
+                .map(c -> c.getCrawlerViewCount() == null ? 0.0 : c.getCrawlerViewCount())
+                .max(Double::compareTo)
+                .orElse(0.0);
+
+        List<Score> scores = CommonCollectionUtils.toList(param.getContents(), c -> {
+            Score score = new Score();
+            score.setContentId(c.getId());
+            double viewCount = c.getCrawlerViewCount() == null ? 0.0 : c.getCrawlerViewCount();
+            score.setScore(NormalizationUtils.minMax(viewCount, min, max));
+            score.setStrategy(this.getClass());
+            return score;
+        });
+        return scores;
+    }
+
+}

+ 13 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/util/NormalizationUtils.java

@@ -0,0 +1,13 @@
+package com.tzld.longarticle.recommend.server.util;
+
+/**
+ * @author dyp
+ */
+public final class NormalizationUtils {
+    public static double minMax(double value, double min, double max) {
+        if (max == min) {
+            return 0; // 防止除以零的情况
+        }
+        return (value - min) / (max - min);
+    }
+}