package com.iplatform.recvideo;
|
|
import com.iplatform.model.po.Rc_video_t2;
|
import com.walker.infrastructure.utils.StringUtils;
|
|
import java.util.ArrayList;
|
import java.util.Collections;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
/**
|
* 对用户推荐视频进行计算排序。
|
* @author 时克英
|
* @date 2022-09-26
|
*/
|
public class SimilarVideoUserOrder {
|
|
// private Map<String, Long> videoIdUserCache = new HashMap<>();
|
|
// 最终排序用的用户推荐视频集合结果, key = 推荐视频ID
|
private Map<String, SimilarVideoUser> orderVideoList = new HashMap<>();
|
|
// 计算多个推荐视频id,总得分, key = 推荐视频ID
|
private Map<String, List<Double>> simiVideoDistanceMap = new HashMap<>();
|
|
/**
|
*
|
* @param videoIdUserCache 推荐视频ID --> 用户ID 关系
|
* @param list 计算后的: rc_video_t2 表数据
|
*/
|
public SimilarVideoUserOrder(Map<String, Long> videoIdUserCache, List<Rc_video_t2> list){
|
// this.videoIdUserCache = videoUser;
|
Long userId = null;
|
for(Rc_video_t2 e : list){
|
this.addSimiVideoDistance(e.getSim_video_id(), e.getScore());
|
userId = videoIdUserCache.get(e.getSrc_video_id());
|
if(userId == null){
|
throw new IllegalArgumentException("源视频未找到对应用户,src_video_id=" + e.getSrc_video_id());
|
}
|
this.addOrderVideo(e.getSim_video_id(), userId);
|
}
|
}
|
|
public List<SimilarVideoUser> calculateOrder(){
|
SimilarVideoUser similarVideoInfo = null;
|
double totalScore = 0;
|
for(Map.Entry<String, List<Double>> entry : this.simiVideoDistanceMap.entrySet()){
|
similarVideoInfo = this.orderVideoList.get(entry.getKey());
|
totalScore = this.getTotalScore(entry.getValue(), entry.getKey());
|
similarVideoInfo.setScore(totalScore);
|
}
|
|
List<SimilarVideoUser> resultList = new ArrayList<>(128);
|
for(SimilarVideoUser e : this.orderVideoList.values()){
|
resultList.add(e);
|
}
|
Collections.sort(resultList);
|
return resultList;
|
}
|
|
private double getTotalScore(List<Double> list, String simiVideoId){
|
if(StringUtils.isEmptyList(list)){
|
return 0;
|
}
|
double total = 0;
|
for(double d : list){
|
total += d;
|
}
|
return total;
|
}
|
|
private void addSimiVideoDistance(String videoId, double score){
|
List<Double> v = this.simiVideoDistanceMap.get(videoId);
|
if(v == null){
|
v = new ArrayList<>(16);
|
this.simiVideoDistanceMap.put(videoId, v);
|
}
|
v.add(score);
|
}
|
|
private void addOrderVideo(String simiVideoId, long userId){
|
SimilarVideoUser v = this.orderVideoList.get(simiVideoId);
|
if(v == null){
|
v = new SimilarVideoUser(userId, simiVideoId);
|
this.orderVideoList.put(simiVideoId, v);
|
}
|
v.increase();
|
}
|
}
|