Browse Source

Merge branch 'feature/564_568_add_mergecate2_reduce_score' of algorithm/recommend-server into master

jiachanghui 1 tháng trước cách đây
mục cha
commit
26b6a49df7

+ 91 - 1
recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/rank/strategy/RankStrategy4RegionMergeModelV564.java

@@ -14,13 +14,16 @@ import com.tzld.piaoquan.recommend.server.service.rank.tansform.FeatureV6;
 import com.tzld.piaoquan.recommend.server.service.recall.strategy.*;
 import com.tzld.piaoquan.recommend.server.service.score.ScorerUtils;
 import com.tzld.piaoquan.recommend.server.util.CommonCollectionUtils;
+import com.tzld.piaoquan.recommend.server.util.DateUtils;
 import com.tzld.piaoquan.recommend.server.util.FeatureBucketUtils;
 import com.tzld.piaoquan.recommend.server.util.JSONUtils;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.collections4.MapUtils;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.time.LocalDateTime;
 import java.util.*;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
@@ -32,12 +35,17 @@ public class RankStrategy4RegionMergeModelV564 extends RankStrategy4RegionMergeM
     @ApolloJsonValue("${rank.score.merge.weightv564:}")
     private Map<String, Double> mergeWeight;
 
+    @ApolloJsonValue("${rank.score.reduce.config.v564:}")
+    private Map<String, List<Map<String, String>>> reduceConfigMap;
+
     @Autowired
     private FeatureService featureService;
 
     @Override
     public List<Video> mergeAndRankRovRecall(RankParam param) {
         Map<String, Double> mergeWeight = this.mergeWeight != null ? this.mergeWeight : new HashMap<>(0);
+        Map<String, List<Map<String, String>>> reduceConfigMap = this.reduceConfigMap != null ? this.reduceConfigMap : new HashMap<>(0);
+
         //-------------------融-------------------
         //-------------------合-------------------
         //-------------------逻-------------------
@@ -145,7 +153,12 @@ public class RankStrategy4RegionMergeModelV564 extends RankStrategy4RegionMergeM
             double newNorXGBScore = norPowerCalibration(xgbNorPowerWeight, xgbNorPowerExp, norXGBScore);
             double vor = Double.parseDouble(vid2MapFeature.getOrDefault(item.getVideoId() + "", new HashMap<>()).getOrDefault("vor", "0"));
             item.getScoresMap().put("vor", vor);
-            score = fmRov * (0.1 + newNorXGBScore) * (0.1 + vor);
+
+            String vidStr = String.valueOf(item.getVideo().getVideoId());
+            String mergeCate2 = this.parseMergeCate2(vidStr, featureOriginVideo);
+            double reduceCoefficient = this.parseReduceCoefficient(mergeCate2, reduceConfigMap);
+            score = fmRov * (0.1 + newNorXGBScore) * (0.1 + vor) * reduceCoefficient;
+
             Video video = item.getVideo();
             video.setScore(score);
             video.setSortScore(score);
@@ -344,4 +357,81 @@ public class RankStrategy4RegionMergeModelV564 extends RankStrategy4RegionMergeM
             setVideo.addAll(list.stream().map(Video::getVideoId).collect(Collectors.toSet()));
         }
     }
