shikeying
2022-09-26 fd03e31f173ad9c52b15a30a9127e2b6a468538d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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();
    }
}