|
@@ -19,6 +19,8 @@ import com.tzld.piaoquan.recommend.server.service.score.ScoreParam;
|
|
|
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.Getter;
|
|
|
+import lombok.Setter;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.collections4.CollectionUtils;
|
|
|
import org.apache.commons.lang3.RandomUtils;
|
|
@@ -40,9 +42,9 @@ import java.util.stream.Collectors;
|
|
|
@Slf4j
|
|
|
public class RankService {
|
|
|
@Autowired
|
|
|
- private RedisTemplate<String, String> redisTemplate;
|
|
|
+ public RedisTemplate<String, String> redisTemplate;
|
|
|
@Autowired
|
|
|
- private FeatureRemoteService featureRemoteService;
|
|
|
+ public FeatureRemoteService featureRemoteService;
|
|
|
@Value("${newRankSwitch:false}")
|
|
|
private boolean newRankSwitch;
|
|
|
@Value("${newRankAbExpCode:528}")
|
|
@@ -79,16 +81,9 @@ public class RankService {
|
|
|
JSONUtils.toJson(flowPoolRank));
|
|
|
|
|
|
|
|
|
- String abCode = param.getAbCode();
|
|
|
- switch (abCode){
|
|
|
- case "60098":
|
|
|
- return this.mergeAndSort4Density(param, rovRecallRank, flowPoolRank);
|
|
|
- default:
|
|
|
- return mergeAndSort(param, rovRecallRank, flowPoolRank);
|
|
|
- }
|
|
|
- }
|
|
|
+ return mergeAndSort(param, rovRecallRank, flowPoolRank); }
|
|
|
|
|
|
- private List<Video> mergeAndRankRovRecall(RankParam param) {
|
|
|
+ public List<Video> mergeAndRankRovRecall(RankParam param) {
|
|
|
|
|
|
|
|
|
boolean hitTest = newRankSwitch
|
|
@@ -100,7 +95,7 @@ public class RankService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private List<Video> mergeAndRankRovRecallOld(RankParam param) {
|
|
|
+ public List<Video> mergeAndRankRovRecallOld(RankParam param) {
|
|
|
List<Video> rovRecallRank = new ArrayList<>();
|
|
|
rovRecallRank.addAll(extractAndSort(param, RegionHRecallStrategy.PUSH_FORM));
|
|
|
rovRecallRank.addAll(extractAndSort(param, RegionHDupRecallStrategy.PUSH_FORM));
|
|
@@ -182,7 +177,7 @@ public class RankService {
|
|
|
return rovRecallRank;
|
|
|
}
|
|
|
|
|
|
- private void removeDuplicate(List<Video> rovRecallRank) {
|
|
|
+ public void removeDuplicate(List<Video> rovRecallRank) {
|
|
|
if (CollectionUtils.isNotEmpty(rovRecallRank)) {
|
|
|
Set<Long> videoIds = new HashSet<>();
|
|
|
Iterator<Video> ite = rovRecallRank.iterator();
|
|
@@ -259,7 +254,7 @@ public class RankService {
|
|
|
return scoreParam;
|
|
|
}
|
|
|
|
|
|
- private List<Video> mergeAndRankFlowPoolRecall(RankParam param) {
|
|
|
+ public List<Video> mergeAndRankFlowPoolRecall(RankParam param) {
|
|
|
if (param.getAppType() == AppTypeEnum.LAO_HAO_KAN_VIDEO.getCode()
|
|
|
|| param.getAppType() == AppTypeEnum.ZUI_JING_QI.getCode()) {
|
|
|
if (param.getAbCode().equals("60054")
|
|
@@ -280,7 +275,7 @@ public class RankService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private List<Video> extractAndSort(RankParam param, String pushFrom) {
|
|
|
+ public List<Video> extractAndSort(RankParam param, String pushFrom) {
|
|
|
if (param == null
|
|
|
|| param.getRecallResult() == null
|
|
|
|| CollectionUtils.isEmpty(param.getRecallResult().getData())) {
|
|
@@ -299,7 +294,7 @@ public class RankService {
|
|
|
return Collections.emptyList();
|
|
|
}
|
|
|
|
|
|
- private double getFlowPoolP(RankParam param) {
|
|
|
+ public double getFlowPoolP(RankParam param) {
|
|
|
if (param.getAppType() == AppTypeEnum.LAO_HAO_KAN_VIDEO.getCode()
|
|
|
|| param.getAppType() == AppTypeEnum.ZUI_JING_QI.getCode()) {
|
|
|
return param.getFlowPoolP();
|
|
@@ -314,7 +309,7 @@ public class RankService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void removeDuplicate(RankParam param, List<Video> rovRecallRank, List<Video> flowPoolRank) {
|
|
|
+ public void removeDuplicate(RankParam param, List<Video> rovRecallRank, List<Video> flowPoolRank) {
|
|
|
|
|
|
|
|
|
|
|
@@ -346,7 +341,7 @@ public class RankService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private RankResult mergeAndSort(RankParam param, List<Video> rovRecallRank, List<Video> flowPoolRank) {
|
|
|
+ public RankResult mergeAndSort(RankParam param, List<Video> rovRecallRank, List<Video> flowPoolRank) {
|
|
|
if (CollectionUtils.isEmpty(rovRecallRank)) {
|
|
|
if (param.getSize() < flowPoolRank.size()) {
|
|
|
return new RankResult(flowPoolRank.subList(0, param.getSize()));
|
|
@@ -394,120 +389,6 @@ public class RankService {
|
|
|
return new RankResult(result);
|
|
|
}
|
|
|
|
|
|
- private RankResult mergeAndSort4Density(RankParam param, List<Video> rovRecallRank, List<Video> flowPoolRank) {
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- if (CollectionUtils.isEmpty(rovRecallRank)) {
|
|
|
- if (param.getSize() < flowPoolRank.size()) {
|
|
|
- return new RankResult(flowPoolRank.subList(0, param.getSize()));
|
|
|
- } else {
|
|
|
- return new RankResult(flowPoolRank);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- String appType = String.valueOf(param.getAppType());
|
|
|
- String ruleStr = this.redisTemplate.opsForValue().get("TAGS_FILTER_RULE_V1_JSON");
|
|
|
- Map<String, Integer> densityRules = new HashMap<>();
|
|
|
- if (ruleStr != null){
|
|
|
- Map<String, Map<String, Object>> ruleOrigin = JSONUtils.fromJson(ruleStr,
|
|
|
- new TypeToken<Map<String, Map<String, Object>>>() {},
|
|
|
- Collections.emptyMap());
|
|
|
- for (Map.Entry<String, Map<String, Object>> entry : ruleOrigin.entrySet()){
|
|
|
- String k = entry.getKey();
|
|
|
- if (!entry.getValue().containsKey(appType)){
|
|
|
- continue;
|
|
|
- }
|
|
|
- JSONObject jb = (JSONObject) entry.getValue().get(appType);
|
|
|
- try{
|
|
|
- if (jb.containsKey("density") && jb.get("density") instanceof Integer){
|
|
|
- densityRules.put(k, jb.getInteger("density"));
|
|
|
- }
|
|
|
- }catch (Exception e){
|
|
|
- log.error("parse densityRules is wrong:", e);
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- List<Long> videoIds = new ArrayList<>();
|
|
|
- for (Video v : rovRecallRank) {
|
|
|
- videoIds.add(v.getVideoId());
|
|
|
- }
|
|
|
- for (Video v : flowPoolRank) {
|
|
|
- videoIds.add(v.getVideoId());
|
|
|
- }
|
|
|
- Map<Long, List<String>> videoTagDict = RankExtractorFeature.getVideoTags(this.redisTemplate, videoIds);
|
|
|
- for (Video v : rovRecallRank) {
|
|
|
- v.setTags(videoTagDict.getOrDefault(v.getVideoId(), new ArrayList<>()));
|
|
|
- }
|
|
|
- for (Video v : flowPoolRank) {
|
|
|
- v.setTags(videoTagDict.getOrDefault(v.getVideoId(), new ArrayList<>()));
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- List<Video> result = new ArrayList<>();
|
|
|
- for (int i = 0; i < param.getTopK() && i < rovRecallRank.size(); i++) {
|
|
|
- result.add(rovRecallRank.get(i));
|
|
|
- }
|
|
|
-
|
|
|
- double flowPoolP = getFlowPoolP(param);
|
|
|
- int flowPoolIndex = 0;
|
|
|
- int rovPoolIndex = param.getTopK();
|
|
|
-
|
|
|
- for (int i = 0; i < param.getSize() - param.getTopK(); i++) {
|
|
|
- double rand = RandomUtils.nextDouble(0, 1);
|
|
|
- log.info("rand={}, flowPoolP={}", rand, flowPoolP);
|
|
|
- if (rand < flowPoolP) {
|
|
|
- if (flowPoolIndex < flowPoolRank.size()) {
|
|
|
- result.add(flowPoolRank.get(flowPoolIndex++));
|
|
|
- } else {
|
|
|
- break;
|
|
|
- }
|
|
|
- } else {
|
|
|
- if (rovPoolIndex < rovRecallRank.size()) {
|
|
|
- result.add(rovRecallRank.get(rovPoolIndex++));
|
|
|
- } else {
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- if (rovPoolIndex >= rovRecallRank.size()) {
|
|
|
- for (int i = flowPoolIndex; i < flowPoolRank.size() && result.size() < param.getSize(); i++) {
|
|
|
- result.add(flowPoolRank.get(i));
|
|
|
- }
|
|
|
- }
|
|
|
- if (flowPoolIndex >= flowPoolRank.size()) {
|
|
|
- for (int i = rovPoolIndex; i < rovRecallRank.size() && result.size() < param.getSize(); i++) {
|
|
|
- result.add(rovRecallRank.get(i));
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- Set<Long> videosSet = result.stream().map(r-> r.getVideoId()).collect(Collectors.toSet());
|
|
|
- List<Video> rovRecallRankNew = rovRecallRank.stream().filter(r -> !videosSet.contains(r.getVideoId())).collect(Collectors.toList());
|
|
|
- List<Video> flowPoolRankNew = flowPoolRank.stream().filter(r -> !videosSet.contains(r.getVideoId())).collect(Collectors.toList());
|
|
|
- List<Video> resultWithDnsity = RankProcessorDensity.mergeDensityControl(result,
|
|
|
- rovRecallRankNew, flowPoolRankNew, densityRules);
|
|
|
- return new RankResult(resultWithDnsity);
|
|
|
- }
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
|
|
|
}
|