|
@@ -0,0 +1,204 @@
|
|
|
+package com.tzld.piaoquan.recommend.server.service.rank.strategy;
|
|
|
+
|
|
|
+
|
|
|
+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.RankService;
|
|
|
+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.JSONUtils;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.collections4.CollectionUtils;
|
|
|
+import org.apache.commons.lang3.math.NumberUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.data.redis.connection.RedisConnectionFactory;
|
|
|
+import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
|
|
|
+import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
|
|
+import org.springframework.data.redis.serializer.StringRedisSerializer;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author zhangbo
|
|
|
+ * @desc 模型的排序实验
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class RankStrategy4RankModel extends RankService {
|
|
|
+
|
|
|
+ @Value("${video.model.weight:}")
|
|
|
+ private Double mergeWeight;
|
|
|
+ final private String CLASS_NAME = this.getClass().getSimpleName();
|
|
|
+ @Override
|
|
|
+ public List<Video> mergeAndRankRovRecall(RankParam param) {
|
|
|
+
|
|
|
+ //-------------------地域内部融合-------------------
|
|
|
+ List<Video> rovRecallRank = new ArrayList<>();
|
|
|
+ rovRecallRank.addAll(extractAndSort(param, RegionHRecallStrategy.PUSH_FORM));
|
|
|
+ rovRecallRank.addAll(extractAndSort(param, RegionHDupRecallStrategy.PUSH_FORM));
|
|
|
+ rovRecallRank.addAll(extractAndSort(param, Region24HRecallStrategy.PUSH_FORM));
|
|
|
+ rovRecallRank.addAll(extractAndSort(param, RegionRelative24HRecallStrategy.PUSH_FORM));
|
|
|
+ rovRecallRank.addAll(extractAndSort(param, RegionRelative24HDupRecallStrategy.PUSH_FORM));
|
|
|
+
|
|
|
+ removeDuplicate(rovRecallRank);
|
|
|
+ rovRecallRank = rovRecallRank.size() <= param.getSize()
|
|
|
+ ? rovRecallRank
|
|
|
+ : rovRecallRank.subList(0, param.getSize());
|
|
|
+
|
|
|
+ //-------------------地域 sim returnv2 融合-------------------
|
|
|
+ rovRecallRank.addAll(extractAndSort(param, SimHotVideoRecallStrategy.PUSH_FORM));
|
|
|
+ rovRecallRank.addAll(extractAndSort(param, ReturnVideoRecallStrategy.PUSH_FORM));
|
|
|
+ removeDuplicate(rovRecallRank);
|
|
|
+
|
|
|
+ //-------------------排-------------------
|
|
|
+ //-------------------序-------------------
|
|
|
+ //-------------------逻-------------------
|
|
|
+ //-------------------辑-------------------
|
|
|
+ List<String> videoIdKeys = rovRecallRank.stream()
|
|
|
+ .map(t -> param.getRankKeyPrefix() + t.getVideoId())
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ List<String> videoScores = this.redisTemplate.opsForValue().multiGet(videoIdKeys);
|
|
|
+ log.info("rank mergeAndRankRovRecall videoIdKeys={}, videoScores={}", JSONUtils.toJson(videoIdKeys),
|
|
|
+ JSONUtils.toJson(videoScores));
|
|
|
+ if (CollectionUtils.isNotEmpty(videoScores)
|
|
|
+ && videoScores.size() == rovRecallRank.size()) {
|
|
|
+ for (int i = 0; i < videoScores.size(); i++) {
|
|
|
+ rovRecallRank.get(i).setSortScore(NumberUtils.toDouble(videoScores.get(i), 0.0));
|
|
|
+ }
|
|
|
+ Collections.sort(rovRecallRank, Comparator.comparingDouble(o -> -o.getSortScore()));
|
|
|
+ }
|
|
|
+
|
|
|
+ //------------------- todo zhangbo 增加排序str模型逻辑 合并二者得分-------------------
|
|
|
+ List<Video> videosWithModel = model(rovRecallRank, param);
|
|
|
+ for (Video v : videosWithModel){
|
|
|
+ double mergeWeightIn = this.mergeWeight == null? 0.0D: this.mergeWeight;
|
|
|
+ v.setSortScore(v.getSortScore() + mergeWeightIn * v.getModelScore());
|
|
|
+ }
|
|
|
+ Collections.sort(videosWithModel, Comparator.comparingDouble(o -> -o.getSortScore()));
|
|
|
+ return videosWithModel;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<Video> model(List<Video> videos, RankParam param){
|
|
|
+ if (videos.isEmpty()){
|
|
|
+ return videos;
|
|
|
+ }
|
|
|
+
|
|
|
+ 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();
|
|
|
+
|
|
|
+ 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));
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ return videos;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ 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_ctr_1day","u_str_1day","u_rov_1day","u_ros_1day",
|
|
|
+ "u_3day_exp_cnt","u_3day_click_cnt","u_3day_share_cnt","u_3day_return_cnt",
|
|
|
+ "u_ctr_3day","u_str_3day","u_rov_3day","u_ros_3day"
|
|
|
+ ));
|
|
|
+ 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();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("userFeature in model = {}", JSONUtils.toJson(userFeatureMap));
|
|
|
+
|
|
|
+ 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_ctr_1day", "i_str_1day", "i_rov_1day", "i_ros_1day",
|
|
|
+ "i_3day_exp_cnt", "i_3day_click_cnt", "i_3day_share_cnt", "i_3day_return_cnt",
|
|
|
+ "i_ctr_3day", "i_str_3day", "i_rov_3day", "i_ros_3day"
|
|
|
+ ));
|
|
|
+
|
|
|
+ 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);
|
|
|
+ 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();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ rankItems.get(i).setFeatureMap(vfMap);
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error(String.format("parse video json is wrong in {} with {}",
|
|
|
+ this.CLASS_NAME, e));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ log.info("ItemFeature = {}", JSONUtils.toJson(videoFeatures));
|
|
|
+
|
|
|
+ Map<String, String> sceneFeatureMap = this.getSceneFeature(param);
|
|
|
+
|
|
|
+ List<RankItem> rovRecallScore = ScorerUtils.getScorerPipeline(ScorerUtils.BASE_CONF)
|
|
|
+ .scoring(sceneFeatureMap, userFeatureMap, rankItems);
|
|
|
+ log.info("mergeAndRankRovRecallNew rovRecallScore={}", JSONUtils.toJson(rovRecallScore));
|
|
|
+ return CommonCollectionUtils.toList(rovRecallScore, i -> {
|
|
|
+ // hard code 将排序分数 赋值给video的sortScore
|
|
|
+ Video v = i.getVideo();
|
|
|
+ v.setModelScore(i.getScore());
|
|
|
+ return v;
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, String> getSceneFeature(RankParam param) {
|
|
|
+ Map<String, String> sceneFeatureMap = new HashMap<>();
|
|
|
+ sceneFeatureMap.put("ctx_region", param.getProvince());
|
|
|
+ sceneFeatureMap.put("ctx_city", param.getCity());
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|