Browse Source

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

jiachanghui 3 weeks ago
parent
commit
467c589d88

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

@@ -384,6 +384,21 @@ public abstract class RankStrategy4RegionMergeModelBasic extends RankService {
         return map;
     }
 
+    protected Map<String, String> getUserRegion(RankParam param) {
+        Map<String, String> regionMap = new HashMap<>();
+        if (null != param) {
+            String province = param.getProvince();
+            if (null != province && !province.isEmpty()) {
+                regionMap.put("province", province.replaceAll("省$", ""));
+            }
+            String city = param.getCity();
+            if (null != city && !city.isEmpty()) {
+                regionMap.put("city", city.replaceAll("市$", ""));
+            }
+        }
+        return regionMap;
+    }
+
     private boolean isInsertDouHotFlowPoolVideo() {
         double rand = RandomUtils.nextDouble(0, 1);
         return rand <= newFlowPoolSelectRate;

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

@@ -45,6 +45,7 @@ public class RankStrategy4RegionMergeModelV564 extends RankStrategy4RegionMergeM
 
         long currentMs = System.currentTimeMillis();
         Set<Long> setVideo = new HashSet<>();
+        setVideo.add(param.getHeadVid());
         List<Video> rovRecallRank = new ArrayList<>();
         // -------------------5路特殊旧召回------------------
         RecallUtils.extractOldSpecialRecall(param, setVideo, rovRecallRank);
@@ -68,6 +69,8 @@ public class RankStrategy4RegionMergeModelV564 extends RankStrategy4RegionMergeM
         RecallUtils.extractRecall(mergeWeight.getOrDefault("headCate2Rov", 5.0).intValue(), param, HeadCate2RovRecallStrategy.PUSH_FROM, setVideo, rovRecallRank);
         //-------------------city rovn------------------
         RecallUtils.extractRecall(mergeWeight.getOrDefault("cityRov", 5.0).intValue(), param, CityRovnRecallStrategy.PUSH_FROM, setVideo, rovRecallRank);
+        //-------------------priori province rovn------------------
+        RecallUtils.extractRecall(mergeWeight.getOrDefault("prioriProvinceRov", 5.0).intValue(), param, PrioriProvinceRovnRecallStrategy.PUSH_FROM, setVideo, rovRecallRank);
 
         //-------------------排-------------------
         //-------------------序-------------------
@@ -118,6 +121,7 @@ public class RankStrategy4RegionMergeModelV564 extends RankStrategy4RegionMergeM
             cate2Coefficient.putAll(simCateScore);
         }
         Double cate2CoefficientDenominator = mergeWeight.getOrDefault("cate2CoefficientDenominator", 1d);
+        Map<String, String> regionMap = getUserRegion(param);
 
         List<Video> result = new ArrayList<>();
         for (RankItem item : items) {
@@ -169,6 +173,9 @@ public class RankStrategy4RegionMergeModelV564 extends RankStrategy4RegionMergeM
             if (MapUtils.isNotEmpty(param.getCreativeInfoFeature())) {
                 video.getMetaFeatureMap().put("creativeInfo", param.getCreativeInfoFeature());
             }
+            if (MapUtils.isNotEmpty(regionMap)) {
+                video.getMetaFeatureMap().put("region", regionMap);
+            }
             result.add(video);
         }
         ExtractVideoMergeCate.addOtherParam(result, videoBaseInfoMap);

+ 0 - 27
recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/rank/tansform/FeatureV6.java

