|  | @@ -0,0 +1,79 @@
 | 
											
												
													
														|  | 
 |  | +package com.tzld.piaoquan.recommend.server.service.recall.strategy;
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +import com.google.common.collect.Lists;
 | 
											
												
													
														|  | 
 |  | +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.RegionFilterService;
 | 
											
												
													
														|  | 
 |  | +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 com.tzld.piaoquan.recommend.server.service.score.ScorerUtils;
 | 
											
												
													
														|  | 
 |  | +import com.tzld.piaoquan.recommend.server.service.score4recall.ScorerPipeline4Recall;
 | 
											
												
													
														|  | 
 |  | +import org.apache.commons.collections4.CollectionUtils;
 | 
											
												
													
														|  | 
 |  | +import org.apache.commons.lang3.tuple.Pair;
 | 
											
												
													
														|  | 
 |  | +import org.springframework.stereotype.Component;
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +import javax.annotation.Resource;
 | 
											
												
													
														|  | 
 |  | +import java.util.*;
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +/**
 | 
											
												
													
														|  | 
 |  | + * @author zhangbo
 | 
											
												
													
														|  | 
 |  | + */
 | 
											
												
													
														|  | 
 |  | +@Component
 | 
											
												
													
														|  | 
 |  | +public class ContentBaseRecallStrategy implements RecallStrategy {
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +    public static final String PUSH_FORM = "content_base_recall_strategy";
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +    @Resource
 | 
											
												
													
														|  | 
 |  | +    private RegionFilterService filterService;
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +    @Override
 | 
											
												
													
														|  | 
 |  | +    public List<Video> recall(RecallParam param) {
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +        // 1 获取省份key 放入参数map中
 | 
											
												
													
														|  | 
 |  | +        String provinceCn = param.getProvince();
 | 
											
												
													
														|  | 
 |  | +        if (provinceCn == null) {
 | 
											
												
													
														|  | 
 |  | +            provinceCn = "中国";
 | 
											
												
													
														|  | 
 |  | +        } else {
 | 
											
												
													
														|  | 
 |  | +            provinceCn = provinceCn.replaceAll("省$", "");
 | 
											
												
													
														|  | 
 |  | +        }
 | 
											
												
													
														|  | 
 |  | +        Map<String, String> param4Model = new HashMap<>(1);
 | 
											
												
													
														|  | 
 |  | +        param4Model.put("region_province", provinceCn);
 | 
											
												
													
														|  | 
 |  | +        // 2 通过model拿到召回list
 | 
											
												
													
														|  | 
 |  | +        ScorerPipeline4Recall pipeline = ScorerUtils.getScorerPipeline4Recall("content_base_recall.conf");
 | 
											
												
													
														|  | 
 |  | +        List<List<Pair<Long, Double>>> results = pipeline.recall(param4Model);
 | 
											
												
													
														|  | 
 |  | +        List<Pair<Long, Double>> result = results.get(0);
 | 
											
												
													
														|  | 
 |  | +        for (int i = 1; i < results.size(); ++i) {
 | 
											
												
													
														|  | 
 |  | +            result.addAll(results.get(i));
 | 
											
												
													
														|  | 
 |  | +        }
 | 
											
												
													
														|  | 
 |  | +        Map<Long, Double> videoMap = new LinkedHashMap<>();
 | 
											
												
													
														|  | 
 |  | +        for (Pair<Long, Double> v : result) {
 | 
											
												
													
														|  | 
 |  | +            videoMap.put(v.getLeft(), v.getRight());
 | 
											
												
													
														|  | 
 |  | +        }
 | 
											
												
													
														|  | 
 |  | +        FilterParam filterParam = FilterParamFactory.create(param, Lists.newArrayList(videoMap.keySet()));
 | 
											
												
													
														|  | 
 |  | +        filterParam.setForceTruncation(10000);
 | 
											
												
													
														|  | 
 |  | +        filterParam.setConcurrent(true);
 | 
											
												
													
														|  | 
 |  | +        filterParam.setNotUsePreView(false);
 | 
											
												
													
														|  | 
 |  | +        FilterResult filterResult = filterService.filter(filterParam);
 | 
											
												
													
														|  | 
 |  | +        List<Video> videosResult = new ArrayList<>();
 | 
											
												
													
														|  | 
 |  | +        if (filterResult != null && CollectionUtils.isNotEmpty(filterResult.getVideoIds())) {
 | 
											
												
													
														|  | 
 |  | +            filterResult.getVideoIds().forEach(vid -> {
 | 
											
												
													
														|  | 
 |  | +                Video video = new Video();
 | 
											
												
													
														|  | 
 |  | +                video.setVideoId(vid);
 | 
											
												
													
														|  | 
 |  | +                video.setAbCode(param.getAbCode());
 | 
											
												
													
														|  | 
 |  | +                video.setRovScore(videoMap.get(vid));
 | 
											
												
													
														|  | 
 |  | +                video.setPushFrom(pushFrom());
 | 
											
												
													
														|  | 
 |  | +                videosResult.add(video);
 | 
											
												
													
														|  | 
 |  | +            });
 | 
											
												
													
														|  | 
 |  | +        }
 | 
											
												
													
														|  | 
 |  | +        return videosResult;
 | 
											
												
													
														|  | 
 |  | +    }
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +    @Override
 | 
											
												
													
														|  | 
 |  | +    public String pushFrom() {
 | 
											
												
													
														|  | 
 |  | +        return PUSH_FORM;
 | 
											
												
													
														|  | 
 |  | +    }
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +}
 |