丁云鹏 1 年之前
父節點
當前提交
624ced5da9

+ 125 - 93
recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/rank/strategy/RankStrategy4RegionMergeModelV2.java

@@ -1,7 +1,9 @@
 package com.tzld.piaoquan.recommend.server.service.rank.strategy;
+
 import java.util.stream.Collectors;
 import java.util.Arrays;
 import java.util.Map;
+
 import com.alibaba.fastjson.JSONObject;
 import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValue;
 import com.google.common.reflect.TypeToken;
@@ -37,6 +39,7 @@ import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.data.redis.serializer.StringRedisSerializer;
 import org.springframework.stereotype.Service;
 import com.tzld.piaoquan.recommend.server.service.rank.extractor.ExtractorUtils;
+
 import java.text.SimpleDateFormat;
 import java.util.*;
 import java.util.stream.Collectors;
@@ -51,8 +54,9 @@ public class RankStrategy4RegionMergeModelV2 extends RankService {
     @ApolloJsonValue("${rank.score.merge.weightv2:}")
     private Map<String, Double> mergeWeight;
     @ApolloJsonValue("${RankStrategy4DensityFilterV2:}")
-    private Map<String,Map<String, Map<String, String>>> filterRules = new HashMap<>();
+    private Map<String, Map<String, Map<String, String>>> filterRules = new HashMap<>();
     final private String CLASS_NAME = this.getClass().getSimpleName();
+
     @Override
     public List<Video> mergeAndRankFlowPoolRecall(RankParam param) {
         List<Video> quickFlowPoolVideos = sortFlowPoolByThompson(param, FlowPoolConstants.QUICK_PUSH_FORM);
@@ -62,6 +66,7 @@ public class RankStrategy4RegionMergeModelV2 extends RankService {
             return sortFlowPoolByThompson(param, FlowPoolConstants.PUSH_FORM);
         }
     }
+
     public List<Video> sortFlowPoolByThompson(RankParam param, String pushFrom) {
 
         //初始化 userid
@@ -98,20 +103,22 @@ public class RankStrategy4RegionMergeModelV2 extends RankService {
             return v;
         });
     }