@@ -427,11 +427,6 @@ public class FeatureV6 {
             double str_plus = FeatureUtils.wilsonScore(is_return_1, exp);
             double ros_one = FeatureUtils.wilsonScore(is_return_1, is_share);
 
-            double rovn = FeatureUtils.plusSmooth(return_n_uv, exp, smoothPlus);
-            double ros = FeatureUtils.plusSmooth(return_n_uv, is_share, smoothPlus);
-            double ros_n = FeatureUtils.plusSmooth(return_n_uv, share_cnt, smoothPlus);
-            double ros_minus = FeatureUtils.plusSmooth(return_n_uv, is_return_1, smoothPlus);
-
             // larger smooth
             double l_rovn = FeatureUtils.plusSmooth(return_n_uv, exp, largerSmoothPlus, 1);
             double l_ros = FeatureUtils.plusSmooth(return_n_uv / 5, is_share, largerSmoothPlus, 2);
@@ -446,10 +441,6 @@ public class FeatureV6 {
             featMap.put(prefix + "_" + period + "@" + "str", str);
             featMap.put(prefix + "_" + period + "@" + "str_plus", str_plus);
             featMap.put(prefix + "_" + period + "@" + "ros_one", ros_one);
-            featMap.put(prefix + "_" + period + "@" + "rovn", rovn);
-            featMap.put(prefix + "_" + period + "@" + "ros", ros);
-            featMap.put(prefix + "_" + period + "@" + "ros_n", ros_n);
-            featMap.put(prefix + "_" + period + "@" + "ros_minus", ros_minus);
 
             // larger smooth
             featMap.put(prefix + "_" + period + "@" + "rovn_#", l_rovn);
@@ -485,16 +476,6 @@ public class FeatureV6 {
             double str_plus = FeatureUtils.wilsonScore(is_return_1, exp);
             double ros_one = FeatureUtils.wilsonScore(is_return_1, is_share);
 
-            double rovn1 = FeatureUtils.plusSmooth(return_1_uv, exp, smoothPlus);
-            double ros1 = FeatureUtils.plusSmooth(return_1_uv, is_share, smoothPlus);
-            double ros_n1 = FeatureUtils.plusSmooth(return_1_uv, share_cnt, smoothPlus);
-            double ros_minus1 = FeatureUtils.plusSmooth(return_1_uv, is_return_1, smoothPlus);
-
-            double rovn = FeatureUtils.plusSmooth(return_n_uv, exp, smoothPlus);
-            double ros = FeatureUtils.plusSmooth(return_n_uv, is_share, smoothPlus);
-            double ros_n = FeatureUtils.plusSmooth(return_n_uv, share_cnt, smoothPlus);
-            double ros_minus = FeatureUtils.plusSmooth(return_n_uv, is_return_1, smoothPlus);
-
             // larger smooth
             double l_rovn1 = FeatureUtils.plusSmooth(return_1_uv, exp, largerSmoothPlus, 1);
             double l_ros1 = FeatureUtils.plusSmooth(return_1_uv / 5, is_share, largerSmoothPlus, 2);
@@ -515,14 +496,6 @@ public class FeatureV6 {
             featMap.put(prefix + "_" + period + "@" + "str", str);
             featMap.put(prefix + "_" + period + "@" + "str_plus", str_plus);
             featMap.put(prefix + "_" + period + "@" + "ros_one", ros_one);
-            featMap.put(prefix + "_" + period + "@" + "rovn1", rovn1);
-            featMap.put(prefix + "_" + period + "@" + "ros1", ros1);
-            featMap.put(prefix + "_" + period + "@" + "ros_n1", ros_n1);
-            featMap.put(prefix + "_" + period + "@" + "ros_minus1", ros_minus1);
-            featMap.put(prefix + "_" + period + "@" + "rovn", rovn);
-            featMap.put(prefix + "_" + period + "@" + "ros", ros);
-            featMap.put(prefix + "_" + period + "@" + "ros_n", ros_n);
-            featMap.put(prefix + "_" + period + "@" + "ros_minus", ros_minus);
 
             // larger smooth
             featMap.put(prefix + "_" + period + "@" + "rovn1_#", l_rovn1);

+ 1 - 0
recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/recall/RecallService.java

@@ -118,6 +118,7 @@ public class RecallService implements ApplicationContextAware {
         strategies.add(strategyMap.get(HeadProvinceCate2RecallStrategy.class.getSimpleName()));
         strategies.add(strategyMap.get(HeadCate2RovRecallStrategy.class.getSimpleName()));
         strategies.add(strategyMap.get(CityRovnRecallStrategy.class.getSimpleName()));
+        strategies.add(strategyMap.get(PrioriProvinceRovnRecallStrategy.class.getSimpleName()));
         Set<String> abExpCodes = param.getAbExpCodes();
         if (CollectionUtils.isNotEmpty(abExpCodes) && abExpCodes.contains("568")) {
             strategies.add(strategyMap.get(PremiumROVRecallStrategy.class.getSimpleName()));

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

@@ -0,0 +1,102 @@
+package com.tzld.piaoquan.recommend.server.service.recall.strategy;
+
+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.FilterService;
+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 lombok.extern.slf4j.Slf4j;
+import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.tuple.MutablePair;
+import org.apache.commons.lang3.tuple.Pair;
+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;
+
+@Component
+@Slf4j
+public class PrioriProvinceRovnRecallStrategy implements RecallStrategy {
+    private final String CLASS_NAME = this.getClass().getSimpleName();
+    @Autowired
+    private FilterService filterService;
+    @Autowired
+    @Qualifier("redisTemplate")
+    public RedisTemplate<String, String> redisTemplate;
+
+    public static final String PUSH_FROM = "priori_province_rovn";
+    public static final String redisKeyPrefix = "priori_province_rovn_recall";
+
+    @Override
+    public String pushFrom() {
+        return PUSH_FROM;
+    }
+
+    @Override
+    public List<Video> recall(RecallParam param) {
+        List<Video> videosResult = new ArrayList<>();
+        try {
+            String province = param.getProvince().replaceAll("省$", "");
+            String redisKey = String.format("%s:%s", redisKeyPrefix, province);
+            String redisValue = redisTemplate.opsForValue().get(redisKey);
+            if (null == redisValue || redisValue.isEmpty()) {
+                return videosResult;
+            }
+            Pair<List<Long>, Map<Long, Double>> pair = parsePair(redisValue, param.getVideoId(), 50);
+            fillVideoResult(param, pair, videosResult);
+        } catch (Exception e) {
+            log.error("recall is wrong in {}, error={}", CLASS_NAME, e);
+        }
+        return videosResult;
+    }
+
+    private Pair<List<Long>, Map<Long, Double>> parsePair(String data, long headVid, int size) {
+        List<Long> idsList = new ArrayList<>();
+        Map<Long, Double> scoresMap = new HashMap<>();
+        if (!StringUtils.isBlank(data)) {
+            String[] cells = data.split("\t");
+            if (2 == cells.length) {
+                List<Long> ids = Arrays.stream(cells[0].split(",")).map(Long::valueOf).collect(Collectors.toList());
+                List<Double> scores = Arrays.stream(cells[1].split(",")).map(Double::valueOf).collect(Collectors.toList());
+                if (!ids.isEmpty() && ids.size() == scores.size()) {
+                    int minSize = Math.min(size, ids.size());
+                    for (int i = 0; i < minSize; ++i) {
+                        long id = ids.get(i);
+                        double score = scores.get(i);
+                        if (headVid != id && !scoresMap.containsKey(id)) {
+                            idsList.add(id);
+                            scoresMap.put(id, score);
+                        }
+                    }
+                }
+            }
+        }
+        return new MutablePair<>(idsList, scoresMap);
+    }
+
+    private void fillVideoResult(RecallParam param, Pair<List<Long>, Map<Long, Double>> pair, List<Video> videosResult) {
+        if (null != pair) {
+            List<Long> ids = pair.getLeft();
+            Map<Long, Double> scoresMap = pair.getRight();
+            if (null != ids && null != scoresMap && !ids.isEmpty()) {
+                FilterParam filterParam = FilterParamFactory.create(param, ids);
+                FilterResult filterResult = filterService.filter(filterParam);
+                if (null != filterResult && CollectionUtils.isNotEmpty(filterResult.getVideoIds())) {
+                    filterResult.getVideoIds().forEach(vid -> {
+                        Video video = new Video();
+                        video.setVideoId(vid);
+                        video.setRovScore(scoresMap.getOrDefault(vid, 0D));
+                        video.setPushFrom(pushFrom());
+                        videosResult.add(video);
+                    });
+                }
+            }
+        }
+    }
+}

+ 1 - 0
recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/util/RecallUtils.java

@@ -53,6 +53,7 @@ public class RecallUtils {
         oldRovs.addAll(extractAndSort(param, RegionRelative24HRecallStrategy.PUSH_FORM));
         oldRovs.addAll(extractAndSort(param, RegionRelative24HDupRecallStrategy.PUSH_FORM));
         removeDuplicate(oldRovs);
+        oldRovs = oldRovs.stream().filter(r -> !setVideo.contains(r.getVideoId())).collect(Collectors.toList());
         int sizeReturn = param.getSize();
         List<Video> v0 = oldRovs.size() <= sizeReturn
                 ? oldRovs