supeng 11 ヶ月 前
コミット
caf8eda126
15 ファイル変更1361 行追加466 行削除
  1. 2 2
      recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/common/ThreadPoolFactory.java
  2. 4 161
      recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/filter/AbstractFilterService.java
  3. 2 0
      recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/filter/strategy/AllowListStrategy.java
  4. 8 0
      recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/rank/RankRouter.java
  5. 3 4
      recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/rank/extractor/RankExtractorItemFeature.java
  6. 31 287
      recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/rank/strategy/RankStrategy4RegionMergeModelV569.java
  7. 386 0
      recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/rank/strategy/RankStrategy4RegionMergeModelV650.java
  8. 456 0
      recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/rank/strategy/RankStrategy4ShareDeepAndWidth.java
  9. 27 12
      recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/recall/RecallService.java
  10. 91 0
      recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/recall/strategy/ContentBaseRecallStrategy.java
  11. 240 0
      recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/recall/strategy/TitleTagRecallStrategyV1.java
  12. 1 0
      recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/score/ScorerUtils.java
  13. 44 0
      recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/score4recall/model4recall/VideoTagModel4RecallMap.java
  14. 59 0
      recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/score4recall/strategy/ContentBaseRecallScore.java
  15. 7 0
      recommend-server-service/src/main/resources/content_base_recall.conf

+ 2 - 2
recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/common/ThreadPoolFactory.java