+
+    public double parseReduceCoefficient(String mergeCate2, Map<String, List<Map<String, String>>> reduceConfigMap) {
+        if (StringUtils.isBlank(mergeCate2) || MapUtils.isEmpty(reduceConfigMap)) {
+            return 1d;
+        }
+
+        List<Map<String, String>> configMaps = reduceConfigMap.getOrDefault(mergeCate2, new ArrayList<>());
+        for (Map<String, String> configMap : configMaps) {
+            boolean currentHourIsNeedReduce = currentHourIsNeedReduce(configMap);
+            boolean currentDateIsNeedReduce = currentDateIsNeedReduce(configMap);
+            if (currentHourIsNeedReduce || currentDateIsNeedReduce) {
+                return Double.parseDouble(configMap.getOrDefault("reduce_coefficient", "1"));
+            }
+        }
+
+        return 1d;
+    }
+
+
+    /**
+     * 当前小时是否需要降权
+     */
+    private boolean currentHourIsNeedReduce(Map<String, String> configMap) {
+        if (!configMap.containsKey("reduce_hour")) {
+            return false;
+        }
+        try {
+
+            int currentHour = LocalDateTime.now().getHour();
+            String[] reduceHours = configMap.get("reduce_hour").split(",");
+            for (String hourRange : reduceHours) {
+                String[] split = hourRange.split("-");
+                int h1 = Integer.parseInt(split[0]);
+                int h2 = (split.length == 2) ? Integer.parseInt(split[1]) : h1;
+
+                if (currentHour >= h1 && currentHour <= h2) {
+                    return true;
+                }
+            }
+        } catch (Exception e) {
+            log.error("564 error parse reduce hour config error. config content: {}, \n", configMap, e);
+        }
+        return false;
+    }
+
+    /**
+     * 当前时间段是否需要降权
+     */
+    private boolean currentDateIsNeedReduce(Map<String, String> configMap) {
+        if (!configMap.containsKey("reduce_date")) {
+            return false;
+        }
+
+        String[] reduceDates = configMap.get("reduce_date").split(",");
+        for (String s : reduceDates) {
+            if (DateUtils.ifTimeRangeInNow(s)) {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
+
+    private String parseMergeCate2(String vidStr, Map<String, Map<String, Map<String, String>>> featureMap) {
+        if (!featureMap.containsKey(vidStr)) {
+            return "";
+        }
+
+        Map<String, Map<String, String>> vidFeature = featureMap.get(vidStr);
+        if (!vidFeature.containsKey("alg_vid_feature_basic_info")) {
+            return "";
+        }
+
+        Map<String, String> basicInfoMap = vidFeature.get("alg_vid_feature_basic_info");
+        return basicInfoMap.getOrDefault("merge_second_level_cate", "");
+    }
 }

+ 91 - 1
recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/rank/strategy/RankStrategy4RegionMergeModelV568.java

@@ -14,13 +14,16 @@ import com.tzld.piaoquan.recommend.server.service.rank.tansform.FeatureV6;
 import com.tzld.piaoquan.recommend.server.service.recall.strategy.*;
 import com.tzld.piaoquan.recommend.server.service.score.ScorerUtils;
 import com.tzld.piaoquan.recommend.server.util.CommonCollectionUtils;
+import com.tzld.piaoquan.recommend.server.util.DateUtils;
 import com.tzld.piaoquan.recommend.server.util.FeatureBucketUtils;
 import com.tzld.piaoquan.recommend.server.util.JSONUtils;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.collections4.MapUtils;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.time.LocalDateTime;
 import java.util.*;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
@@ -32,12 +35,17 @@ public class RankStrategy4RegionMergeModelV568 extends RankStrategy4RegionMergeM
     @ApolloJsonValue("${rank.score.merge.weightv568:}")
     private Map<String, Double> mergeWeight;
 
+    @ApolloJsonValue("${rank.score.reduce.config.v568:}")
+    private Map<String, List<Map<String, String>>> reduceConfigMap;
+
     @Autowired
     private FeatureService featureService;
 
     @Override
     public List<Video> mergeAndRankRovRecall(RankParam param) {
         Map<String, Double> mergeWeight = this.mergeWeight != null ? this.mergeWeight : new HashMap<>(0);
+        Map<String, List<Map<String, String>>> reduceConfigMap = this.reduceConfigMap != null ? this.reduceConfigMap : new HashMap<>(0);
+
         //-------------------融-------------------
         //-------------------合-------------------
         //-------------------逻-------------------
@@ -145,7 +153,12 @@ public class RankStrategy4RegionMergeModelV568 extends RankStrategy4RegionMergeM
             double newNorXGBScore = norPowerCalibration(xgbNorPowerWeight, xgbNorPowerExp, norXGBScore);
             double vor = Double.parseDouble(vid2MapFeature.getOrDefault(item.getVideoId() + "", new HashMap<>()).getOrDefault("vor", "0"));
             item.getScoresMap().put("vor", vor);
-            score = fmRov * (0.1 + hasReturnRovScore) * (0.1 + vor);
+
+            String vidStr = String.valueOf(item.getVideo().getVideoId());
+            String mergeCate2 = this.parseMergeCate2(vidStr, featureOriginVideo);
+            double reduceCoefficient = this.parseReduceCoefficient(mergeCate2, reduceConfigMap);
+            score = fmRov * (0.1 + hasReturnRovScore) * (0.1 + vor) * reduceCoefficient;
+
             Video video = item.getVideo();
             video.setScore(score);
             video.setSortScore(score);
@@ -344,4 +357,81 @@ public class RankStrategy4RegionMergeModelV568 extends RankStrategy4RegionMergeM
             setVideo.addAll(list.stream().map(Video::getVideoId).collect(Collectors.toSet()));
         }
     }
+
+    public double parseReduceCoefficient(String mergeCate2, Map<String, List<Map<String, String>>> reduceConfigMap) {
+        if (StringUtils.isBlank(mergeCate2) || MapUtils.isEmpty(reduceConfigMap)) {
+            return 1d;
+        }
+
+        List<Map<String, String>> configMaps = reduceConfigMap.getOrDefault(mergeCate2, new ArrayList<>());
+        for (Map<String, String> configMap : configMaps) {
+            boolean currentHourIsNeedReduce = currentHourIsNeedReduce(configMap);
+            boolean currentDateIsNeedReduce = currentDateIsNeedReduce(configMap);
+            if (currentHourIsNeedReduce || currentDateIsNeedReduce) {
+                return Double.parseDouble(configMap.getOrDefault("reduce_coefficient", "1"));
+            }
+        }
+
+        return 1d;
+    }
+
+
+    /**
+     * 当前小时是否需要降权
+     */
+    private boolean currentHourIsNeedReduce(Map<String, String> configMap) {
+        if (!configMap.containsKey("reduce_hour")) {
+            return false;
+        }
+        try {
+
+            int currentHour = LocalDateTime.now().getHour();
+            String[] reduceHours = configMap.get("reduce_hour").split(",");
+            for (String hourRange : reduceHours) {
+                String[] split = hourRange.split("-");
+                int h1 = Integer.parseInt(split[0]);
+                int h2 = (split.length == 2) ? Integer.parseInt(split[1]) : h1;
+
+                if (currentHour >= h1 && currentHour <= h2) {
+                    return true;
+                }
+            }
+        } catch (Exception e) {
+            log.error("564 error parse reduce hour config error. config content: {}, \n", configMap, e);
+        }
+        return false;
+    }
+
+    /**
+     * 当前时间段是否需要降权
+     */
+    private boolean currentDateIsNeedReduce(Map<String, String> configMap) {
+        if (!configMap.containsKey("reduce_date")) {
+            return false;
+        }
+
+        String[] reduceDates = configMap.get("reduce_date").split(",");
+        for (String s : reduceDates) {
+            if (DateUtils.ifTimeRangeInNow(s)) {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
+
+    private String parseMergeCate2(String vidStr, Map<String, Map<String, Map<String, String>>> featureMap) {
+        if (!featureMap.containsKey(vidStr)) {
+            return "";
+        }
+
+        Map<String, Map<String, String>> vidFeature = featureMap.get(vidStr);
+        if (!vidFeature.containsKey("alg_vid_feature_basic_info")) {
+            return "";
+        }
+
+        Map<String, String> basicInfoMap = vidFeature.get("alg_vid_feature_basic_info");
+        return basicInfoMap.getOrDefault("merge_second_level_cate", "");
+    }
 }