-    public void duplicate(Set<Long> setVideo, List<Video> videos){
+
+    public void duplicate(Set<Long> setVideo, List<Video> videos) {
         Iterator<Video> iterator = videos.iterator();
-        while(iterator.hasNext()){
+        while (iterator.hasNext()) {
             Video v = iterator.next();
-            if (setVideo.contains(v.getVideoId())){
+            if (setVideo.contains(v.getVideoId())) {
                 iterator.remove();
-            }else{
+            } else {
                 setVideo.add(v.getVideoId());
             }
         }
     }
+
     @Override
     public List<Video> mergeAndRankRovRecall(RankParam param) {
-        Map<String, Double> mergeWeight = this.mergeWeight != null? this.mergeWeight: new HashMap<>(0);
+        Map<String, Double> mergeWeight = this.mergeWeight != null ? this.mergeWeight : new HashMap<>(0);
         //-------------------融-------------------
         //-------------------合-------------------
         //-------------------逻-------------------
@@ -160,8 +167,6 @@ public class RankStrategy4RegionMergeModelV2 extends RankService {
         rovRecallRank.addAll(v7.subList(0, Math.min(mergeWeight.getOrDefault("v7", 10.0).intValue(), v7.size())));
 
 
-
-
         //-------------------排-------------------
         //-------------------序-------------------
         //-------------------逻-------------------
@@ -176,19 +181,19 @@ public class RankStrategy4RegionMergeModelV2 extends RankService {
         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){
+        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){
+        for (int i = 0; i < 24; ++i) {
             datehours.add(cur);
             cur = ExtractorUtils.subtractHours(cur, 1);
         }
-        for (RankItem item : items){
+        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");
@@ -246,10 +251,10 @@ public class RankStrategy4RegionMergeModelV2 extends RankService {
         double g = mergeWeight.getOrDefault("g", 1.0);
         double h = mergeWeight.getOrDefault("h", 20.0);
         double ifAdd = mergeWeight.getOrDefault("ifAdd", 1.0);
-        for (RankItem item : items){
-            double trendScore =  item.scoresMap.getOrDefault("trendScore", 0.0) > 1E-8 ?
+        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 ?
+            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);
@@ -257,16 +262,16 @@ public class RankStrategy4RegionMergeModelV2 extends RankService {
             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){
+            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 {
+                        (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);
+                        (newVideoScore > 1E-8 ? d * trendScore * (e + newVideoScore) : 0.0);
 
             }
             double allreturnsScore = item.scoresMap.getOrDefault("allreturnsScore", 0.0);
-            if (allreturnsScore > h){
+            if (allreturnsScore > h) {
                 score += (f * share2allreturnScore + g * view2allreturnScore);
             }
             Video video = item.getVideo();
@@ -279,46 +284,50 @@ public class RankStrategy4RegionMergeModelV2 extends RankService {
         Collections.sort(result, Comparator.comparingDouble(o -> -o.getSortScore()));
         return result;
     }
-    public double calNewVideoScore(Map<String, String> itemBasicMap){
+
+    public double calNewVideoScore(Map<String, String> itemBasicMap) {
         double existenceDays = Double.valueOf(itemBasicMap.getOrDefault("existence_days", "30"));
-        if (existenceDays > 5){
+        if (existenceDays > 5) {
             return 0.0;
         }
         double score = 1.0 / (existenceDays + 10.0);
         return score;
     }
-    public double calTrendScore(List<Double> data){
+
+    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);
+        for (int i = 0; i < size - 4; ++i) {
+            sum += data.get(i) - data.get(i + 4);
         }
-        if (sum * 10 > 0.6){
+        if (sum * 10 > 0.6) {
             sum = 0.6;
-        }else{
+        } else {
             sum = sum * 10;
         }
-        if (sum > 0){
+        if (sum > 0) {
             // 为了打断点
             sum = sum;
         }
         return sum;
     }
-    public Double calScoreWeight(List<Double> data){
+
+    public Double calScoreWeight(List<Double> data) {
         Double up = 0.0;
         Double down = 0.0;
-        for (int i=0; i<data.size(); ++i){
+        for (int i = 0; i < data.size(); ++i) {
             up += 1.0 / (i + 1) * data.get(i);
             down += 1.0 / (i + 1);
         }
-        return down > 1E-8? up / down: 0.0;
+        return down > 1E-8 ? up / down : 0.0;
     }
-    public List<Double> getRateData(List<Double> ups, List<Double> downs, Double up, Double down){
+
+    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)){
+        for (int i = 0; i < ups.size(); ++i) {
+            if (ExtractorUtils.isDoubleEqualToZero(downs.get(i) + down)) {
                 data.add(0.0);
-            }else{
+            } else {
                 data.add(
                         (ups.get(i) + up) / (downs.get(i) + down)
                 );
@@ -326,21 +335,23 @@ public class RankStrategy4RegionMergeModelV2 extends RankService {
         }
         return data;
     }
+
     public List<Double> getStaticData(Map<String, Map<String, Double>> itemRealMap,
-                                      List<String> datehours, String key){
+                                      List<String> datehours, String key) {
         List<Double> views = new LinkedList<>();
         Map<String, Double> tmp = itemRealMap.getOrDefault(key, new HashMap<>());
-        for (String dh : datehours){
+        for (String dh : datehours) {
             views.add(tmp.getOrDefault(dh, 0.0D) +
-                    (views.isEmpty() ? 0.0: views.get(views.size()-1))
+                    (views.isEmpty() ? 0.0 : views.get(views.size() - 1))
             );
         }
         return views;
     }
+
     public List<RankItem> model(List<Video> videos, RankParam param,
-                                List<String> rtFeaPart){
+                                List<String> rtFeaPart) {
         List<RankItem> result = new ArrayList<>();
-        if (videos.isEmpty()){
+        if (videos.isEmpty()) {
             return result;
         }
 
@@ -355,19 +366,20 @@ public class RankStrategy4RegionMergeModelV2 extends RankService {
         redisTemplate.afterPropertiesSet();
 
         // 0: 场景特征处理
-        Map<String, String> sceneFeatureMap =  this.getSceneFeature(param);
+        Map<String, String> sceneFeatureMap = this.getSceneFeature(param);
 
         // 1: user特征处理
         Map<String, String> userFeatureMap = new HashMap<>();
-        if (param.getMid() != null && !param.getMid().isEmpty()){
+        if (param.getMid() != null && !param.getMid().isEmpty()) {
             String midKey = "user_info_4video_" + param.getMid();
             String userFeatureStr = redisTemplate.opsForValue().get(midKey);
-            if (userFeatureStr != null){
-                try{
+            if (userFeatureStr != null) {
+                try {
                     userFeatureMap = JSONUtils.fromJson(userFeatureStr,
-                            new TypeToken<Map<String, String>>() {},
+                            new TypeToken<Map<String, String>>() {
+                            },
                             userFeatureMap);
-                }catch (Exception e){
+                } catch (Exception e) {
                     log.error(String.format("parse user json is wrong in {} with {}", this.CLASS_NAME, e));
                 }
             }
@@ -385,6 +397,22 @@ public class RankStrategy4RegionMergeModelV2 extends RankService {
             }
         }
 
+        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);
+//        log.info("userFeature in model = {}", JSONUtils.toJson(f1));
+
         // 2-1: item特征处理
         final Set<String> itemFeatureSet = new HashSet<>(Arrays.asList(
                 "total_time", "play_count_total",
@@ -394,18 +422,19 @@ public class RankStrategy4RegionMergeModelV2 extends RankService {
 
         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)
+        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){
+        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){
+                if (vF == null) {
                     continue;
                 }
-                try{
-                    vfMap = JSONUtils.fromJson(vF, new TypeToken<Map<String, String>>() {}, vfMap);
+                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();
@@ -424,7 +453,7 @@ public class RankStrategy4RegionMergeModelV2 extends RankService {
                     );
                     f4.putAll(f5);
                     rankItems.get(i).setFeatureMap(f4);
-                }catch (Exception e){
+                } catch (Exception e) {
                     log.error(String.format("parse video json is wrong in {} with {}", this.CLASS_NAME, e));
                 }
             }
@@ -437,80 +466,82 @@ public class RankStrategy4RegionMergeModelV2 extends RankService {
         String hour = new SimpleDateFormat("HH").format(calendar.getTime());
         String rtFeaPart1day = date + hour;
         String rtFeaPart1h = date + hour;
-        if (rtFeaPartKeyResult != null){
-            if (rtFeaPartKeyResult.get(0) != null){
+        if (rtFeaPartKeyResult != null) {
+            if (rtFeaPartKeyResult.get(0) != null) {
                 rtFeaPart1day = rtFeaPartKeyResult.get(0);
             }
-            if (rtFeaPartKeyResult.get(1) != null){
+            if (rtFeaPartKeyResult.get(1) != null) {
                 rtFeaPart1h = rtFeaPartKeyResult.get(1);
             }
         }
 
-        List<String> videoRtKeys1 = videoIds.stream().map(r-> "item_rt_fea_1day_" + r)
+        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)
+        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){
+        if (videoRtFeatures != null) {
             int j = 0;
-            for (RankItem item: rankItems){
+            for (RankItem item : rankItems) {
                 String vF = videoRtFeatures.get(j);
                 ++j;
-                if (vF == null){
+                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()){
+                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){
+                        if (value == null) {
                             continue;
                         }
-                        String [] var1 = value.split(",");
+                        String[] var1 = value.split(",");
                         Map<String, Double> tmp = new HashMap<>();
-                        for (String var2 : var1){
-                            String [] var3 = var2.split(":");
+                        for (String var2 : var1) {
+                            String[] var3 = var2.split(":");
                             tmp.put(var3[0], Double.valueOf(var3[1]));
                         }
                         vfMapNew.put(entry.getKey(), tmp);
                     }
-                }catch (Exception e){
+                } 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){
+            for (RankItem item : rankItems) {
                 String vF = videoRtFeatures.get(j);
                 ++j;
-                if (vF == null){
+                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);
+                try {
+                    vfMap = JSONUtils.fromJson(vF, new TypeToken<Map<String, String>>() {
+                    }, vfMap);
 
-                    for (Map.Entry<String, String> entry : vfMap.entrySet()){
+                    for (Map.Entry<String, String> entry : vfMap.entrySet()) {
                         String value = entry.getValue();
-                        if (value == null){
+                        if (value == null) {
                             continue;
                         }
-                        String [] var1 = value.split(",");
+                        String[] var1 = value.split(",");
                         Map<String, Double> tmp = new HashMap<>();
-                        for (String var2 : var1){
-                            String [] var3 = var2.split(":");
+                        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){
+                } 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);
@@ -522,7 +553,6 @@ public class RankStrategy4RegionMergeModelV2 extends RankService {
 //        log.info("ItemFeature = {}", JSONUtils.toJson(videoFeatures));
 
 
-
         List<RankItem> rovRecallScore = ScorerUtils.getScorerPipeline(ScorerUtils.BASE_CONF)
                 .scoring(sceneFeatureMap, userFeatureMap, rankItems);
 //        log.info("mergeAndRankRovRecallNew rovRecallScore={}", JSONUtils.toJson(rovRecallScore));
@@ -532,6 +562,7 @@ public class RankStrategy4RegionMergeModelV2 extends RankService {
 //        log.info(obj.toString());
         return rovRecallScore;
     }
+
     private Map<String, String> getSceneFeature(RankParam param) {
         Map<String, String> sceneFeatureMap = new HashMap<>();
         String provinceCn = param.getProvince();
@@ -548,8 +579,8 @@ public class RankStrategy4RegionMergeModelV2 extends RankService {
                 "吉林市".equals(city) |
                 "新竹市".equals(city) |
                 "嘉义市".equals(city)
-        ){
-        }else{
+        ) {
+        } else {
             city = city.replaceAll("市$", "");
         }
         sceneFeatureMap.put("ctx_city", city);
@@ -560,6 +591,7 @@ public class RankStrategy4RegionMergeModelV2 extends RankService {
 
         return sceneFeatureMap;
     }
+
     @Override
     public RankResult mergeAndSort(RankParam param, List<Video> rovVideos, List<Video> flowVideos) {
 
@@ -577,12 +609,12 @@ public class RankStrategy4RegionMergeModelV2 extends RankService {
         Map<String, Map<String, String>> rulesMap = this.filterRules.getOrDefault(abCode, new HashMap<>(0));
 
         //3 标签读取
-        if (rulesMap != null && !rulesMap.isEmpty()){
+        if (rulesMap != null && !rulesMap.isEmpty()) {
             RankExtractorItemTags extractorItemTags = new RankExtractorItemTags(this.redisTemplate);
             extractorItemTags.processor(rovVideos, flowVideos);
         }
         //6 合并结果时间卡控
-        if (rulesMap != null && !rulesMap.isEmpty()){
+        if (rulesMap != null && !rulesMap.isEmpty()) {
             RankProcessorTagFilter.processor(rovVideos, flowVideos, rulesMap);
         }
 
@@ -658,28 +690,28 @@ public class RankStrategy4RegionMergeModelV2 extends RankService {
         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";
 
         RankStrategy4RegionMergeModelV2 job = new RankStrategy4RegionMergeModelV2();
-        List<Double> l1 = job.getRateData(job.help(up1, "2024031115", 24),job.help(down1, "2024031115", 24),1.,10.);
+        List<Double> l1 = job.getRateData(job.help(up1, "2024031115", 24), job.help(down1, "2024031115", 24), 1., 10.);
         Double d1 = job.calScoreWeight(l1);
 
         System.out.println(d1);
 
-        List<Double> l2 = job.getRateData(job.help(up2, "2024031115", 24),job.help(down2, "2024031115", 24),1.,10.);
+        List<Double> l2 = job.getRateData(job.help(up2, "2024031115", 24), job.help(down2, "2024031115", 24), 1., 10.);
         Double d2 = job.calScoreWeight(l2);
 
         System.out.println(d2);
 
     }
 
-    List<Double> help(String s, String date, Integer h){
+    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])
-        ));
+                .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));
+        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);

+ 116 - 87
recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/rank/strategy/RankStrategy4RegionMergeModelV547.java

@@ -48,8 +48,9 @@ public class RankStrategy4RegionMergeModelV547 extends RankService {
     @ApolloJsonValue("${rank.score.merge.weightv547:}")
     private Map<String, Double> mergeWeight;
     @ApolloJsonValue("${RankStrategy4DensityFilterV2:}")
-    private Map<String,Map<String, Map<String, String>>> filterRules = new HashMap<>();
+    private Map<String, Map<String, Map<String, String>>> filterRules = new HashMap<>();
     final private String CLASS_NAME = this.getClass().getSimpleName();
+
     @Override
     public List<Video> mergeAndRankFlowPoolRecall(RankParam param) {
         List<Video> quickFlowPoolVideos = sortFlowPoolByThompson(param, FlowPoolConstants.QUICK_PUSH_FORM);
@@ -59,6 +60,7 @@ public class RankStrategy4RegionMergeModelV547 extends RankService {
             return sortFlowPoolByThompson(param, FlowPoolConstants.PUSH_FORM);
         }
     }
+
     public List<Video> sortFlowPoolByThompson(RankParam param, String pushFrom) {
 
         //初始化 userid
@@ -95,20 +97,22 @@ public class RankStrategy4RegionMergeModelV547 extends RankService {
             return v;
         });
     }
-    public void duplicate(Set<Long> setVideo, List<Video> videos){
+
+    public void duplicate(Set<Long> setVideo, List<Video> videos) {
         Iterator<Video> iterator = videos.iterator();
-        while(iterator.hasNext()){
+        while (iterator.hasNext()) {
             Video v = iterator.next();
-            if (setVideo.contains(v.getVideoId())){
+            if (setVideo.contains(v.getVideoId())) {
                 iterator.remove();
-            }else{
+            } else {
                 setVideo.add(v.getVideoId());
             }
         }
     }
+
     @Override
     public List<Video> mergeAndRankRovRecall(RankParam param) {
-        Map<String, Double> mergeWeight = this.mergeWeight != null? this.mergeWeight: new HashMap<>(0);
+        Map<String, Double> mergeWeight = this.mergeWeight != null ? this.mergeWeight : new HashMap<>(0);
         //-------------------融-------------------
         //-------------------合-------------------
         //-------------------逻-------------------
@@ -157,8 +161,6 @@ public class RankStrategy4RegionMergeModelV547 extends RankService {
         rovRecallRank.addAll(v7.subList(0, Math.min(mergeWeight.getOrDefault("v7", 10.0).intValue(), v7.size())));
 
 
-
-
         //-------------------排-------------------
         //-------------------序-------------------
         //-------------------逻-------------------
@@ -173,19 +175,19 @@ public class RankStrategy4RegionMergeModelV547 extends RankService {
         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){
+        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){
+        for (int i = 0; i < 24; ++i) {
             datehours.add(cur);
             cur = ExtractorUtils.subtractHours(cur, 1);
         }
-        for (RankItem item : items){
+        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");
@@ -243,10 +245,10 @@ public class RankStrategy4RegionMergeModelV547 extends RankService {
         double g = mergeWeight.getOrDefault("g", 2.0);
         double h = mergeWeight.getOrDefault("h", 240.0);
         double ifAdd = mergeWeight.getOrDefault("ifAdd", 1.0);
-        for (RankItem item : items){
-            double trendScore =  item.scoresMap.getOrDefault("trendScore", 0.0) > 1E-8 ?
+        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 ?
+            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);
@@ -254,16 +256,16 @@ public class RankStrategy4RegionMergeModelV547 extends RankService {
             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){
+            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 {
+                        (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);
+                        (newVideoScore > 1E-8 ? d * trendScore * (e + newVideoScore) : 0.0);
 
             }
             double allreturnsScore = item.scoresMap.getOrDefault("allreturnsScore", 0.0);
-            if (allreturnsScore > h){
+            if (allreturnsScore > h) {
                 score += (f * share2allreturnScore + g * view2allreturnScore);
             }
             Video video = item.getVideo();
@@ -276,46 +278,50 @@ public class RankStrategy4RegionMergeModelV547 extends RankService {
         Collections.sort(result, Comparator.comparingDouble(o -> -o.getSortScore()));
         return result;
     }
-    public double calNewVideoScore(Map<String, String> itemBasicMap){
+
+    public double calNewVideoScore(Map<String, String> itemBasicMap) {
         double existenceDays = Double.valueOf(itemBasicMap.getOrDefault("existence_days", "30"));
-        if (existenceDays > 5){
+        if (existenceDays > 5) {
             return 0.0;
         }
         double score = 1.0 / (existenceDays + 10.0);
         return score;
     }
-    public double calTrendScore(List<Double> data){
+
+    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);
+        for (int i = 0; i < size - 4; ++i) {
+            sum += data.get(i) - data.get(i + 4);
         }
-        if (sum * 10 > 0.6){
+        if (sum * 10 > 0.6) {
             sum = 0.6;
-        }else{
+        } else {
             sum = sum * 10;
         }
-        if (sum > 0){
+        if (sum > 0) {
             // 为了打断点
             sum = sum;
         }
         return sum;
     }
-    public Double calScoreWeight(List<Double> data){
+
+    public Double calScoreWeight(List<Double> data) {
         Double up = 0.0;
         Double down = 0.0;
-        for (int i=0; i<data.size(); ++i){
+        for (int i = 0; i < data.size(); ++i) {
             up += 1.0 / (i + 1) * data.get(i);
             down += 1.0 / (i + 1);
         }
-        return down > 1E-8? up / down: 0.0;
+        return down > 1E-8 ? up / down : 0.0;
     }
-    public List<Double> getRateData(List<Double> ups, List<Double> downs, Double up, Double down){
+
+    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)){
+        for (int i = 0; i < ups.size(); ++i) {
+            if (ExtractorUtils.isDoubleEqualToZero(downs.get(i) + down)) {
                 data.add(0.0);
-            }else{
+            } else {
                 data.add(
                         (ups.get(i) + up) / (downs.get(i) + down)
                 );
@@ -323,21 +329,23 @@ public class RankStrategy4RegionMergeModelV547 extends RankService {
         }
         return data;
     }
+
     public List<Double> getStaticData(Map<String, Map<String, Double>> itemRealMap,
-                                      List<String> datehours, String key){
+                                      List<String> datehours, String key) {
         List<Double> views = new LinkedList<>();
         Map<String, Double> tmp = itemRealMap.getOrDefault(key, new HashMap<>());
-        for (String dh : datehours){
+        for (String dh : datehours) {
             views.add(tmp.getOrDefault(dh, 0.0D) +
-                    (views.isEmpty() ? 0.0: views.get(views.size()-1))
+                    (views.isEmpty() ? 0.0 : views.get(views.size() - 1))
             );
         }
         return views;
     }
+
     public List<RankItem> model(List<Video> videos, RankParam param,
-                                List<String> rtFeaPart){
+                                List<String> rtFeaPart) {
         List<RankItem> result = new ArrayList<>();
-        if (videos.isEmpty()){
+        if (videos.isEmpty()) {
             return result;
         }
 
@@ -352,7 +360,7 @@ public class RankStrategy4RegionMergeModelV547 extends RankService {
         redisTemplate.afterPropertiesSet();
 
         // 0: 场景特征处理
-        Map<String, String> sceneFeatureMap =  this.getSceneFeature(param);
+        Map<String, String> sceneFeatureMap = this.getSceneFeature(param);
 
         // 1: user特征处理
         Map<String, String> userFeatureMap = new HashMap<>();
@@ -383,6 +391,22 @@ public class RankStrategy4RegionMergeModelV547 extends RankService {
             }
         }
 
+        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);
+//        log.info("userFeature in model = {}", JSONUtils.toJson(f1));
+        
         // 2-1: item特征处理
         final Set<String> itemFeatureSet = new HashSet<>(Arrays.asList(
                 "total_time", "play_count_total",
@@ -392,18 +416,19 @@ public class RankStrategy4RegionMergeModelV547 extends RankService {
 
         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)
+        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){
+        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){
+                if (vF == null) {
                     continue;
                 }
-                try{
-                    vfMap = JSONUtils.fromJson(vF, new TypeToken<Map<String, String>>() {}, vfMap);
+                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();
@@ -422,7 +447,7 @@ public class RankStrategy4RegionMergeModelV547 extends RankService {
                     );
                     f4.putAll(f5);
                     rankItems.get(i).setFeatureMap(f4);
-                }catch (Exception e){
+                } catch (Exception e) {
                     log.error(String.format("parse video json is wrong in {} with {}", this.CLASS_NAME, e));
                 }
             }
@@ -435,80 +460,82 @@ public class RankStrategy4RegionMergeModelV547 extends RankService {
         String hour = new SimpleDateFormat("HH").format(calendar.getTime());
         String rtFeaPart1day = date + hour;
         String rtFeaPart1h = date + hour;
-        if (rtFeaPartKeyResult != null){
-            if (rtFeaPartKeyResult.get(0) != null){
+        if (rtFeaPartKeyResult != null) {
+            if (rtFeaPartKeyResult.get(0) != null) {
                 rtFeaPart1day = rtFeaPartKeyResult.get(0);
             }
-            if (rtFeaPartKeyResult.get(1) != null){
+            if (rtFeaPartKeyResult.get(1) != null) {
                 rtFeaPart1h = rtFeaPartKeyResult.get(1);
             }
         }
 
-        List<String> videoRtKeys1 = videoIds.stream().map(r-> "item_rt_fea_1day_" + r)
+        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)
+        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){
+        if (videoRtFeatures != null) {
             int j = 0;
-            for (RankItem item: rankItems){
+            for (RankItem item : rankItems) {
                 String vF = videoRtFeatures.get(j);
                 ++j;
-                if (vF == null){
+                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()){
+                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){
+                        if (value == null) {
                             continue;
                         }
-                        String [] var1 = value.split(",");
+                        String[] var1 = value.split(",");
                         Map<String, Double> tmp = new HashMap<>();
-                        for (String var2 : var1){
-                            String [] var3 = var2.split(":");
+                        for (String var2 : var1) {
+                            String[] var3 = var2.split(":");
                             tmp.put(var3[0], Double.valueOf(var3[1]));
                         }
                         vfMapNew.put(entry.getKey(), tmp);
                     }
-                }catch (Exception e){
+                } 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){
+            for (RankItem item : rankItems) {
                 String vF = videoRtFeatures.get(j);
                 ++j;
-                if (vF == null){
+                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);
+                try {
+                    vfMap = JSONUtils.fromJson(vF, new TypeToken<Map<String, String>>() {
+                    }, vfMap);
 
-                    for (Map.Entry<String, String> entry : vfMap.entrySet()){
+                    for (Map.Entry<String, String> entry : vfMap.entrySet()) {
                         String value = entry.getValue();
-                        if (value == null){
+                        if (value == null) {
                             continue;
                         }
-                        String [] var1 = value.split(",");
+                        String[] var1 = value.split(",");
                         Map<String, Double> tmp = new HashMap<>();
-                        for (String var2 : var1){
-                            String [] var3 = var2.split(":");
+                        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){
+                } 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);
@@ -521,6 +548,7 @@ public class RankStrategy4RegionMergeModelV547 extends RankService {
                 .scoring(sceneFeatureMap, userFeatureMap, rankItems);
         return rovRecallScore;
     }
+
     private Map<String, String> getSceneFeature(RankParam param) {
         Map<String, String> sceneFeatureMap = new HashMap<>();
         String provinceCn = param.getProvince();
@@ -537,8 +565,8 @@ public class RankStrategy4RegionMergeModelV547 extends RankService {
                 "吉林市".equals(city) |
                 "新竹市".equals(city) |
                 "嘉义市".equals(city)
-        ){
-        }else{
+        ) {
+        } else {
             city = city.replaceAll("市$", "");
         }
         sceneFeatureMap.put("ctx_city", city);
@@ -549,6 +577,7 @@ public class RankStrategy4RegionMergeModelV547 extends RankService {
 
         return sceneFeatureMap;
     }
+
     @Override
     public RankResult mergeAndSort(RankParam param, List<Video> rovVideos, List<Video> flowVideos) {
 
@@ -566,12 +595,12 @@ public class RankStrategy4RegionMergeModelV547 extends RankService {
         Map<String, Map<String, String>> rulesMap = this.filterRules.getOrDefault(abCode, new HashMap<>(0));
 
         //3 标签读取
-        if (rulesMap != null && !rulesMap.isEmpty()){
+        if (rulesMap != null && !rulesMap.isEmpty()) {
             RankExtractorItemTags extractorItemTags = new RankExtractorItemTags(this.redisTemplate);
             extractorItemTags.processor(rovVideos, flowVideos);
         }
         //6 合并结果时间卡控
-        if (rulesMap != null && !rulesMap.isEmpty()){
+        if (rulesMap != null && !rulesMap.isEmpty()) {
             RankProcessorTagFilter.processor(rovVideos, flowVideos, rulesMap);
         }
 
@@ -646,28 +675,28 @@ public class RankStrategy4RegionMergeModelV547 extends RankService {
         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";
 
         RankStrategy4RegionMergeModelV547 job = new RankStrategy4RegionMergeModelV547();
-        List<Double> l1 = job.getRateData(job.help(up1, "2024031115", 24),job.help(down1, "2024031115", 24),1.,10.);
+        List<Double> l1 = job.getRateData(job.help(up1, "2024031115", 24), job.help(down1, "2024031115", 24), 1., 10.);
         Double d1 = job.calScoreWeight(l1);
 
         System.out.println(d1);
 
-        List<Double> l2 = job.getRateData(job.help(up2, "2024031115", 24),job.help(down2, "2024031115", 24),1.,10.);
+        List<Double> l2 = job.getRateData(job.help(up2, "2024031115", 24), job.help(down2, "2024031115", 24), 1., 10.);
         Double d2 = job.calScoreWeight(l2);
 
         System.out.println(d2);
 
     }
 
-    List<Double> help(String s, String date, Integer h){
+    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])
-        ));
+                .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));
+        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);

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

@@ -340,6 +340,22 @@ public class RankStrategy4RegionMergeModelV548 extends RankService {
             }
         }
 
+        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);
+//        log.info("userFeature in model = {}", JSONUtils.toJson(f1));
+
         // 2-1: item特征处理
         final Set<String> itemFeatureSet = new HashSet<>(Arrays.asList(
                 "total_time", "play_count_total",