@@ -19,8 +19,8 @@ public final class ThreadPoolFactory {
             new ThreadFactoryBuilder().setNameFormat("DEFAULT-%d").build(),
             new ThreadPoolExecutor.AbortPolicy());
     public final static ExecutorService RECALL = new CommonThreadPoolExecutor(
-            128,
-            128,
+            256,
+            256,
             0L, TimeUnit.SECONDS,
             new LinkedBlockingQueue<>(1000),
             new ThreadFactoryBuilder().setNameFormat("RecallService-%d").build(),

+ 4 - 161
recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/filter/AbstractFilterService.java

@@ -1,175 +1,29 @@
 package com.tzld.piaoquan.recommend.server.service.filter;
 
-import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValue;
 import com.google.common.collect.Lists;
 import com.tzld.piaoquan.recommend.server.common.ThreadPoolFactory;
-import com.tzld.piaoquan.recommend.server.service.PreViewedService;
 import com.tzld.piaoquan.recommend.server.service.ServiceBeanFactory;
-import com.tzld.piaoquan.recommend.server.service.ViewedService;
 import com.tzld.piaoquan.recommend.server.service.filter.strategy.*;
-import com.tzld.piaoquan.recommend.server.util.CommonCollectionUtils;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.collections4.CollectionUtils;
-import org.apache.commons.collections4.MapUtils;
-import org.apache.commons.lang.math.NumberUtils;
-import org.apache.commons.lang3.StringUtils;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.beans.factory.annotation.Value;
 
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
-import java.util.stream.Collectors;
 
 /**
  * @author dyp
  */
 @Slf4j
 public abstract class AbstractFilterService {
-    @Autowired
-    private PreViewedService preViewedService;
-
-    @Autowired
-    private ViewedService viewedService;
 
     private final ExecutorService pool = ThreadPoolFactory.filterPool();
 
-    @Value("${newFilterGlobalSwitch:false}")
-    private boolean newFilterGlobalSwitch;
-    @Value("${newFilterAbExpCode:}")
-    private String newFilterAbExpCode;
-
-    @ApolloJsonValue("${supply.exp.list:[6]}")
-    private Set<Integer> supplyExps;
-
-    @Value("${supply.exp.id:666}")
-    private int supplyExpId;
-
-    @ApolloJsonValue("${supply.exp.video.list:[]}")
-    private Set<Long> supplyExpVideos;
-    @ApolloJsonValue("${not.supply.exp.video.list:[]}")
-    private Set<Long> notSupplyExpVideos;
-
-
-    private List<Long> viewFilterOld(FilterParam param) {
-        // 风险过滤
-        List<Long> videoIds = filterWithRiskVideo(param.getRiskFilterFlag(),
-                param.getAppType(), param.getRegionCode(), param.getAppRegionFiltered(), param.getVideosWithRisk(),
-                param.getVideoIds(), param.getForceTruncation());
-
-        videoIds = filterBySupplyExp(param.getAppType(), param.getExpIdMap(), videoIds);
-
-        if (param.isNotUsePreView()) {
-
-        } else {
-            videoIds = filterByPreViewed(param.getAppType(), param.getMid(), videoIds);
-        }
-        if (param.isConcurrent()) {
-            videoIds = filterByViewedConcurrent(param, videoIds);
-        } else {
-            videoIds = filterByViewed(param, videoIds);
-        }
-//        log.info("viewFilterOld after {}", JSONUtils.toJson(videoIds));
-        return videoIds;
-    }
-
-    private List<Long> filterBySupplyExp(int appType, Map<String, String> expIdMap, List<Long> videoIds) {
-        if (!supplyExps.contains(appType)) {
-            return videoIds;
-        }
-        if (MapUtils.isEmpty(expIdMap)) {
-            return videoIds;
-        }
-        // 供给实验
-        if (supplyExpId == NumberUtils.toInt(expIdMap.get("supply"), -1)) {
-            // 对照组视频只在对照组出
-            return videoIds.stream()
-                    .filter(l -> !notSupplyExpVideos.contains(l))
-                    .collect(Collectors.toList());
-        } else {
-            // 实验组视频只在实验组出
-            return videoIds.stream()
-                    .filter(l -> !supplyExpVideos.contains(l))
-                    .collect(Collectors.toList());
-        }
-    }
-
-    private List<Long> filterByViewedConcurrent(FilterParam param, List<Long> videoIds) {
-//        if (StringUtils.isBlank(param.getMid())
-//                || CollectionUtils.isEmpty(videoIds)) {
-//            return videoIds;
-//        }
-        if (CollectionUtils.isEmpty(videoIds)) {
-            return videoIds;
-        }
-
-        int chunkSize = 20;
-        List<List<Long>> chunks = new ArrayList<>();
-        int size = videoIds.size();
-
-        for (int i = 0; i < size; i += chunkSize) {
-            int endIndex = Math.min(i + chunkSize, size);
-            List<Long> chunk = videoIds.subList(i, endIndex);
-            chunks.add(chunk);
-        }
-
-
-        CountDownLatch cdl = new CountDownLatch(chunks.size());
-        List<Future<List<Long>>> futures = new ArrayList<>();
-        for (final List<Long> ids : chunks) {
-            Future<List<Long>> future = pool.submit(() ->
-                    viewedService.filterViewedVideo(param.getAppType(), param.getMid(), param.getUid(), ids, param.getCityCode(),
-                            param.getAbExpCodes(), param.getHotSceneType(), param.getClientIp()));
-            futures.add(future);
-        }
-        try {
-            cdl.await(150, TimeUnit.MILLISECONDS);
-        } catch (InterruptedException e) {
-            log.error("filter error", e);
-            return null;
-        }
-
-        List<Long> result = new ArrayList<>();
-        for (Future<List<Long>> f : futures) {
-            try {
-                result.addAll(f.get());
-            } catch (Exception e) {
-                log.error("future get error ", e);
-            }
-        }
-
-        return result;
-
-
-    }
-
-    private List<Long> filterByViewed(FilterParam param, List<Long> videoIds) {
-        // TODO uid为空时,还需要过滤么?
-//        if (StringUtils.isBlank(param.getMid())
-//                || CollectionUtils.isEmpty(videoIds)) {
-//            return videoIds;
-//        }
-        if (CollectionUtils.isEmpty(videoIds)) {
-            return videoIds;
-        }
-        return viewedService.filterViewedVideo(param.getAppType(), param.getMid(), param.getUid(), videoIds, param.getCityCode(),
-                param.getAbExpCodes(), param.getHotSceneType(), param.getClientIp());
-
-    }
-
-    private List<Long> filterByPreViewed(int appType, String mid, List<Long> videoIds) {
-
-        if (StringUtils.isBlank(mid)) {
-            return videoIds;
-        }
-        Set<Long> preViewedVideoIds = preViewedService.getVideoIds(appType, mid);
-        return videoIds.stream()
-                .filter(l -> !preViewedVideoIds.contains(l))
-                .collect(Collectors.toList());
-
-    }
 
     private List<Long> filterWithRiskVideo(boolean riskFlag,
                                            int appType,
@@ -210,17 +64,6 @@ public abstract class AbstractFilterService {
     }
 
     protected List<Long> viewFilter(FilterParam param) {
-        boolean hit = newFilterGlobalSwitch
-                || CommonCollectionUtils.contains(param.getAbExpCodes(), newFilterAbExpCode);
-        if (hit) {
-            return viewFilterNew(param);
-        } else {
-            return viewFilterOld(param);
-        }
-    }
-
-    private List<Long> viewFilterNew(FilterParam param) {
-
         // hardcode : 风险过滤会做截断,所以先同步调用
         List<Long> riskVideoIds = filterWithRiskVideo(param.getRiskFilterFlag(),
                 param.getAppType(), param.getRegionCode(), param.getAppRegionFiltered(), param.getVideosWithRisk(),

+ 2 - 0
recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/filter/strategy/AllowListStrategy.java

@@ -91,6 +91,8 @@ public class AllowListStrategy implements FilterStrategy {
                     retainVideoIds.add(videoId);
                 }
             }
+        } else {
+            return Lists.newArrayList(param.getVideoIds());
         }
         if (CollectionUtils.isEmpty(retainVideoIds)) {
             return Collections.emptyList();

+ 8 - 0
recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/rank/RankRouter.java

@@ -52,12 +52,16 @@ public class RankRouter {
     @Autowired
     private RankStrategy4RegionMergeModelV569 rankStrategy4RegionMergeModelV569;
     @Autowired
+    private RankStrategy4RegionMergeModelV650 rankStrategy4RegionMergeModelV650;
+    @Autowired
     private FestivalStrategy4RankModel festivalStrategy4RankModel;
 
     @Autowired
     private RankStrategyFlowThompsonModel rankStrategyFlowThompsonModel;
     @Autowired
     private RankStrategy4RegionMerge rankStrategy4RegionMerge;
+    @Autowired
+    private RankStrategy4ShareDeepAndWidth rankStrategy4ShareDeepAndWidth;
 
     public RankResult rank(RankParam param) {
         String abCode = param.getAbCode();
@@ -110,6 +114,10 @@ public class RankRouter {
             case "60131":
             case "60132":
                 return festivalStrategy4RankModel.rank(param);
+            case "60150": // 645
+                return rankStrategy4ShareDeepAndWidth.rank(param);
+            case "60151": // 650
+                return rankStrategy4RegionMergeModelV650.rank(param);
             default:
                 break;
         }

+ 3 - 4
recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/rank/extractor/RankExtractorItemFeature.java

@@ -162,7 +162,7 @@ public class RankExtractorItemFeature {
     public static Map<String, String> cntFeatureChange4Double(Map<String, Double> maps){
         Map<String, String> result = new HashMap<>();
         for (Map.Entry<String, Double> entry : maps.entrySet()){
-            int value = ExtractorUtils.bucketCnt(Double.valueOf(entry.getValue()));
+            int value = ExtractorUtils.bucketCnt(entry.getValue());
             result.put(entry.getKey(), String.valueOf(value));
         }
         return result;
@@ -292,16 +292,15 @@ public class RankExtractorItemFeature {
 
         k1 = "view_pv_list_1h";
         k2 = "return_uv_list_1h";
-        if (maps.containsKey(k1) && maps.containsKey(k2)){
+        if (maps.containsKey(k1) && maps.containsKey(k2)) {
             d1 = maps.get(k1).getOrDefault(dateHour, 0.0);
             d2 = maps.get(k2).getOrDefault(dateHour, 0.0);
             d = ExtractorUtils.divisionDouble(d1, d2);
-            if (!ExtractorUtils.isDoubleEqualToZero(d)){
+            if (!ExtractorUtils.isDoubleEqualToZero(d)) {
                 result.put("i_1h_rov_rt", d);
             }
         }
 
-
         return rateFeatureChange(result);
     }
 

+ 31 - 287
recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/rank/strategy/RankStrategy4RegionMergeModelV569.java

@@ -66,45 +66,44 @@ public class RankStrategy4RegionMergeModelV569 extends RankService {
         //-------------------逻-------------------
         //-------------------辑-------------------
 
-        //-------------------老地域相关召回-------------------
         List<Video> oldRovs = new ArrayList<>();
         oldRovs.addAll(extractAndSort(param, RegionHRecallStrategy.PUSH_FORM));
         oldRovs.addAll(extractAndSort(param, RegionHDupRecallStrategy.PUSH_FORM));
         oldRovs.addAll(extractAndSort(param, Region24HRecallStrategy.PUSH_FORM));
         oldRovs.addAll(extractAndSort(param, RegionRelative24HRecallStrategy.PUSH_FORM));
         oldRovs.addAll(extractAndSort(param, RegionRelative24HDupRecallStrategy.PUSH_FORM));
-        int sizeReturn = param.getSize();
         removeDuplicate(oldRovs);
+        int sizeReturn = param.getSize();
+        //-------------------老地域召回-------------------
         List<Video> v0 = oldRovs.size() <= sizeReturn
                 ? oldRovs
                 : oldRovs.subList(0, sizeReturn);
-        //-------------------地域相关召回-------------------
-        List<Video> v1 = extractAndSort(param, RegionRealtimeRecallStrategyV1.PUSH_FORM);
-        List<Video> v2 = extractAndSort(param, RegionRealtimeRecallStrategyV2_sort.PUSH_FORM);
-        List<Video> v3 = extractAndSort(param, RegionRealtimeRecallStrategyV3.PUSH_FORM);
-        //-------------------相关性召回-------------------
         List<Video> v5 = extractAndSort(param, SimHotVideoRecallStrategy.PUSH_FORM);
         List<Video> v6 = extractAndSort(param, ReturnVideoRecallStrategy.PUSH_FORM);
-        //-------------------节日扶持召回-------------------
+        //-------------------新地域召回------------------
+        List<Video> v1 = extractAndSort(param, RegionRealtimeRecallStrategyV1.PUSH_FORM);
+        //-------------------节日特殊召回-------------------
         List<Video> v7 = extractAndSort(param, FestivalRecallStrategyV1.PUSH_FORM);
-        //-------------------去重-------------------
+        //-------------------基于title的tag召回-------------------
+        List<Video> v10 = extractAndSort(param, TitleTagRecallStrategyV1.PUSH_FORM);
+
+
+
         Set<Long> setVideo = new HashSet<>();
-        this.duplicate(setVideo, v3);
-        this.duplicate(setVideo, v2);
-        this.duplicate(setVideo, v1);
         this.duplicate(setVideo, v0);
         this.duplicate(setVideo, v5);
         this.duplicate(setVideo, v6);
+        this.duplicate(setVideo, v1);
         this.duplicate(setVideo, v7);
-        //-------------------合并-------------------
+        this.duplicate(setVideo, v10);
+
         List<Video> rovRecallRank = new ArrayList<>();
         rovRecallRank.addAll(v0);
-        rovRecallRank.addAll(v1.subList(0, Math.min(mergeWeight.getOrDefault("v1", 10.0).intValue(), v1.size())));
-        rovRecallRank.addAll(v2.subList(0, Math.min(mergeWeight.getOrDefault("v2", 25.0).intValue(), v2.size())));
-        rovRecallRank.addAll(v3.subList(0, Math.min(mergeWeight.getOrDefault("v3", 10.0).intValue(), v3.size())));
-        rovRecallRank.addAll(v5.subList(0, Math.min(mergeWeight.getOrDefault("v5", 10.0).intValue(), v5.size())));
-        rovRecallRank.addAll(v6.subList(0, Math.min(mergeWeight.getOrDefault("v6", 10.0).intValue(), v6.size())));
-        rovRecallRank.addAll(v7.subList(0, Math.min(mergeWeight.getOrDefault("v7", 10.0).intValue(), v7.size())));
+        rovRecallRank.addAll(v5.subList(0, Math.min(mergeWeight.getOrDefault("v5", 5.0).intValue(), v5.size())));
+        rovRecallRank.addAll(v6.subList(0, Math.min(mergeWeight.getOrDefault("v6", 5.0).intValue(), v6.size())));
+        rovRecallRank.addAll(v1.subList(0, Math.min(mergeWeight.getOrDefault("v1", 5.0).intValue(), v1.size())));
+        rovRecallRank.addAll(v7.subList(0, Math.min(mergeWeight.getOrDefault("v7", 5.0).intValue(), v7.size())));
+        rovRecallRank.addAll(v10.subList(0, Math.min(mergeWeight.getOrDefault("v10", 6.0).intValue(), v10.size())));
 
         //-------------------排-------------------
         //-------------------序-------------------
@@ -133,85 +132,38 @@ public class RankStrategy4RegionMergeModelV569 extends RankService {
             cur = ExtractorUtils.subtractHours(cur, 1);
         }
         for (RankItem item : items) {
-            Map<String, String> itemBasicMap = item.getItemBasicFeature();
             Map<String, Map<String, Double>> itemRealMap = item.getItemRealTimeFeature();
-            List<Double> views = getStaticData(itemRealMap, datehours, "view_pv_list_1h");
-            List<Double> plays = getStaticData(itemRealMap, datehours, "play_pv_list_1h");
-            List<Double> shares = getStaticData(itemRealMap, datehours, "share_pv_list_1h");
-            List<Double> preturns = getStaticData(itemRealMap, datehours, "p_return_uv_list_1h");
+            List<Double> views = getStaticData(itemRealMap, datehours, "view_uv_list_1h");
+            List<Double> shares = getStaticData(itemRealMap, datehours, "share_uv_list_1h");
             List<Double> allreturns = getStaticData(itemRealMap, datehours, "return_uv_list_1h");
 
-            List<Double> share2return = getRateData(preturns, shares, 1.0, 1000.0);
-            Double share2returnScore = calScoreWeightNoTimeDecay(share2return);
-            List<Double> view2return = getRateData(preturns, views, 1.0, 1000.0);
-            Double view2returnScore = calScoreWeightNoTimeDecay(view2return);
-            List<Double> view2play = getRateData(plays, views, 1.0, 1000.0);
-            Double view2playScore = calScoreWeightNoTimeDecay(view2play);
-            List<Double> play2share = getRateData(shares, plays, 1.0, 1000.0);
-            Double play2shareScore = calScoreWeightNoTimeDecay(play2share);
-            item.scoresMap.put("share2returnScore", share2returnScore);
-            item.scoresMap.put("view2returnScore", view2returnScore);
-            item.scoresMap.put("view2playScore", view2playScore);
-            item.scoresMap.put("play2shareScore", play2shareScore);
-
             // 全部回流的rov和ros
-            List<Double> share2allreturn = getRateData(allreturns, shares, 1.0, 10.0);
+            List<Double> share2allreturn = getRateData(allreturns, shares, 0.0, 0.0);
             Double share2allreturnScore = calScoreWeightNoTimeDecay(share2allreturn);
+            item.scoresMap.put("share2allreturnScore", share2allreturnScore);
             List<Double> view2allreturn = getRateData(allreturns, views, 0.0, 0.0);
             Double view2allreturnScore = calScoreWeightNoTimeDecay(view2allreturn);
-            item.scoresMap.put("share2allreturnScore", share2allreturnScore);
             item.scoresMap.put("view2allreturnScore", view2allreturnScore);
 
             // 全部回流
             Double allreturnsScore = calScoreWeightNoTimeDecay(allreturns);
             item.scoresMap.put("allreturnsScore", allreturnsScore);
 
-            // 平台回流
-            Double preturnsScore = calScoreWeightNoTimeDecay(preturns);
-            item.scoresMap.put("preturnsScore", preturnsScore);
-
-            // rov的趋势
-            double trendScore = calTrendScore(view2return);
-            item.scoresMap.put("trendScore", trendScore);
-
-            // 新视频提取
-            double newVideoScore = calNewVideoScore(itemBasicMap);
-            item.scoresMap.put("newVideoScore", newVideoScore);
 
         }
         // 3 融合公式
         List<Video> result = new ArrayList<>();
-        double a = mergeWeight.getOrDefault("a", 0.1);
-        double b = mergeWeight.getOrDefault("b", 0.0);
-        double c = mergeWeight.getOrDefault("c", 0.000001);
-        double d = mergeWeight.getOrDefault("d", 1.0);
-        double e = mergeWeight.getOrDefault("e", 1.0);
-        double f = mergeWeight.getOrDefault("f", 0.6);
-        double g = mergeWeight.getOrDefault("g", 2.0);
-        double h = mergeWeight.getOrDefault("h", 240.0);
-        double ifAdd = mergeWeight.getOrDefault("ifAdd", 1.0);
+        double f = mergeWeight.getOrDefault("f", 0.1);
+        double g = mergeWeight.getOrDefault("g", 1.0);
         for (RankItem item : items) {
-            double trendScore = item.scoresMap.getOrDefault("trendScore", 0.0) > 1E-8 ?
-                    item.scoresMap.getOrDefault("trendScore", 0.0) : 0.0;
-            double newVideoScore = item.scoresMap.getOrDefault("newVideoScore", 0.0) > 1E-8 ?
-                    item.scoresMap.getOrDefault("newVideoScore", 0.0) : 0.0;
-            double strScore = item.getScoreStr();
-            double rosScore = item.scoresMap.getOrDefault("share2returnScore", 0.0);
             double share2allreturnScore = item.scoresMap.getOrDefault("share2allreturnScore", 0.0);
             double view2allreturnScore = item.scoresMap.getOrDefault("view2allreturnScore", 0.0);
-            double preturnsScore = Math.log(1 + item.scoresMap.getOrDefault("preturnsScore", 0.0));
             double score = 0.0;
-            if (ifAdd < 0.5) {
-                score = Math.pow(strScore, a) * Math.pow(rosScore, b) + c * preturnsScore +
-                        (newVideoScore > 1E-8 ? d * trendScore * (e + newVideoScore) : 0.0);
-            } else {
-                score = a * strScore + b * rosScore + c * preturnsScore +
-                        (newVideoScore > 1E-8 ? d * trendScore * (e + newVideoScore) : 0.0);
-
-            }
             double allreturnsScore = item.scoresMap.getOrDefault("allreturnsScore", 0.0);
-            if (allreturnsScore > h) {
+            if (allreturnsScore > 50) {
                 score += (f * share2allreturnScore + g * view2allreturnScore);
+            }else{
+                score += (f * share2allreturnScore + g * view2allreturnScore) * 0.01;
             }
             Video video = item.getVideo();
             video.setScore(score);
@@ -220,37 +172,10 @@ public class RankStrategy4RegionMergeModelV569 extends RankService {
             video.setScoresMap(item.getScoresMap());
             result.add(video);
         }
-        Collections.sort(result, Comparator.comparingDouble(o -> -o.getSortScore()));
+        result.sort(Comparator.comparingDouble(o -> -o.getSortScore()));
         return result;
     }
 
-    public double calNewVideoScore(Map<String, String> itemBasicMap) {
-        double existenceDays = Double.valueOf(itemBasicMap.getOrDefault("existence_days", "30"));
-        if (existenceDays > 5) {
-            return 0.0;
-        }
-        double score = 1.0 / (existenceDays + 10.0);
-        return score;
-    }
-
-    public double calTrendScore(List<Double> data) {
-        double sum = 0.0;
-        int size = data.size();
-        for (int i = 0; i < size - 4; ++i) {
-            sum += data.get(i) - data.get(i + 4);
-        }
-        if (sum * 10 > 0.6) {
-            sum = 0.6;
-        } else {
-            sum = sum * 10;
-        }
-        if (sum > 0) {
-            // 为了打断点
-            sum = sum;
-        }
-        return sum;
-    }
-
     public Double calScoreWeightNoTimeDecay(List<Double> data) {
         Double up = 0.0;
         Double down = 0.0;
@@ -294,165 +219,18 @@ public class RankStrategy4RegionMergeModelV569 extends RankService {
             return result;
         }
 
-        RedisStandaloneConfiguration redisSC = new RedisStandaloneConfiguration();
-        redisSC.setPort(6379);
-        redisSC.setPassword("Wqsd@2019");
-        redisSC.setHostName("r-bp1pi8wyv6lzvgjy5z.redis.rds.aliyuncs.com");
-        RedisConnectionFactory connectionFactory = new JedisConnectionFactory(redisSC);
-        RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
-        redisTemplate.setConnectionFactory(connectionFactory);
-        redisTemplate.setDefaultSerializer(new StringRedisSerializer());
-        redisTemplate.afterPropertiesSet();
-
-        // 0: 场景特征处理
-        Map<String, String> sceneFeatureMap = this.getSceneFeature(param);
-
-        // 1: user特征处理
-        Map<String, String> userFeatureMap = new HashMap<>();
-        if (param.getMid() != null && !param.getMid().isEmpty()) {
-            String midKey = "user_info_4video_" + param.getMid();
-            String userFeatureStr = redisTemplate.opsForValue().get(midKey);
-            if (userFeatureStr != null) {
-                try {
-                    userFeatureMap = JSONUtils.fromJson(userFeatureStr,
-                            new TypeToken<Map<String, String>>() {
-                            },
-                            userFeatureMap);
-                } catch (Exception e) {
-                    log.error(String.format("parse user json is wrong in {} with {}", this.CLASS_NAME, e));
-                }
-            }
-        }
-        final Set<String> userFeatureSet = new HashSet<>(Arrays.asList(
-                "machineinfo_brand", "machineinfo_model", "machineinfo_platform", "machineinfo_system",
-                "u_1day_exp_cnt", "u_1day_click_cnt", "u_1day_share_cnt", "u_1day_return_cnt",
-                "u_3day_exp_cnt", "u_3day_click_cnt", "u_3day_share_cnt", "u_3day_return_cnt"
-        ));
-        Iterator<Map.Entry<String, String>> iterator = userFeatureMap.entrySet().iterator();
-        while (iterator.hasNext()) {
-            Map.Entry<String, String> entry = iterator.next();
-            if (!userFeatureSet.contains(entry.getKey())) {
-                iterator.remove();
-            }
-        }
-
-        Map<String, String> f1 = RankExtractorUserFeature.getOriginFeature(userFeatureMap,
-                new HashSet<String>(Arrays.asList(
-                        "machineinfo_brand", "machineinfo_model", "machineinfo_platform", "machineinfo_system"
-                ))
-        );
-        Map<String, String> f2 = RankExtractorUserFeature.getUserRateFeature(userFeatureMap);
-        Map<String, String> f3 = RankExtractorUserFeature.cntFeatureChange(userFeatureMap,
-                new HashSet<String>(Arrays.asList(
-                        "u_1day_exp_cnt", "u_1day_click_cnt", "u_1day_share_cnt", "u_1day_return_cnt",
-                        "u_3day_exp_cnt", "u_3day_click_cnt", "u_3day_share_cnt", "u_3day_return_cnt"
-                ))
-        );
-        f1.putAll(f2);
-        f1.putAll(f3);
-
-        // 2-1: item特征处理
-        final Set<String> itemFeatureSet = new HashSet<>(Arrays.asList(
-                "total_time", "play_count_total",
-                "i_1day_exp_cnt", "i_1day_click_cnt", "i_1day_share_cnt", "i_1day_return_cnt",
-                "i_3day_exp_cnt", "i_3day_click_cnt", "i_3day_share_cnt", "i_3day_return_cnt"
-        ));
 
         List<RankItem> rankItems = CommonCollectionUtils.toList(videos, RankItem::new);
         List<Long> videoIds = CommonCollectionUtils.toListDistinct(videos, Video::getVideoId);
-        List<String> videoFeatureKeys = videoIds.stream().map(r -> "video_info_" + r)
-                .collect(Collectors.toList());
-        List<String> videoFeatures = redisTemplate.opsForValue().multiGet(videoFeatureKeys);
-        if (videoFeatures != null) {
-            for (int i = 0; i < videoFeatures.size(); ++i) {
-                String vF = videoFeatures.get(i);
-                Map<String, String> vfMap = new HashMap<>();
-                if (vF == null) {
-                    continue;
-                }
-                try {
-                    vfMap = JSONUtils.fromJson(vF, new TypeToken<Map<String, String>>() {
-                    }, vfMap);
-                    Map<String, String> vfMapCopy = new HashMap<>(vfMap);
-                    rankItems.get(i).setItemBasicFeature(vfMapCopy);
-                    Iterator<Map.Entry<String, String>> iteratorIn = vfMap.entrySet().iterator();
-                    while (iteratorIn.hasNext()) {
-                        Map.Entry<String, String> entry = iteratorIn.next();
-                        if (!itemFeatureSet.contains(entry.getKey())) {
-                            iteratorIn.remove();
-                        }
-                    }
-                    Map<String, String> f4 = RankExtractorItemFeature.getItemRateFeature(vfMap);
-                    Map<String, String> f5 = RankExtractorItemFeature.cntFeatureChange(vfMap,
-                            new HashSet<String>(Arrays.asList(
-                                    "total_time", "play_count_total",
-                                    "i_1day_exp_cnt", "i_1day_click_cnt", "i_1day_share_cnt", "i_1day_return_cnt",
-                                    "i_3day_exp_cnt", "i_3day_click_cnt", "i_3day_share_cnt", "i_3day_return_cnt"))
-                    );
-                    f4.putAll(f5);
-                    rankItems.get(i).setFeatureMap(f4);
-                } catch (Exception e) {
-                    log.error(String.format("parse video json is wrong in {} with {}", this.CLASS_NAME, e));
-                }
-            }
-        }
-        // 2-2: item 实时特征处理
-        List<String> rtFeaPartKey = new ArrayList<>(Arrays.asList("item_rt_fea_1day_partition", "item_rt_fea_1h_partition"));
-        List<String> rtFeaPartKeyResult = this.redisTemplate.opsForValue().multiGet(rtFeaPartKey);
-        Calendar calendar = Calendar.getInstance();
-        String date = new SimpleDateFormat("yyyyMMdd").format(calendar.getTime());
-        String hour = new SimpleDateFormat("HH").format(calendar.getTime());
-        String rtFeaPart1day = date + hour;
-        String rtFeaPart1h = date + hour;
-        if (rtFeaPartKeyResult != null) {
-            if (rtFeaPartKeyResult.get(0) != null) {
-                rtFeaPart1day = rtFeaPartKeyResult.get(0);
-            }
-            if (rtFeaPartKeyResult.get(1) != null) {
-                rtFeaPart1h = rtFeaPartKeyResult.get(1);
-            }
-        }
 
-        List<String> videoRtKeys1 = videoIds.stream().map(r -> "item_rt_fea_1day_" + r)
-                .collect(Collectors.toList());
+        // 2-2: item 实时特征处理
         List<String> videoRtKeys2 = videoIds.stream().map(r -> "item_rt_fea_1h_" + r)
                 .collect(Collectors.toList());
-        videoRtKeys1.addAll(videoRtKeys2);
-        List<String> videoRtFeatures = this.redisTemplate.opsForValue().multiGet(videoRtKeys1);
+        List<String> videoRtFeatures = this.redisTemplate.opsForValue().multiGet(videoRtKeys2);
 
 
         if (videoRtFeatures != null) {
             int j = 0;
-            for (RankItem item : rankItems) {
-                String vF = videoRtFeatures.get(j);
-                ++j;
-                if (vF == null) {
-                    continue;
-                }
-                Map<String, String> vfMap = new HashMap<>();
-                Map<String, Map<String, Double>> vfMapNew = new HashMap<>();
-                try {
-                    vfMap = JSONUtils.fromJson(vF, new TypeToken<Map<String, String>>() {
-                    }, vfMap);
-                    for (Map.Entry<String, String> entry : vfMap.entrySet()) {
-                        String value = entry.getValue();
-                        if (value == null) {
-                            continue;
-                        }
-                        String[] var1 = value.split(",");
-                        Map<String, Double> tmp = new HashMap<>();
-                        for (String var2 : var1) {
-                            String[] var3 = var2.split(":");
-                            tmp.put(var3[0], Double.valueOf(var3[1]));
-                        }
-                        vfMapNew.put(entry.getKey(), tmp);
-                    }
-                } catch (Exception e) {
-                    log.error(String.format("parse video item_rt_fea_1day_ json is wrong in {} with {}", this.CLASS_NAME, e));
-                }
-                Map<String, String> f8 = RankExtractorItemFeature.getItemRealtimeRate(vfMapNew, rtFeaPart1day);
-                item.getFeatureMap().putAll(f8);
-            }
             for (RankItem item : rankItems) {
                 String vF = videoRtFeatures.get(j);
                 ++j;
@@ -482,44 +260,10 @@ public class RankStrategy4RegionMergeModelV569 extends RankService {
                 } catch (Exception e) {
                     log.error(String.format("parse video item_rt_fea_1h_ json is wrong in {} with {}", this.CLASS_NAME, e));
                 }
-                Map<String, String> f8 = RankExtractorItemFeature.getItemRealtimeRate(vfMapNew, rtFeaPart1h);
-                item.getFeatureMap().putAll(f8);
             }
         }
 
-
-        List<RankItem> rovRecallScore = ScorerUtils.getScorerPipeline(ScorerUtils.BASE_CONF)
-                .scoring(sceneFeatureMap, userFeatureMap, rankItems);
-        return rovRecallScore;
-    }
-
-    private Map<String, String> getSceneFeature(RankParam param) {
-        Map<String, String> sceneFeatureMap = new HashMap<>();
-        String provinceCn = param.getProvince();
-        provinceCn = provinceCn.replaceAll("省$", "");
-        sceneFeatureMap.put("ctx_region", provinceCn);
-        String city = param.getCity();
-        if ("台北市".equals(city) |
-                "高雄市".equals(city) |
-                "台中市".equals(city) |
-                "桃园市".equals(city) |
-                "新北市".equals(city) |
-                "台南市".equals(city) |
-                "基隆市".equals(city) |
-                "吉林市".equals(city) |
-                "新竹市".equals(city) |
-                "嘉义市".equals(city)
-        ) {
-        } else {
-            city = city.replaceAll("市$", "");
-        }
-        sceneFeatureMap.put("ctx_city", city);
-
-        Calendar calendar = Calendar.getInstance();
-        sceneFeatureMap.put("ctx_week", (calendar.get(Calendar.DAY_OF_WEEK) + 6) % 7 + "");
-        sceneFeatureMap.put("ctx_hour", new SimpleDateFormat("HH").format(calendar.getTime()));
-
-        return sceneFeatureMap;
+        return rankItems;
     }
 
     @Override

+ 386 - 0
recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/rank/strategy/RankStrategy4RegionMergeModelV650.java

@@ -0,0 +1,386 @@
+package com.tzld.piaoquan.recommend.server.service.rank.strategy;
+
+import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValue;
+import com.google.common.reflect.TypeToken;
+import com.tzld.piaoquan.recommend.server.common.base.RankItem;
+import com.tzld.piaoquan.recommend.server.model.Video;
+import com.tzld.piaoquan.recommend.server.service.rank.RankParam;
+import com.tzld.piaoquan.recommend.server.service.rank.RankResult;
+import com.tzld.piaoquan.recommend.server.service.rank.RankService;
+import com.tzld.piaoquan.recommend.server.service.rank.extractor.ExtractorUtils;
+import com.tzld.piaoquan.recommend.server.service.rank.extractor.RankExtractorItemTags;
+import com.tzld.piaoquan.recommend.server.service.rank.processor.RankProcessorBoost;
+import com.tzld.piaoquan.recommend.server.service.rank.processor.RankProcessorDensity;
+import com.tzld.piaoquan.recommend.server.service.rank.processor.RankProcessorInsert;
+import com.tzld.piaoquan.recommend.server.service.rank.processor.RankProcessorTagFilter;
+import com.tzld.piaoquan.recommend.server.service.recall.strategy.*;
+import com.tzld.piaoquan.recommend.server.util.CommonCollectionUtils;
+import com.tzld.piaoquan.recommend.server.util.JSONUtils;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.lang3.RandomUtils;
+import org.springframework.stereotype.Service;
+
+import java.text.SimpleDateFormat;
+import java.util.*;
+import java.util.stream.Collectors;
+
+/**
+ * @author zhangbo
+ * @desc 地域召回融合 流量池汤姆森
+ */
+@Service
+@Slf4j
+public class RankStrategy4RegionMergeModelV650 extends RankService {
+    @ApolloJsonValue("${rank.score.merge.weightv650:}")
+    private Map<String, Double> mergeWeight;
+    @ApolloJsonValue("${RankStrategy4DensityFilterV2:}")
+    private final Map<String, Map<String, Map<String, String>>> filterRules = new HashMap<>();
+    final private String CLASS_NAME = this.getClass().getSimpleName();
+
+    public void duplicate(Set<Long> setVideo, List<Video> videos) {
+        Iterator<Video> iterator = videos.iterator();
+        while (iterator.hasNext()) {
+            Video v = iterator.next();
+            if (setVideo.contains(v.getVideoId())) {
+                iterator.remove();
+            } else {
+                setVideo.add(v.getVideoId());
+            }
+        }
+    }
+
+    @Override
+    public List<Video> mergeAndRankRovRecall(RankParam param) {
+        Map<String, Double> mergeWeight = this.mergeWeight != null ? this.mergeWeight : new HashMap<>(0);
+        //-------------------融-------------------
+        //-------------------合-------------------
+        //-------------------逻-------------------
+        //-------------------辑-------------------
+
+        List<Video> oldRovs = new ArrayList<>();
+        oldRovs.addAll(extractAndSort(param, RegionHRecallStrategy.PUSH_FORM));
+        oldRovs.addAll(extractAndSort(param, RegionHDupRecallStrategy.PUSH_FORM));
+        oldRovs.addAll(extractAndSort(param, Region24HRecallStrategy.PUSH_FORM));
+        oldRovs.addAll(extractAndSort(param, RegionRelative24HRecallStrategy.PUSH_FORM));
+        oldRovs.addAll(extractAndSort(param, RegionRelative24HDupRecallStrategy.PUSH_FORM));
+        removeDuplicate(oldRovs);
+        int sizeReturn = param.getSize();
+        //-------------------老地域召回-------------------
+        List<Video> v0 = oldRovs.size() <= sizeReturn
+                ? oldRovs
+                : oldRovs.subList(0, sizeReturn);
+        List<Video> v5 = extractAndSort(param, SimHotVideoRecallStrategy.PUSH_FORM);
+        List<Video> v6 = extractAndSort(param, ReturnVideoRecallStrategy.PUSH_FORM);
+        //-------------------新地域召回------------------
+        List<Video> v1 = extractAndSort(param, RegionRealtimeRecallStrategyV1.PUSH_FORM);
+        //-------------------节日特殊召回-------------------
+        List<Video> v7 = extractAndSort(param, FestivalRecallStrategyV1.PUSH_FORM);
+        //-------------------基于CB的tag召回-------------------
+        List<Video> v10 = extractAndSort(param, ContentBaseRecallStrategy.PUSH_FORM);
+
+
+        Set<Long> setVideo = new HashSet<>();
+        this.duplicate(setVideo, v0);
+        this.duplicate(setVideo, v5);
+        this.duplicate(setVideo, v6);
+        this.duplicate(setVideo, v1);
+        this.duplicate(setVideo, v7);
+        this.duplicate(setVideo, v10);
+
+        List<Video> rovRecallRank = new ArrayList<>();
+        rovRecallRank.addAll(v0);
+        rovRecallRank.addAll(v5.subList(0, Math.min(mergeWeight.getOrDefault("v5", 5.0).intValue(), v5.size())));
+        rovRecallRank.addAll(v6.subList(0, Math.min(mergeWeight.getOrDefault("v6", 5.0).intValue(), v6.size())));
+        rovRecallRank.addAll(v1.subList(0, Math.min(mergeWeight.getOrDefault("v1", 5.0).intValue(), v1.size())));
+        rovRecallRank.addAll(v7.subList(0, Math.min(mergeWeight.getOrDefault("v7", 5.0).intValue(), v7.size())));
+        rovRecallRank.addAll(v10.subList(0, Math.min(mergeWeight.getOrDefault("v10", 6.0).intValue(), v10.size())));
+
+        //-------------------排-------------------
+        //-------------------序-------------------
+        //-------------------逻-------------------
+        //-------------------辑-------------------
+
+        // 1 模型分
+        List<String> rtFeaPart = new ArrayList<>();
+        List<RankItem> items = model(rovRecallRank, param, rtFeaPart);
+        List<String> rtFeaPartKey = new ArrayList<>(Arrays.asList("item_rt_fea_1day_partition", "item_rt_fea_1h_partition"));
+        List<String> rtFeaPartKeyResult = this.redisTemplate.opsForValue().multiGet(rtFeaPartKey);
+        Calendar calendar = Calendar.getInstance();
+        String date = new SimpleDateFormat("yyyyMMdd").format(calendar.getTime());
+        String hour = new SimpleDateFormat("HH").format(calendar.getTime());
+        String rtFeaPart1h = date + hour;
+        if (rtFeaPartKeyResult != null) {
+            if (rtFeaPartKeyResult.get(1) != null) {
+                rtFeaPart1h = rtFeaPartKeyResult.get(1);
+            }
+        }
+        // 2 统计分
+        String cur = rtFeaPart1h;
+        List<String> datehours = new LinkedList<>(); // 时间是倒叙的
+        for (int i = 0; i < 24; ++i) {
+            datehours.add(cur);
+            cur = ExtractorUtils.subtractHours(cur, 1);
+        }
+        for (RankItem item : items) {
+            Map<String, Map<String, Double>> itemRealMap = item.getItemRealTimeFeature();
+            List<Double> views = getStaticData(itemRealMap, datehours, "view_uv_list_1h");
+            List<Double> shares = getStaticData(itemRealMap, datehours, "share_uv_list_1h");
+            List<Double> allreturns = getStaticData(itemRealMap, datehours, "return_uv_list_1h");
+
+            // 全部回流的rov和ros
+            List<Double> share2allreturn = getRateData(allreturns, shares, 0.0, 0.0);
+            Double share2allreturnScore = calScoreWeightNoTimeDecay(share2allreturn);
+            item.scoresMap.put("share2allreturnScore", share2allreturnScore);
+            List<Double> view2allreturn = getRateData(allreturns, views, 0.0, 0.0);
+            Double view2allreturnScore = calScoreWeightNoTimeDecay(view2allreturn);
+            item.scoresMap.put("view2allreturnScore", view2allreturnScore);
+
+            // 全部回流
+            Double allreturnsScore = calScoreWeightNoTimeDecay(allreturns);
+            item.scoresMap.put("allreturnsScore", allreturnsScore);
+
+
+        }
+        // 3 融合公式
+        List<Video> result = new ArrayList<>();
+        double f = mergeWeight.getOrDefault("f", 0.1);
+        double g = mergeWeight.getOrDefault("g", 1.0);
+        for (RankItem item : items) {
+            double share2allreturnScore = item.scoresMap.getOrDefault("share2allreturnScore", 0.0);
+            double view2allreturnScore = item.scoresMap.getOrDefault("view2allreturnScore", 0.0);
+            double score = 0.0;
+            double allreturnsScore = item.scoresMap.getOrDefault("allreturnsScore", 0.0);
+            if (allreturnsScore > 50) {
+                score += (f * share2allreturnScore + g * view2allreturnScore);
+            } else {
+                score += (f * share2allreturnScore + g * view2allreturnScore) * 0.01;
+            }
+            Video video = item.getVideo();
+            video.setScore(score);
+            video.setSortScore(score);
+            video.setScoreStr(item.getScoreStr());
+            video.setScoresMap(item.getScoresMap());
+            result.add(video);
+        }
+        result.sort(Comparator.comparingDouble(o -> -o.getSortScore()));
+        return result;
+    }
+
+    public Double calScoreWeightNoTimeDecay(List<Double> data) {
+        Double up = 0.0;
+        Double down = 0.0;
+        for (int i = 0; i < data.size(); ++i) {
+            up += 1.0 * data.get(i);
+            down += 1.0;
+        }
+        return down > 1E-8 ? up / down : 0.0;
+    }
+
+    public List<Double> getRateData(List<Double> ups, List<Double> downs, Double up, Double down) {
+        List<Double> data = new LinkedList<>();
+        for (int i = 0; i < ups.size(); ++i) {
+            if (ExtractorUtils.isDoubleEqualToZero(downs.get(i) + down)) {
+                data.add(0.0);
+            } else {
+                data.add(
+                        (ups.get(i) + up) / (downs.get(i) + down)
+                );
+            }
+        }
+        return data;
+    }
+
+    public List<Double> getStaticData(Map<String, Map<String, Double>> itemRealMap,
+                                      List<String> datehours, String key) {
+        List<Double> views = new LinkedList<>();
+        Map<String, Double> tmp = itemRealMap.getOrDefault(key, new HashMap<>());
+        for (String dh : datehours) {
+            views.add(tmp.getOrDefault(dh, 0.0D) +
+                    (views.isEmpty() ? 0.0 : views.get(views.size() - 1))
+            );
+        }
+        return views;
+    }
+
+    public List<RankItem> model(List<Video> videos, RankParam param,
+                                List<String> rtFeaPart) {
+        List<RankItem> result = new ArrayList<>();
+        if (videos.isEmpty()) {
+            return result;
+        }
+
+
+        List<RankItem> rankItems = CommonCollectionUtils.toList(videos, RankItem::new);
+        List<Long> videoIds = CommonCollectionUtils.toListDistinct(videos, Video::getVideoId);
+
+        // 2-2: item 实时特征处理
+        List<String> videoRtKeys2 = videoIds.stream().map(r -> "item_rt_fea_1h_" + r)
+                .collect(Collectors.toList());
+        List<String> videoRtFeatures = this.redisTemplate.opsForValue().multiGet(videoRtKeys2);
+
+
+        if (videoRtFeatures != null) {
+            int j = 0;
+            for (RankItem item : rankItems) {
+                String vF = videoRtFeatures.get(j);
+                ++j;
+                if (vF == null) {
+                    continue;
+                }
+                Map<String, String> vfMap = new HashMap<>();
+                Map<String, Map<String, Double>> vfMapNew = new HashMap<>();
+                try {
+                    vfMap = JSONUtils.fromJson(vF, new TypeToken<Map<String, String>>() {
+                    }, vfMap);
+
+                    for (Map.Entry<String, String> entry : vfMap.entrySet()) {
+                        String value = entry.getValue();
+                        if (value == null) {
+                            continue;
+                        }
+                        String[] var1 = value.split(",");
+                        Map<String, Double> tmp = new HashMap<>();
+                        for (String var2 : var1) {
+                            String[] var3 = var2.split(":");
+                            tmp.put(var3[0], Double.valueOf(var3[1]));
+                        }
+                        vfMapNew.put(entry.getKey(), tmp);
+                    }
+                    item.setItemRealTimeFeature(vfMapNew);
+                } catch (Exception e) {
+                    log.error(String.format("parse video item_rt_fea_1h_ json is wrong in {} with {}", this.CLASS_NAME, e));
+                }
+            }
+        }
+
+        return rankItems;
+    }
+
+    @Override
+    public RankResult mergeAndSort(RankParam param, List<Video> rovVideos, List<Video> flowVideos) {
+
+        //1 兜底策略,rov池子不足时,用冷启池填补。直接返回。
+        if (CollectionUtils.isEmpty(rovVideos)) {
+            if (param.getSize() < flowVideos.size()) {
+                return new RankResult(flowVideos.subList(0, param.getSize()));
+            } else {
+                return new RankResult(flowVideos);
+            }
+        }
+
+        //2 根据实验号解析阿波罗参数。
+        String abCode = param.getAbCode();
+        Map<String, Map<String, String>> rulesMap = this.filterRules.getOrDefault(abCode, new HashMap<>(0));
+
+        //3 标签读取
+        if (rulesMap != null && !rulesMap.isEmpty()) {
+            RankExtractorItemTags extractorItemTags = new RankExtractorItemTags(this.redisTemplate);
+            extractorItemTags.processor(rovVideos, flowVideos);
+        }
+        //6 合并结果时间卡控
+        if (rulesMap != null && !rulesMap.isEmpty()) {
+            RankProcessorTagFilter.processor(rovVideos, flowVideos, rulesMap);
+        }
+
+        //4 rov池提权功能
+        RankProcessorBoost.boostByTag(rovVideos, rulesMap);
+
+        //5 rov池强插功能
+        RankProcessorInsert.insertByTag(param, rovVideos, rulesMap);
+
+        //7 流量池按比例强插
+        List<Video> result = new ArrayList<>();
+        for (int i = 0; i < param.getTopK() && i < rovVideos.size(); i++) {
+            result.add(rovVideos.get(i));
+        }
+        double flowPoolP = getFlowPoolP(param);
+        int flowPoolIndex = 0;
+        int rovPoolIndex = param.getTopK();
+        for (int i = 0; i < param.getSize() - param.getTopK(); i++) {
+            double rand = RandomUtils.nextDouble(0, 1);
+            if (rand < flowPoolP) {
+                if (flowPoolIndex < flowVideos.size()) {
+                    result.add(flowVideos.get(flowPoolIndex++));
+                } else {
+                    break;
+                }
+            } else {
+                if (rovPoolIndex < rovVideos.size()) {
+                    result.add(rovVideos.get(rovPoolIndex++));
+                } else {
+                    break;
+                }
+            }
+        }
+        if (rovPoolIndex >= rovVideos.size()) {
+            for (int i = flowPoolIndex; i < flowVideos.size() && result.size() < param.getSize(); i++) {
+                result.add(flowVideos.get(i));
+            }
+        }
+        if (flowPoolIndex >= flowVideos.size()) {
+            for (int i = rovPoolIndex; i < rovVideos.size() && result.size() < param.getSize(); i++) {
+                result.add(rovVideos.get(i));
+            }
+        }
+
+        //8 合并结果密度控制
+        Map<String, Integer> densityRules = new HashMap<>();
+        if (rulesMap != null && !rulesMap.isEmpty()) {
+            for (Map.Entry<String, Map<String, String>> entry : rulesMap.entrySet()) {
+                String key = entry.getKey();
+                Map<String, String> value = entry.getValue();
+                if (value.containsKey("density")) {
+                    densityRules.put(key, Integer.valueOf(value.get("density")));
+                }
+            }
+        }
+        Set<Long> videosSet = result.stream().map(Video::getVideoId).collect(Collectors.toSet());
+        List<Video> rovRecallRankNew = rovVideos.stream().filter(r -> !videosSet.contains(r.getVideoId())).collect(Collectors.toList());
+        List<Video> flowPoolRankNew = flowVideos.stream().filter(r -> !videosSet.contains(r.getVideoId())).collect(Collectors.toList());
+        List<Video> resultWithDensity = RankProcessorDensity.mergeDensityControl(result,
+                rovRecallRankNew, flowPoolRankNew, densityRules);
+
+        return new RankResult(resultWithDensity);
+    }
+
+    public static void main(String[] args) {
+//        String up1 = "2024031012:513,2024031013:456,2024031014:449,2024031015:262,2024031016:414,2024031017:431,2024031018:643,2024031019:732,2024031020:927,2024031021:859,2024031022:866,2024031023:358,2024031100:133,2024031101:28,2024031102:22,2024031103:15,2024031104:21,2024031105:36,2024031106:157,2024031107:371,2024031108:378,2024031109:216,2024031110:269,2024031111:299,2024031112:196,2024031113:186,2024031114:85,2024031115:82";
+        String up1 = "2024031012:1167,2024031013:1023,2024031014:947,2024031015:664,2024031016:842,2024031017:898,2024031018:1170,2024031019:1439,2024031020:2010,2024031021:1796,2024031022:1779,2024031023:722,2024031100:226,2024031101:50,2024031102:31,2024031103:30,2024031104:38,2024031105:63,2024031106:293,2024031107:839,2024031108:1250,2024031109:858,2024031110:767,2024031111:697,2024031112:506,2024031113:534,2024031114:381,2024031115:278";
+        String down1 = "2024031012:2019,2024031013:1676,2024031014:1626,2024031015:1458,2024031016:1508,2024031017:1510,2024031018:1713,2024031019:1972,2024031020:2500,2024031021:2348,2024031022:2061,2024031023:1253,2024031100:659,2024031101:243,2024031102:191,2024031103:282,2024031104:246,2024031105:439,2024031106:1079,2024031107:1911,2024031108:2023,2024031109:1432,2024031110:1632,2024031111:1183,2024031112:1024,2024031113:938,2024031114:701,2024031115:541";
+
+//        String up2 = "2024031012:215,2024031013:242,2024031014:166,2024031015:194,2024031016:209,2024031017:245,2024031018:320,2024031019:332,2024031020:400,2024031021:375,2024031022:636,2024031023:316,2024031100:167,2024031101:45,2024031102:22,2024031103:26,2024031104:12,2024031105:22,2024031106:24,2024031107:143,2024031108:181,2024031109:199,2024031110:194,2024031111:330,2024031112:423,2024031113:421,2024031114:497,2024031115:424";
+        String up2 = "2024031012:409,2024031013:464,2024031014:354,2024031015:474,2024031016:436,2024031017:636,2024031018:709,2024031019:741,2024031020:802,2024031021:904,2024031022:1112,2024031023:639,2024031100:378,2024031101:78,2024031102:47,2024031103:37,2024031104:17,2024031105:49,2024031106:103,2024031107:293,2024031108:457,2024031109:488,2024031110:558,2024031111:711,2024031112:785,2024031113:830,2024031114:974,2024031115:850";
+        String down2 = "2024031012:748,2024031013:886,2024031014:788,2024031015:1029,2024031016:957,2024031017:1170,2024031018:1208,2024031019:1181,2024031020:1275,2024031021:1265,2024031022:1512,2024031023:1190,2024031100:1127,2024031101:486,2024031102:289,2024031103:254,2024031104:197,2024031105:310,2024031106:344,2024031107:693,2024031108:976,2024031109:1045,2024031110:1039,2024031111:1257,2024031112:1202,2024031113:1454,2024031114:1785,2024031115:1544";
+
+        RankStrategy4RegionMergeModelV569 job = new RankStrategy4RegionMergeModelV569();
+        List<Double> l1 = job.getRateData(job.help(up1, "2024031115", 24), job.help(down1, "2024031115", 24), 1., 10.);
+        Double d1 = job.calScoreWeightNoTimeDecay(l1);
+
+        System.out.println(d1);
+
+        List<Double> l2 = job.getRateData(job.help(up2, "2024031115", 24), job.help(down2, "2024031115", 24), 1., 10.);
+        Double d2 = job.calScoreWeightNoTimeDecay(l2);
+
+        System.out.println(d2);
+
+    }
+
+    List<Double> help(String s, String date, Integer h) {
+        Map<String, Double> maps = Arrays.stream(s.split(",")).map(pair -> pair.split(":"))
+                .collect(Collectors.toMap(
+                        arr -> arr[0],
+                        arr -> Double.valueOf(arr[1])
+                ));
+        List<String> datehours = new LinkedList<>(); // 时间是倒叙的
+        List<Double> result = new ArrayList<>();
+        for (int i = 0; i < h; ++i) {
+            Double d = (result.isEmpty() ? 0.0 : result.get(result.size() - 1));
+            result.add(d + maps.getOrDefault(date, 0D));
+            datehours.add(date);
+            date = ExtractorUtils.subtractHours(date, 1);
+        }
+        return result;
+    }
+
+}

+ 456 - 0
recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/rank/strategy/RankStrategy4ShareDeepAndWidth.java

@@ -0,0 +1,456 @@
+package com.tzld.piaoquan.recommend.server.service.rank.strategy;
+
+import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValue;
+import com.google.common.reflect.TypeToken;
+import com.tzld.piaoquan.recommend.server.common.base.RankItem;
+import com.tzld.piaoquan.recommend.server.model.Video;
+import com.tzld.piaoquan.recommend.server.service.rank.RankParam;
+import com.tzld.piaoquan.recommend.server.service.rank.RankResult;
+import com.tzld.piaoquan.recommend.server.service.rank.RankService;
+import com.tzld.piaoquan.recommend.server.service.rank.extractor.ExtractorUtils;
+import com.tzld.piaoquan.recommend.server.service.rank.extractor.RankExtractorItemTags;
+import com.tzld.piaoquan.recommend.server.service.rank.processor.RankProcessorBoost;
+import com.tzld.piaoquan.recommend.server.service.rank.processor.RankProcessorDensity;
+import com.tzld.piaoquan.recommend.server.service.rank.processor.RankProcessorInsert;
+import com.tzld.piaoquan.recommend.server.service.rank.processor.RankProcessorTagFilter;
+import com.tzld.piaoquan.recommend.server.service.recall.strategy.*;
+import com.tzld.piaoquan.recommend.server.util.CommonCollectionUtils;
+import com.tzld.piaoquan.recommend.server.util.JSONUtils;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.lang3.RandomUtils;
+import org.springframework.stereotype.Service;
+
+import java.text.SimpleDateFormat;
+import java.util.*;
+import java.util.stream.Collectors;
+
+/**
+ * @author zhangbo
+ * @desc 地域召回融合 流量池汤姆森
+ */
+@Service
+@Slf4j
+public class RankStrategy4ShareDeepAndWidth extends RankService {
+    @ApolloJsonValue("${rank.score.merge.weightv645:}")
+    private Map<String, Double> mergeWeight;
+    @ApolloJsonValue("${RankStrategy4DensityFilterV2:}")
+    private final Map<String, Map<String, Map<String, String>>> filterRules = new HashMap<>();
+    final private String CLASS_NAME = this.getClass().getSimpleName();
+
+    public void duplicate(Set<Long> setVideo, List<Video> videos) {
+        Iterator<Video> iterator = videos.iterator();
+        while (iterator.hasNext()) {
+            Video v = iterator.next();
+            if (setVideo.contains(v.getVideoId())) {
+                iterator.remove();
+            } else {
+                setVideo.add(v.getVideoId());
+            }
+        }
+    }
+
+    @Override
+    public List<Video> mergeAndRankRovRecall(RankParam param) {
+        Map<String, Double> mergeWeight = this.mergeWeight != null ? this.mergeWeight : new HashMap<>(0);
+        //-------------------融-------------------
+        //-------------------合-------------------
+        //-------------------逻-------------------
+        //-------------------辑-------------------
+
+        List<Video> oldRovs = new ArrayList<>();
+        oldRovs.addAll(extractAndSort(param, RegionHRecallStrategy.PUSH_FORM));
+        oldRovs.addAll(extractAndSort(param, RegionHDupRecallStrategy.PUSH_FORM));
+        oldRovs.addAll(extractAndSort(param, Region24HRecallStrategy.PUSH_FORM));
+        oldRovs.addAll(extractAndSort(param, RegionRelative24HRecallStrategy.PUSH_FORM));
+        oldRovs.addAll(extractAndSort(param, RegionRelative24HDupRecallStrategy.PUSH_FORM));
+        removeDuplicate(oldRovs);
+        int sizeReturn = param.getSize();
+        List<Video> v0 = oldRovs.size() <= sizeReturn
+                ? oldRovs
+                : oldRovs.subList(0, sizeReturn);
+        Set<Long> setVideo = new HashSet<>();
+        this.duplicate(setVideo, v0);
+
+
+        //-------------------相关性召回 融合+去重-------------------
+        List<Video> v5 = extractAndSort(param, SimHotVideoRecallStrategy.PUSH_FORM);
+        List<Video> v6 = extractAndSort(param, ReturnVideoRecallStrategy.PUSH_FORM);
+        this.duplicate(setVideo, v5);
+        this.duplicate(setVideo, v6);
+        //-------------------分享深度、广度 融合+去重-------------------
+        List<Video> v9 = extractAndSort(param, ShareWidthRecallStrategy.PUSH_FORM);
+        List<Video> v10 = extractAndSort(param, ShareDeepRecallStrategy.PUSH_FORM);
+        this.duplicate(setVideo, v9);
+        //-------------------地域相关召回 融合+去重-------------------
+        List<Video> v1 = extractAndSort(param, RegionRealtimeRecallStrategyV1.PUSH_FORM);
+        this.duplicate(setVideo, v1);
+        //-------------------节日扶持召回 融合+去重-------------------
+        List<Video> v7 = extractAndSort(param, FestivalRecallStrategyV1.PUSH_FORM);
+        this.duplicate(setVideo, v7);
+        List<Video> rovRecallRank = new ArrayList<>();
+        rovRecallRank.addAll(v0);
+        rovRecallRank.addAll(v5.subList(0, Math.min(mergeWeight.getOrDefault("v5", 5.0).intValue(), v5.size())));
+        rovRecallRank.addAll(v6.subList(0, Math.min(mergeWeight.getOrDefault("v6", 5.0).intValue(), v6.size())));
+        rovRecallRank.addAll(v9.subList(0, Math.min(mergeWeight.getOrDefault("v9", 5.0).intValue(), v9.size())));
+        rovRecallRank.addAll(v10.subList(0, Math.min(mergeWeight.getOrDefault("v10", 5.0).intValue(), v10.size())));
+        rovRecallRank.addAll(v1.subList(0, Math.min(mergeWeight.getOrDefault("v1", 5.0).intValue(), v1.size())));
+        rovRecallRank.addAll(v7.subList(0, Math.min(mergeWeight.getOrDefault("v7", 5.0).intValue(), v7.size())));
+
+        //-------------------排-------------------
+        //-------------------序-------------------
+        //-------------------逻-------------------
+        //-------------------辑-------------------
+
+        // 1 模型分
+        List<String> rtFeaPart = new ArrayList<>();
+        List<RankItem> items = model(rovRecallRank, param, rtFeaPart);
+        List<String> rtFeaPartKey = new ArrayList<>(Arrays.asList("item_rt_fea_1day_partition", "item_rt_fea_1h_partition"));
+        List<String> rtFeaPartKeyResult = this.redisTemplate.opsForValue().multiGet(rtFeaPartKey);
+        Calendar calendar = Calendar.getInstance();
+        String date = new SimpleDateFormat("yyyyMMdd").format(calendar.getTime());
+        String hour = new SimpleDateFormat("HH").format(calendar.getTime());
+        String rtFeaPart1h = date + hour;
+        if (rtFeaPartKeyResult != null) {
+            if (rtFeaPartKeyResult.get(1) != null) {
+                rtFeaPart1h = rtFeaPartKeyResult.get(1);
+            }
+        }
+        // 2 统计分
+        String cur = rtFeaPart1h;
+        List<String> datehours = new LinkedList<>(); // 时间是倒叙的
+        for (int i = 0; i < 24; ++i) {
+            datehours.add(cur);
+            cur = ExtractorUtils.subtractHours(cur, 1);
+        }
+        for (RankItem item : items) {
+            Map<String, Map<String, Double>> itemRealMap = item.getItemRealTimeFeature();
+            List<Double> views = getStaticData(itemRealMap, datehours, "view_uv_list_1h");
+            List<Double> shares = getStaticData(itemRealMap, datehours, "share_uv_list_1h");
+            List<Double> allreturns = getStaticData(itemRealMap, datehours, "return_uv_list_1h");
+
+            // 全部回流的rov和ros
+            List<Double> share2allreturn = getRateData(allreturns, shares, 0.0, 0.0);
+            Double share2allreturnScore = calScoreWeightNoTimeDecay(share2allreturn);
+            item.scoresMap.put("share2allreturnScore", share2allreturnScore);
+            List<Double> view2allreturn = getRateData(allreturns, views, 0.0, 0.0);
+            Double view2allreturnScore = calScoreWeightNoTimeDecay(view2allreturn);
+            item.scoresMap.put("view2allreturnScore", view2allreturnScore);
+
+            // 全部回流
+            Double allreturnsScore = calScoreWeightNoTimeDecay(allreturns);
+            item.scoresMap.put("allreturnsScore", allreturnsScore);
+
+            Double sumShareCount = shares.stream().mapToDouble(Double::doubleValue).sum();
+            item.scoresMap.put("sumShareCount", sumShareCount);
+
+
+        }
+        // 3 融合公式
+        List<Video> result = new ArrayList<>();
+        double a = mergeWeight.getOrDefault("a", 1.0);
+        double b = mergeWeight.getOrDefault("b", 1.0);
+
+        double f = mergeWeight.getOrDefault("f", 0.1);
+        double g = mergeWeight.getOrDefault("g", 1.0);
+        for (RankItem item : items) {
+            Map<String, String> featureMap = item.getFeatureMap();
+            double share2allreturnScore = item.scoresMap.getOrDefault("share2allreturnScore", 0.0);
+            double view2allreturnScore = item.scoresMap.getOrDefault("view2allreturnScore", 0.0);
+            double score = 0.0;
+            double allreturnsScore = item.scoresMap.getOrDefault("allreturnsScore", 0.0);
+            if (allreturnsScore > 50) {
+                score += (f * share2allreturnScore + g * view2allreturnScore);
+            } else {
+                score += (f * share2allreturnScore + g * view2allreturnScore) * 0.01;
+            }
+            double sumShareCount = item.scoresMap.getOrDefault("sumShareCount", 0.0);
+            double daySharedepthMaxAvg = Double.parseDouble(featureMap.getOrDefault("i_1day_sharedepth_max_avg", "0.0"));
+            double daySharewidthMaxAvg = Double.parseDouble(featureMap.getOrDefault("i_1day_sharewidth_max_avg", "0.0"));
+
+            if (sumShareCount > 30) {
+                score += a * daySharedepthMaxAvg + b * daySharewidthMaxAvg;
+            }
+
+            Video video = item.getVideo();
+            video.setScore(score);
+            video.setSortScore(score);
+            video.setScoreStr(item.getScoreStr());
+            video.setScoresMap(item.getScoresMap());
+            result.add(video);
+        }
+        result.sort(Comparator.comparingDouble(o -> -o.getSortScore()));
+        return result;
+    }
+
+    public Double calScoreWeightNoTimeDecay(List<Double> data) {
+        Double up = 0.0;
+        Double down = 0.0;
+        for (int i = 0; i < data.size(); ++i) {
+            up += 1.0 * data.get(i);
+            down += 1.0;
+        }
+        return down > 1E-8 ? up / down : 0.0;
+    }
+
+    public List<Double> getRateData(List<Double> ups, List<Double> downs, Double up, Double down) {
+        List<Double> data = new LinkedList<>();
+        for (int i = 0; i < ups.size(); ++i) {
+            if (ExtractorUtils.isDoubleEqualToZero(downs.get(i) + down)) {
+                data.add(0.0);
+            } else {
+                data.add(
+                        (ups.get(i) + up) / (downs.get(i) + down)
+                );
+            }
+        }
+        return data;
+    }
+
+    public List<Double> getStaticData(Map<String, Map<String, Double>> itemRealMap,
+                                      List<String> datehours, String key) {
+        List<Double> views = new LinkedList<>();
+        Map<String, Double> tmp = itemRealMap.getOrDefault(key, new HashMap<>());
+        for (String dh : datehours) {
+            views.add(tmp.getOrDefault(dh, 0.0D) +
+                    (views.isEmpty() ? 0.0 : views.get(views.size() - 1))
+            );
+        }
+        return views;
+    }
+
+    public List<RankItem> model(List<Video> videos, RankParam param,
+                                List<String> rtFeaPart) {
+        List<RankItem> result = new ArrayList<>();
+        if (videos.isEmpty()) {
+            return result;
+        }
+
+        List<RankItem> rankItems = CommonCollectionUtils.toList(videos, RankItem::new);
+        List<Long> videoIds = CommonCollectionUtils.toListDistinct(videos, Video::getVideoId);
+
+        // 2-2: item 实时特征处理
+        List<String> rtFeaPartKey = new ArrayList<>(Arrays.asList("item_rt_fea_1day_partition", "item_rt_fea_1h_partition"));
+        List<String> rtFeaPartKeyResult = this.redisTemplate.opsForValue().multiGet(rtFeaPartKey);
+        Calendar calendar = Calendar.getInstance();
+        String date = new SimpleDateFormat("yyyyMMdd").format(calendar.getTime());
+        String hour = new SimpleDateFormat("HH").format(calendar.getTime());
+        String rtFeaPart1day = date + hour;
+        if (rtFeaPartKeyResult != null) {
+            if (rtFeaPartKeyResult.get(0) != null) {
+                rtFeaPart1day = rtFeaPartKeyResult.get(0);
+            }
+        }
+
+        List<String> videoRtKeys1 = videoIds.stream().map(r -> "item_rt_fea_1day_" + r)
+                .collect(Collectors.toList());
+        List<String> videoRtKeys2 = videoIds.stream().map(r -> "item_rt_fea_1h_" + r)
+                .collect(Collectors.toList());
+        videoRtKeys1.addAll(videoRtKeys2);
+        List<String> videoRtFeatures = this.redisTemplate.opsForValue().multiGet(videoRtKeys1);
+
+
+        if (videoRtFeatures != null) {
+            int j = 0;
+            for (RankItem item : rankItems) {
+                String vF = videoRtFeatures.get(j);
+                ++j;
+                if (vF == null) {
+                    continue;
+                }
+                Map<String, String> vfMap = new HashMap<>();
+                Map<String, Map<String, Double>> vfMapNew = new HashMap<>();
+                try {
+                    vfMap = JSONUtils.fromJson(vF, new TypeToken<Map<String, String>>() {
+                    }, vfMap);
+                    for (Map.Entry<String, String> entry : vfMap.entrySet()) {
+                        String value = entry.getValue();
+                        if (value == null) {
+                            continue;
+                        }
+                        String[] var1 = value.split(",");
+                        Map<String, Double> tmp = new HashMap<>();
+                        for (String var2 : var1) {
+                            String[] var3 = var2.split(":");
+                            tmp.put(var3[0], Double.valueOf(var3[1]));
+                        }
+                        vfMapNew.put(entry.getKey(), tmp);
+                    }
+                } catch (Exception e) {
+                    log.error(String.format("parse video item_rt_fea_1day_ json is wrong in {} with {}", this.CLASS_NAME, e));
+                }
+
+                String k1 = "sharedepth_max_avg_list_1day";
+                String k2 = "sharewidth_max_avg_list_1day";
+                if (vfMapNew.containsKey(k1)) {
+                    Double shareDepth = vfMapNew.get(k1).getOrDefault(rtFeaPart1day, 0.0);
+                    item.getFeatureMap().put("i_1day_sharedepth_max_avg", shareDepth + "");
+                }
+                if (vfMapNew.containsKey(k2)) {
+                    Double shareWidth = vfMapNew.get(k2).getOrDefault(rtFeaPart1day, 0.0);
+                    item.getFeatureMap().put("i_1day_sharewidth_max_avg", shareWidth + "");
+                }
+
+            }
+            for (RankItem item : rankItems) {
+                String vF = videoRtFeatures.get(j);
+                ++j;
+                if (vF == null) {
+                    continue;
+                }
+                Map<String, String> vfMap = new HashMap<>();
+                Map<String, Map<String, Double>> vfMapNew = new HashMap<>();
+                try {
+                    vfMap = JSONUtils.fromJson(vF, new TypeToken<Map<String, String>>() {
+                    }, vfMap);
+
+                    for (Map.Entry<String, String> entry : vfMap.entrySet()) {
+                        String value = entry.getValue();
+                        if (value == null) {
+                            continue;
+                        }
+                        String[] var1 = value.split(",");
+                        Map<String, Double> tmp = new HashMap<>();
+                        for (String var2 : var1) {
+                            String[] var3 = var2.split(":");
+                            tmp.put(var3[0], Double.valueOf(var3[1]));
+                        }
+                        vfMapNew.put(entry.getKey(), tmp);
+                    }
+                    item.setItemRealTimeFeature(vfMapNew);
+                } catch (Exception e) {
+                    log.error(String.format("parse video item_rt_fea_1h_ json is wrong in {} with {}", this.CLASS_NAME, e));
+                }
+            }
+        }
+
+        return rankItems;
+    }
+
+    @Override
+    public RankResult mergeAndSort(RankParam param, List<Video> rovVideos, List<Video> flowVideos) {
+
+        //1 兜底策略,rov池子不足时,用冷启池填补。直接返回。
+        if (CollectionUtils.isEmpty(rovVideos)) {
+            if (param.getSize() < flowVideos.size()) {
+                return new RankResult(flowVideos.subList(0, param.getSize()));
+            } else {
+                return new RankResult(flowVideos);
+            }
+        }
+
+        //2 根据实验号解析阿波罗参数。
+        String abCode = param.getAbCode();
+        Map<String, Map<String, String>> rulesMap = this.filterRules.getOrDefault(abCode, new HashMap<>(0));
+
+        //3 标签读取
+        if (rulesMap != null && !rulesMap.isEmpty()) {
+            RankExtractorItemTags extractorItemTags = new RankExtractorItemTags(this.redisTemplate);
+            extractorItemTags.processor(rovVideos, flowVideos);
+        }
+        //6 合并结果时间卡控
+        if (rulesMap != null && !rulesMap.isEmpty()) {
+            RankProcessorTagFilter.processor(rovVideos, flowVideos, rulesMap);
+        }
+
+        //4 rov池提权功能
+        RankProcessorBoost.boostByTag(rovVideos, rulesMap);
+
+        //5 rov池强插功能
+        RankProcessorInsert.insertByTag(param, rovVideos, rulesMap);
+
+        //7 流量池按比例强插
+        List<Video> result = new ArrayList<>();
+        for (int i = 0; i < param.getTopK() && i < rovVideos.size(); i++) {
+            result.add(rovVideos.get(i));
+        }
+        double flowPoolP = getFlowPoolP(param);
+        int flowPoolIndex = 0;
+        int rovPoolIndex = param.getTopK();
+        for (int i = 0; i < param.getSize() - param.getTopK(); i++) {
+            double rand = RandomUtils.nextDouble(0, 1);
+            if (rand < flowPoolP) {
+                if (flowPoolIndex < flowVideos.size()) {
+                    result.add(flowVideos.get(flowPoolIndex++));
+                } else {
+                    break;
+                }
+            } else {
+                if (rovPoolIndex < rovVideos.size()) {
+                    result.add(rovVideos.get(rovPoolIndex++));
+                } else {
+                    break;
+                }
+            }
+        }
+        if (rovPoolIndex >= rovVideos.size()) {
+            for (int i = flowPoolIndex; i < flowVideos.size() && result.size() < param.getSize(); i++) {
+                result.add(flowVideos.get(i));
+            }
+        }
+        if (flowPoolIndex >= flowVideos.size()) {
+            for (int i = rovPoolIndex; i < rovVideos.size() && result.size() < param.getSize(); i++) {
+                result.add(rovVideos.get(i));
+            }
+        }
+
+        //8 合并结果密度控制
+        Map<String, Integer> densityRules = new HashMap<>();
+        if (rulesMap != null && !rulesMap.isEmpty()) {
+            for (Map.Entry<String, Map<String, String>> entry : rulesMap.entrySet()) {
+                String key = entry.getKey();
+                Map<String, String> value = entry.getValue();
+                if (value.containsKey("density")) {
+                    densityRules.put(key, Integer.valueOf(value.get("density")));
+                }
+            }
+        }
+        Set<Long> videosSet = result.stream().map(Video::getVideoId).collect(Collectors.toSet());
+        List<Video> rovRecallRankNew = rovVideos.stream().filter(r -> !videosSet.contains(r.getVideoId())).collect(Collectors.toList());
+        List<Video> flowPoolRankNew = flowVideos.stream().filter(r -> !videosSet.contains(r.getVideoId())).collect(Collectors.toList());
+        List<Video> resultWithDensity = RankProcessorDensity.mergeDensityControl(result,
+                rovRecallRankNew, flowPoolRankNew, densityRules);
+
+        return new RankResult(resultWithDensity);
+    }
+
+    public static void main(String[] args) {
+//        String up1 = "2024031012:513,2024031013:456,2024031014:449,2024031015:262,2024031016:414,2024031017:431,2024031018:643,2024031019:732,2024031020:927,2024031021:859,2024031022:866,2024031023:358,2024031100:133,2024031101:28,2024031102:22,2024031103:15,2024031104:21,2024031105:36,2024031106:157,2024031107:371,2024031108:378,2024031109:216,2024031110:269,2024031111:299,2024031112:196,2024031113:186,2024031114:85,2024031115:82";
+        String up1 = "2024031012:1167,2024031013:1023,2024031014:947,2024031015:664,2024031016:842,2024031017:898,2024031018:1170,2024031019:1439,2024031020:2010,2024031021:1796,2024031022:1779,2024031023:722,2024031100:226,2024031101:50,2024031102:31,2024031103:30,2024031104:38,2024031105:63,2024031106:293,2024031107:839,2024031108:1250,2024031109:858,2024031110:767,2024031111:697,2024031112:506,2024031113:534,2024031114:381,2024031115:278";
+        String down1 = "2024031012:2019,2024031013:1676,2024031014:1626,2024031015:1458,2024031016:1508,2024031017:1510,2024031018:1713,2024031019:1972,2024031020:2500,2024031021:2348,2024031022:2061,2024031023:1253,2024031100:659,2024031101:243,2024031102:191,2024031103:282,2024031104:246,2024031105:439,2024031106:1079,2024031107:1911,2024031108:2023,2024031109:1432,2024031110:1632,2024031111:1183,2024031112:1024,2024031113:938,2024031114:701,2024031115:541";
+
+//        String up2 = "2024031012:215,2024031013:242,2024031014:166,2024031015:194,2024031016:209,2024031017:245,2024031018:320,2024031019:332,2024031020:400,2024031021:375,2024031022:636,2024031023:316,2024031100:167,2024031101:45,2024031102:22,2024031103:26,2024031104:12,2024031105:22,2024031106:24,2024031107:143,2024031108:181,2024031109:199,2024031110:194,2024031111:330,2024031112:423,2024031113:421,2024031114:497,2024031115:424";
+        String up2 = "2024031012:409,2024031013:464,2024031014:354,2024031015:474,2024031016:436,2024031017:636,2024031018:709,2024031019:741,2024031020:802,2024031021:904,2024031022:1112,2024031023:639,2024031100:378,2024031101:78,2024031102:47,2024031103:37,2024031104:17,2024031105:49,2024031106:103,2024031107:293,2024031108:457,2024031109:488,2024031110:558,2024031111:711,2024031112:785,2024031113:830,2024031114:974,2024031115:850";
+        String down2 = "2024031012:748,2024031013:886,2024031014:788,2024031015:1029,2024031016:957,2024031017:1170,2024031018:1208,2024031019:1181,2024031020:1275,2024031021:1265,2024031022:1512,2024031023:1190,2024031100:1127,2024031101:486,2024031102:289,2024031103:254,2024031104:197,2024031105:310,2024031106:344,2024031107:693,2024031108:976,2024031109:1045,2024031110:1039,2024031111:1257,2024031112:1202,2024031113:1454,2024031114:1785,2024031115:1544";
+
+        RankStrategy4RegionMergeModelV567 job = new RankStrategy4RegionMergeModelV567();
+        List<Double> l1 = job.getRateData(job.help(up1, "2024031115", 24), job.help(down1, "2024031115", 24), 1., 10.);
+        Double d1 = job.calScoreWeightNoTimeDecay(l1);
+
+        System.out.println(d1);
+
+        List<Double> l2 = job.getRateData(job.help(up2, "2024031115", 24), job.help(down2, "2024031115", 24), 1., 10.);
+        Double d2 = job.calScoreWeightNoTimeDecay(l2);
+
+        System.out.println(d2);
+
+    }
+
+    List<Double> help(String s, String date, Integer h) {
+        Map<String, Double> maps = Arrays.stream(s.split(",")).map(pair -> pair.split(":"))
+                .collect(Collectors.toMap(
+                        arr -> arr[0],
+                        arr -> Double.valueOf(arr[1])
+                ));
+        List<String> datehours = new LinkedList<>(); // 时间是倒叙的
+        List<Double> result = new ArrayList<>();
+        for (int i = 0; i < h; ++i) {
+            Double d = (result.isEmpty() ? 0.0 : result.get(result.size() - 1));
+            result.add(d + maps.getOrDefault(date, 0D));
+            datehours.add(date);
+            date = ExtractorUtils.subtractHours(date, 1);
+        }
+        return result;
+    }
+
+}

+ 27 - 12
recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/recall/RecallService.java

@@ -7,6 +7,7 @@ import com.tzld.piaoquan.recommend.server.service.filter.strategy.BlacklistConta
 import com.tzld.piaoquan.recommend.server.service.flowpool.FlowPoolConstants;
 import com.tzld.piaoquan.recommend.server.service.recall.strategy.*;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.BeansException;
 import org.springframework.context.ApplicationContext;
@@ -40,6 +41,12 @@ public class RecallService implements ApplicationContextAware {
      */
     private static final String SUPPLY_AB_CODE = "60600";
 
+    /**
+     * 在流量池场景下,哪些appType用判断黑名单
+     */
+    @ApolloJsonValue("${content.security.recommendflowpool.blacklist.apptype.config:[]}")
+    private Set<Integer> blacklistAppTypeSet;
+
     @PostConstruct
     public void init() {
         Map<String, RecallStrategy> type = applicationContext.getBeansOfType(RecallStrategy.class);
@@ -87,9 +94,11 @@ public class RecallService implements ApplicationContextAware {
             strategies.add(strategyMap.get(SpecialRecallStrategy.class.getSimpleName()));
             return strategies;
         }
-        String matchUserBlacklistTypeEnum = blacklistContainer.matchUserBlacklistTypeEnum(param.getUid(), param.getHotSceneType(), param.getCityCode(),
-                param.getClientIp(), param.getMid(), "recommend-flow-pool", param.getAppType());
-        boolean hitUserBlacklist = StringUtils.isNotBlank(matchUserBlacklistTypeEnum);
+
+            String matchUserBlacklistTypeEnum = blacklistContainer.matchUserBlacklistTypeEnum(param.getUid(), param.getHotSceneType(), param.getCityCode(),
+                    param.getClientIp(), param.getMid(), "recommend-flow-pool", param.getAppType());
+        boolean hitUserBlacklist =  StringUtils.isNotBlank(matchUserBlacklistTypeEnum);
+        boolean isInBlacklist = CollectionUtils.isNotEmpty(blacklistAppTypeSet) && blacklistAppTypeSet.contains(param.getAppType());
 
         String abCode = param.getAbCode();
         //1:通过“产品”控制“召回子策略”
@@ -149,26 +158,31 @@ public class RecallService implements ApplicationContextAware {
                     strategies.add(strategyMap.get(RegionRealtimeRecallStrategyV3.class.getSimpleName()));
                     strategies.add(strategyMap.get(RegionRealtimeRecallStrategyV4.class.getSimpleName()));
                     break;
-                case "60150":
-                    strategies.add(strategyMap.get(SimHotVideoRecallStrategy.class.getSimpleName()));
-                    strategies.add(strategyMap.get(ReturnVideoRecallStrategy.class.getSimpleName()));
+                case "60150": // 645
+                    strategies.add(strategyMap.get(RegionRealtimeRecallStrategyV1.class.getSimpleName()));
                     strategies.add(strategyMap.get(ShareWidthRecallStrategy.class.getSimpleName()));
                     strategies.add(strategyMap.get(ShareDeepRecallStrategy.class.getSimpleName()));
                     break;
+                case "60151": // 650
+                    strategies.addAll(getRegionRecallStrategy(param));
+                    strategies.add(strategyMap.get(SimHotVideoRecallStrategy.class.getSimpleName()));
+                    strategies.add(strategyMap.get(ReturnVideoRecallStrategy.class.getSimpleName()));
+                    strategies.add(strategyMap.get(ContentBaseRecallStrategy.class.getSimpleName()));
+                    strategies.add(strategyMap.get(FestivalRecallStrategyV1.class.getSimpleName()));
+                    break;
                 case "60117": // 567
                 case "60118": // 568
                     strategies.add(strategyMap.get(RegionRealtimeRecallStrategyV1.class.getSimpleName()));
                     strategies.addAll(getRegionRecallStrategy(param));
                 case "60119": // 569
                     strategies.add(strategyMap.get(RegionRealtimeRecallStrategyV1.class.getSimpleName()));
-                    strategies.add(strategyMap.get(RegionRealtimeRecallStrategyV2_sort.class.getSimpleName()));
-                    strategies.add(strategyMap.get(RegionRealtimeRecallStrategyV3.class.getSimpleName()));
+                    strategies.add(strategyMap.get(TitleTagRecallStrategyV1.class.getSimpleName()));
                     strategies.addAll(getRegionRecallStrategy(param));
                 default:
                     strategies.addAll(getRegionRecallStrategy(param));
             }
             // 命中用户黑名单不走流量池
-            if (!hitUserBlacklist) {
+            if (!hitUserBlacklist || !isInBlacklist) {
                 if (Objects.equals(SUPPLY_AB_CODE, abCode)) {
                     // 供给流量池策略 648 实验 random
                     strategies.add(strategyMap.get(FlowPoolWithLevelSupplyRecallStrategy.class.getSimpleName()));
@@ -198,7 +212,7 @@ public class RecallService implements ApplicationContextAware {
                                 || "60105".equals(abCode) || "60106".equals(abCode) || "60107".equals(abCode)
                                 || "60113".equals(abCode) || "60114".equals(abCode)
                                 || "60115".equals(abCode) || "60117".equals(abCode) || "60118".equals(abCode)
-                                || "60119".equals(abCode)) {
+                                || "60119".equals(abCode) || "60150".equals(abCode) || "60151".equals(abCode)) {
                             strategies.add(strategyMap.get(FlowPoolWithLevelRecallStrategyTomson.class.getSimpleName()));
                         } else {
                             strategies.add(strategyMap.get(FlowPoolWithLevelRecallStrategy.class.getSimpleName()));
@@ -286,7 +300,8 @@ public class RecallService implements ApplicationContextAware {
                 case "60115": // 565
                 case "60117": // 567
                 case "60118": // 568
-                    if (!hitUserBlacklist) {
+                case "60150": // 645
+                    if (!hitUserBlacklist || !isInBlacklist) {
                         strategies.add(strategyMap.get(FlowPoolLastDayTopRecallStrategy.class.getSimpleName()));
                     }
                     strategies.add(strategyMap.get(SimHotVideoRecallStrategy.class.getSimpleName()));
@@ -298,7 +313,7 @@ public class RecallService implements ApplicationContextAware {
                     break;
                 case "60110": // 新内容的召回(流量池的Top内容)
                     strategies.add(strategyMap.get(TopGoodPerformanceVideoRecallStrategy.class.getSimpleName()));
-                    if (!hitUserBlacklist) {
+                    if (!hitUserBlacklist || !isInBlacklist) {
                         strategies.add(strategyMap.get(FlowPoolLastDayTopRecallStrategy.class.getSimpleName()));
                     }
                     strategies.add(strategyMap.get(SimHotVideoRecallStrategy.class.getSimpleName()));

+ 91 - 0
recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/recall/strategy/ContentBaseRecallStrategy.java

@@ -0,0 +1,91 @@
+package com.tzld.piaoquan.recommend.server.service.recall.strategy;
+
+import com.alibaba.fastjson.JSONObject;
+import com.google.common.collect.Lists;
+import com.tzld.piaoquan.recommend.server.model.Video;
+import com.tzld.piaoquan.recommend.server.service.filter.FilterParam;
+import com.tzld.piaoquan.recommend.server.service.filter.FilterResult;
+import com.tzld.piaoquan.recommend.server.service.filter.RegionFilterService;
+import com.tzld.piaoquan.recommend.server.service.recall.FilterParamFactory;
+import com.tzld.piaoquan.recommend.server.service.recall.RecallParam;
+import com.tzld.piaoquan.recommend.server.service.recall.RecallStrategy;
+import com.tzld.piaoquan.recommend.server.service.score.ScorerUtils;
+import com.tzld.piaoquan.recommend.server.service.score4recall.ScorerPipeline4Recall;
+import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.lang3.tuple.Pair;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.Resource;
+import java.util.*;
+
+/**
+ * @author zhangbo
+ */
+@Component
+public class ContentBaseRecallStrategy implements RecallStrategy {
+
+    public static final String PUSH_FORM = "content_base_recall_strategy";
+
+    private static final String VIDEO_2_TAG_REDIS_KEY = "content:base:video:tags:";
+
+    @Value("${recommend.content.base.recall.strategy.limit:50}")
+    private Integer limit;
+    @Resource
+    private RegionFilterService filterService;
+    @Resource
+    private RedisTemplate<String, String> redisTemplate;
+
+    @Override
+    public List<Video> recall(RecallParam param) {
+        Long videoId = param.getVideoId();
+        Map<String, String> param4Model = new HashMap<>(1);
+        String redisKey = VIDEO_2_TAG_REDIS_KEY + videoId;
+        String tags = redisTemplate.opsForValue().get(redisKey);
+        try {
+            String tagStr = JSONObject.parseObject(tags).getString("tags");
+            param4Model.put("tags", tagStr);
+        } catch (Exception e) {
+            return Collections.emptyList();
+        }
+        // 2 通过model拿到召回list
+        ScorerPipeline4Recall pipeline = ScorerUtils.getScorerPipeline4Recall("content_base_recall.conf");
+        List<List<Pair<Long, Double>>> results = pipeline.recall(param4Model);
+        List<Pair<Long, Double>> result = results.get(0);
+        for (int i = 1; i < results.size(); ++i) {
+            result.addAll(results.get(i));
+        }
+        Map<Long, Double> videoMap = new LinkedHashMap<>();
+        for (Pair<Long, Double> v : result) {
+            videoMap.put(v.getLeft(), v.getRight());
+        }
+        FilterParam filterParam = FilterParamFactory.create(param, Lists.newArrayList(videoMap.keySet()));
+        filterParam.setForceTruncation(10000);
+        filterParam.setConcurrent(true);
+        filterParam.setNotUsePreView(false);
+        FilterResult filterResult = filterService.filter(filterParam);
+        List<Video> videosResult = new ArrayList<>();
+        if (filterResult != null && CollectionUtils.isNotEmpty(filterResult.getVideoIds())) {
+            filterResult.getVideoIds().stream()
+                    // 按照 rovScore 倒序排序
+                    .sorted(Comparator.comparing(vid -> videoMap.getOrDefault(vid, 0.0)).reversed())
+                    .limit(limit).forEach(vid -> {
+                        Video video = new Video();
+                        video.setVideoId(vid);
+                        video.setAbCode(param.getAbCode());
+                        video.setRovScore(videoMap.get(vid));
+                        video.setPushFrom(pushFrom());
+                        videosResult.add(video);
+                    });
+        }
+        return videosResult;
+    }
+
+    @Override
+    public String pushFrom() {
+        return PUSH_FORM;
+    }
+
+
+}

+ 240 - 0
recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/recall/strategy/TitleTagRecallStrategyV1.java

@@ -0,0 +1,240 @@
+package com.tzld.piaoquan.recommend.server.service.recall.strategy;
+
+import com.google.common.reflect.TypeToken;
+import com.tzld.piaoquan.recommend.server.model.Video;
+import com.tzld.piaoquan.recommend.server.service.filter.FilterParam;
+import com.tzld.piaoquan.recommend.server.service.filter.FilterResult;
+import com.tzld.piaoquan.recommend.server.service.filter.RegionFilterService;
+import com.tzld.piaoquan.recommend.server.service.recall.FilterParamFactory;
+import com.tzld.piaoquan.recommend.server.service.recall.RecallParam;
+import com.tzld.piaoquan.recommend.server.service.recall.RecallStrategy;
+import com.tzld.piaoquan.recommend.server.util.JSONUtils;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.collections4.CollectionUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.stereotype.Component;
+
+import java.util.*;
+import java.util.stream.Collectors;
+
+/**
+ * @author zhangbo
+ */
+@Component
+@Slf4j
+public class TitleTagRecallStrategyV1 implements RecallStrategy {
+    private final String CLASS_NAME = this.getClass().getSimpleName();
+    @Autowired
+    private RegionFilterService filterService;
+    @Autowired
+    @Qualifier("redisTemplate")
+    public RedisTemplate<String, String> redisTemplate;
+    @Override
+    public List<Video> recall(RecallParam param) {
+        long t0 = System.currentTimeMillis();
+        List<Video> result = new ArrayList<>();
+        // 1 获取头部vid,请求redis得到tag。
+        Long headVid = param.getVideoId();
+        String key1 = "redis:vid_title_tags:" + headVid;
+        String value1 = redisTemplate.opsForValue().get(key1);
+        if (value1 == null || value1.isEmpty()){
+            return result;
+        }
+        Map<String, String> vfMap = new HashMap<>();
+        vfMap = JSONUtils.fromJson(value1, new TypeToken<Map<String, String>>() {}, vfMap);
+        String tags = vfMap.getOrDefault("tags","");
+        if (tags.isEmpty()){
+            return result;
+        }
+        String title = vfMap.getOrDefault("title","");
+        // 2 通过tags请求redis,得到list。
+        List<String> key2 = Arrays.stream(tags.split(",")).map(r -> "redis:tag2vids:"+r).collect(Collectors.toList());
+        List<String> value2 = redisTemplate.opsForValue().multiGet(key2);
+        if (value2 == null || value2.isEmpty()){
+            return result;
+        }
+        // 3 合并多个tag的结果。
+        Map<Long, VideoTmp> vid2Info = new HashMap<>();
+        for (String v2: value2){
+            if (v2 == null || v2.isEmpty()){
+                continue;
+            }
+            vfMap = new HashMap<>();
+            vfMap = JSONUtils.fromJson(v2, new TypeToken<Map<String, String>>() {}, vfMap);
+            List<Long> vids = new ArrayList<>();
+            List<Double> scores = new ArrayList<>();
+            try{
+                vids = Arrays.stream(vfMap.getOrDefault("videoid_arr", "").split(","))
+                        .filter(s -> !s.trim().isEmpty() && s.matches("-?\\d+"))
+                        .map(Long::valueOf).limit(20).collect(Collectors.toList());
+                scores = Arrays.stream(vfMap.getOrDefault("score_arr", "").split(","))
+                        .map(Double::valueOf).limit(20).collect(Collectors.toList());
+            }catch(Exception e){
+                log.error(String.format("json parse is wrong in {}, key={}, error={}", CLASS_NAME, v2, e));
+                vids = new ArrayList<>();
+                scores = new ArrayList<>();
+            }
+            if (vids.size() != scores.size() || vids.isEmpty()){
+                continue;
+            }
+            for (int i = 0; i < vids.size(); ++i){
+                Long id = vids.get(i);
+                if (id.equals(headVid)){
+                    continue;
+                }
+                Double score = scores.get(i);
+                if (vid2Info.containsKey(id)){
+                    VideoTmp videoTmp = vid2Info.get(id);
+                    videoTmp.cnt += 1;
+                    videoTmp.score += score;
+                    vid2Info.put(id, videoTmp);
+                }else{
+                    VideoTmp videoTmp = new VideoTmp();
+                    videoTmp.id = id;
+                    videoTmp.cnt = 1;
+                    videoTmp.score = score;
+                    vid2Info.put(id, videoTmp);
+                }
+            }
+        }
+        if (vid2Info.isEmpty()){
+            return result;
+        }
+
+        List<Map.Entry<Long, VideoTmp>> entries = new ArrayList<>(vid2Info.entrySet());
+        entries.sort(new Comparator<Map.Entry<Long, VideoTmp>>() {
+            @Override
+            public int compare(Map.Entry<Long, VideoTmp> o1, Map.Entry<Long, VideoTmp> o2) {
+                // 先比较cnt,如果cnt相同则比较score,如果score也相同则比较id
+                int cmpCnt = Integer.compare(o2.getValue().cnt, o1.getValue().cnt); // 从大到小
+                if (cmpCnt != 0) {
+                    return cmpCnt;
+                }
+                int cmpScore = Double.compare(o2.getValue().score, o1.getValue().score); // 从大到小
+                if (cmpScore != 0) {
+                    return cmpScore;
+                }
+                return Long.compare(o2.getKey(), o1.getKey()); // 从大到小
+            }
+        });
+        // 现在entries是按照cnt、score、id从大到小排序的,提取key到List<Long>
+        List<Long> sortedKeys = new ArrayList<>();
+        for (Map.Entry<Long, VideoTmp> entry : entries) {
+            sortedKeys.add(entry.getKey());
+        }
+
+        // 4 集体走title匹配过滤,推荐过滤。
+        List<Long> deleteKey = new ArrayList<>();
+        if (!title.isEmpty()){
+            List<String> key3 = sortedKeys.stream().map(r -> "redis:vid_title_tags:"+r).collect(Collectors.toList());
+            List<String> value3 = redisTemplate.opsForValue().multiGet(key3);
+            if (value3 != null && !value3.isEmpty()){
+                int j = 0;
+                for (String v3 : value3){
+                    vfMap = new HashMap<>();
+                    vfMap = JSONUtils.fromJson(v3, new TypeToken<Map<String, String>>() {}, vfMap);
+                    String titleTmp = vfMap.getOrDefault("title","");
+                    if (!titleTmp.isEmpty() && calculateStringSimilarity(title, titleTmp) >= 0.9){
+                        deleteKey.add(sortedKeys.get(j));
+                    }
+                    j++;
+                }
+            }
+        }
+        sortedKeys.removeIf(deleteKey::contains);
+        if (sortedKeys.isEmpty()){
+            return result;
+        }
+
+        FilterParam filterParam = FilterParamFactory.create(param, sortedKeys);
+        filterParam.setForceTruncation(10000);
+        filterParam.setConcurrent(true);
+        filterParam.setNotUsePreView(false);
+        FilterResult filterResult = filterService.filter(filterParam);
+        List<Video> videosResult = new ArrayList<>();
+        if (filterResult != null && CollectionUtils.isNotEmpty(filterResult.getVideoIds())) {
+            filterResult.getVideoIds().forEach(vid -> {
+                Video video = new Video();
+                video.setVideoId(vid);
+                video.setAbCode(param.getAbCode());
+                double score = 0.0;
+                if (vid2Info.containsKey(vid)){
+                    VideoTmp videoTmp = vid2Info.get(vid);
+                    score = videoTmp.score / videoTmp.cnt;
+                }
+                video.setRovScore(score);
+                video.setPushFrom(pushFrom());
+                videosResult.add(video);
+            });
+        }
+
+        // 5 内部日志打印
+
+        // 6 返回结果
+        return videosResult;
+    }
+
+    public static final String PUSH_FORM = "recall_strategy_title_tag";
+
+    @Override
+    public String pushFrom() {
+        return PUSH_FORM;
+    }
+
+    static class VideoTmp{
+        Long id;
+        Integer cnt;
+        Double score;
+    }
+
+    public static int calculateLevenshteinDistance(String s1, String s2) {
+        if (s1 == null || s2 == null) {
+            throw new IllegalArgumentException("Strings must not be null");
+        }
+
+        int len0 = s1.length() + 1;
+        int len1 = s2.length() + 1;
+
+        // 创建一个二维数组来保存已经计算过的子问题的解
+        int[][] dp = new int[len0][len1];
+
+        // 初始化第一行和第一列
+        for (int i = 0; i < len0; i++) {
+            dp[i][0] = i;
+        }
+        for (int j = 0; j < len1; j++) {
+            dp[0][j] = j;
+        }
+
+        // 动态规划计算Levenshtein距离
+        for (int i = 1; i < len0; i++) {
+            for (int j = 1; j < len1; j++) {
+                int cost = (s1.charAt(i - 1) == s2.charAt(j - 1)) ? 0 : 1;
+                dp[i][j] = Math.min(
+                        Math.min(dp[i - 1][j] + 1, dp[i][j - 1] + 1),
+                        dp[i - 1][j - 1] + cost
+                );
+            }
+        }
+
+        return dp[len0 - 1][len1 - 1];
+    }
+
+    // 计算字符串匹配度(0到1之间)
+    public static double calculateStringSimilarity(String s1, String s2) {
+        int distance = calculateLevenshteinDistance(s1, s2);
+        int maxLength = Math.max(s1.length(), s2.length());
+        // 如果maxLength为0,则两个字符串都是空的,返回1表示完全匹配
+        return maxLength == 0 ? 1.0 : 1.0 - ((double) distance / maxLength);
+    }
+
+    public static void main(String[] args) {
+        String s1 = "老年人都来看看那";
+        String s2 = "老年人都来看看";
+        double score = calculateStringSimilarity(s1, s2);
+        System.out.println(score);
+    }
+
+}

+ 1 - 0
recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/score/ScorerUtils.java

@@ -46,6 +46,7 @@ public final class ScorerUtils {
         ScorerUtils.init4Recall("feeds_recall_config_tomson_v2.conf");
         ScorerUtils.init4Recall("feeds_score_config_share_width.conf");
         ScorerUtils.init4Recall("feeds_score_config_share_deep.conf");
+        ScorerUtils.init4Recall("content_base_recall.conf");
     }
 
     private ScorerUtils() {

+ 44 - 0
recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/score4recall/model4recall/VideoTagModel4RecallMap.java

@@ -0,0 +1,44 @@
+package com.tzld.piaoquan.recommend.server.service.score4recall.model4recall;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+
+public class VideoTagModel4RecallMap extends AbstractModel {
+    private static final Logger LOGGER = LoggerFactory.getLogger(VideoTagModel4RecallMap.class);
+    public Map<String, List<Long>> map = new HashMap<>();
+
+    @Override
+    public boolean loadFromStream(InputStreamReader in) throws IOException {
+        BufferedReader input = new BufferedReader(in);
+        String line;
+        while ((line = input.readLine()) != null) {
+            String[] items = line.split("\t");
+            if (items.length < 2) {
+                continue;
+            }
+            String videoIds = items[1].trim();
+            try {
+                String[] videoIdArr = videoIds.split(",");
+                List<Long> videoIdList = new ArrayList<>();
+                for (String videoId : videoIdArr) {
+                    videoIdList.add(Long.parseLong(videoId));
+                }
+                map.put(items[0], videoIdList);
+            } catch (Exception e) {
+                LOGGER.error(String.format("VideoTagModel4RecallMap is wrong with parse %s: ", line), e);
+            }
+        }
+        input.close();
+        in.close();
+        return true;
+    }
+}

+ 59 - 0
recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/score4recall/strategy/ContentBaseRecallScore.java

@@ -0,0 +1,59 @@
+package com.tzld.piaoquan.recommend.server.service.score4recall.strategy;
+
+import com.tzld.piaoquan.recommend.server.service.score.ScorerConfigInfo;
+import com.tzld.piaoquan.recommend.server.service.score4recall.AbstractScorer4Recall;
+import com.tzld.piaoquan.recommend.server.service.score4recall.model4recall.Model4RecallKeyValue;
+import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.collections4.MapUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.tuple.Pair;
+
+import java.util.*;
+import java.util.stream.Collectors;
+
+
+public class ContentBaseRecallScore extends AbstractScorer4Recall {
+
+    public ContentBaseRecallScore(ScorerConfigInfo configInfo) {
+        super(configInfo);
+    }
+
+    @Override
+    public void loadModel() {
+        doLoadModel(Model4RecallKeyValue.class);
+    }
+
+    @Override
+    public List<Pair<Long, Double>> recall(Map<String, String> params) {
+        Model4RecallKeyValue model = (Model4RecallKeyValue) this.getModel();
+        if (model == null || MapUtils.isEmpty(model.kv)) {
+            return Collections.emptyList();
+        }
+        String tags = params.get("tags");
+        if (StringUtils.isBlank(tags)) {
+            return Collections.emptyList();
+        }
+        List<String> tagList = Arrays.stream(tags.split(",")).collect(Collectors.toList());
+        List<Pair<Long, Double>> result = new ArrayList<>();
+        for (String tag : tagList) {
+            List<Pair<Long, Double>> videoAndScores = model.kv.get(tag);
+            if (CollectionUtils.isNotEmpty(videoAndScores)) {
+                result.addAll(videoAndScores);
+            }
+        }
+        // 结果去重
+        Set<Long> videoIdSet = new HashSet<>();
+        List<Pair<Long, Double>> distinctResult = new ArrayList<>();
+        for (Pair<Long, Double> pair : result) {
+            if (pair.getLeft() == null || pair.getRight() == null) {
+                continue;
+            }
+            if (videoIdSet.add(pair.getLeft())) {
+                distinctResult.add(pair);
+            }
+        }
+        return distinctResult;
+    }
+
+
+}

+ 7 - 0
recommend-server-service/src/main/resources/content_base_recall.conf

@@ -0,0 +1,7 @@
+scorer-config = {
+  content-score-config = {
+    scorer-name = "com.tzld.piaoquan.recommend.server.service.score4recall.strategy.ContentBaseRecallScore"
+    scorer-priority = 100
+    model-path = "alg_recall_file/content_base_recall.txt"
+  }
+}