|
@@ -1,11 +1,10 @@
|
|
-package com.tzld.piaoquan.ad.engine.service.score.impl;
|
|
|
|
|
|
+package com.tzld.piaoquan.ad.engine.service.score;
|
|
|
|
|
|
import com.tzld.piaoquan.ad.engine.commons.score.ScoreParam;
|
|
import com.tzld.piaoquan.ad.engine.commons.score.ScoreParam;
|
|
import com.tzld.piaoquan.ad.engine.commons.util.ExtractorUtils;
|
|
import com.tzld.piaoquan.ad.engine.commons.util.ExtractorUtils;
|
|
import com.tzld.piaoquan.ad.engine.commons.util.NumUtil;
|
|
import com.tzld.piaoquan.ad.engine.commons.util.NumUtil;
|
|
import com.tzld.piaoquan.ad.engine.service.feature.Feature;
|
|
import com.tzld.piaoquan.ad.engine.service.feature.Feature;
|
|
import com.tzld.piaoquan.ad.engine.service.feature.FeatureService;
|
|
import com.tzld.piaoquan.ad.engine.service.feature.FeatureService;
|
|
-import com.tzld.piaoquan.ad.engine.service.score.RankService;
|
|
|
|
import com.tzld.piaoquan.ad.engine.service.score.convert.RequestConvert;
|
|
import com.tzld.piaoquan.ad.engine.service.score.convert.RequestConvert;
|
|
import com.tzld.piaoquan.ad.engine.service.score.dto.AdPlatformCreativeDTO;
|
|
import com.tzld.piaoquan.ad.engine.service.score.dto.AdPlatformCreativeDTO;
|
|
import com.tzld.piaoquan.ad.engine.service.score.param.RankRecommendRequestParam;
|
|
import com.tzld.piaoquan.ad.engine.service.score.param.RankRecommendRequestParam;
|
|
@@ -16,6 +15,10 @@ import org.apache.commons.lang3.StringUtils;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
+import java.io.BufferedReader;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.io.InputStream;
|
|
|
|
+import java.io.InputStreamReader;
|
|
import java.time.LocalDateTime;
|
|
import java.time.LocalDateTime;
|
|
import java.time.ZoneOffset;
|
|
import java.time.ZoneOffset;
|
|
import java.util.*;
|
|
import java.util.*;
|
|
@@ -23,13 +26,16 @@ import java.util.stream.Collectors;
|
|
|
|
|
|
@Slf4j
|
|
@Slf4j
|
|
@Service
|
|
@Service
|
|
-public class RankService680 implements RankService {
|
|
|
|
|
|
+public class RankService680 {
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
private FeatureService featureService;
|
|
private FeatureService featureService;
|
|
|
|
|
|
- @Override
|
|
|
|
- public AdRankItem adItemRank(RankRecommendRequestParam request) {
|
|
|
|
|
|
+ private Map<String, double[]> bucketsMap = new HashMap<>();
|
|
|
|
+
|
|
|
|
+ private Map<String, Double> bucketsLen = new HashMap<>();
|
|
|
|
+
|
|
|
|
+ public List<AdRankItem> adItemRank(RankRecommendRequestParam request) {
|
|
|
|
|
|
long ts = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")) / 1000;
|
|
long ts = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")) / 1000;
|
|
|
|
|
|
@@ -55,7 +61,14 @@ public class RankService680 implements RankService {
|
|
|
|
|
|
Map<String, String> d2Feature = videoFeature.getOrDefault("alg_cid_feature_vid_cf_rank", new HashMap<>());
|
|
Map<String, String> d2Feature = videoFeature.getOrDefault("alg_cid_feature_vid_cf_rank", new HashMap<>());
|
|
|
|
|
|
|
|
+ List<AdRankItem> adRankItems = new ArrayList<>(request.getAdIdList().size());
|
|
for (AdPlatformCreativeDTO dto : request.getAdIdList()) {
|
|
for (AdPlatformCreativeDTO dto : request.getAdIdList()) {
|
|
|
|
+ AdRankItem adRankItem = new AdRankItem();
|
|
|
|
+ adRankItem.setAdId(dto.getCreativeId());
|
|
|
|
+ adRankItem.setCreativeCode(dto.getCreativeCode());
|
|
|
|
+ adRankItem.setAdVerId(dto.getAdVerId());
|
|
|
|
+ adRankItem.setVideoId(request.getVideoId());
|
|
|
|
+
|
|
String cidStr = dto.getCreativeId().toString();
|
|
String cidStr = dto.getCreativeId().toString();
|
|
Map<String, String> cidFeatureMap = new HashMap<>();
|
|
Map<String, String> cidFeatureMap = new HashMap<>();
|
|
Map<String, Map<String, String>> cidFeature = allCidFeature.getOrDefault(cidStr, new HashMap<>());
|
|
Map<String, Map<String, String>> cidFeature = allCidFeature.getOrDefault(cidStr, new HashMap<>());
|
|
@@ -75,8 +88,22 @@ public class RankService680 implements RankService {
|
|
|
|
|
|
String title = b1Feature.getOrDefault("cidtitle", "");
|
|
String title = b1Feature.getOrDefault("cidtitle", "");
|
|
this.handleE1AndE2Feature(e1Feature, e2Feature, title, cidFeatureMap);
|
|
this.handleE1AndE2Feature(e1Feature, e2Feature, title, cidFeatureMap);
|
|
|
|
+
|
|
|
|
+ adRankItem.setFeatureMap(cidFeatureMap);
|
|
|
|
+
|
|
|
|
+ adRankItems.add(adRankItem);
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // 分桶
|
|
|
|
+ this.readBucketFile();
|
|
|
|
+ userFeatureMap = this.featureBucket(userFeatureMap);
|
|
|
|
+ for (AdRankItem adRankItem : adRankItems) {
|
|
|
|
+ Map<String, String> featureMap = adRankItem.getFeatureMap();
|
|
|
|
+ adRankItem.setFeatureMap(this.featureBucket(featureMap));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
@@ -371,6 +398,57 @@ public class RankService680 implements RankService {
|
|
return midActionStaticsMap;
|
|
return midActionStaticsMap;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private void readBucketFile() {
|
|
|
|
+ InputStream resourceStream = RankService680.class.getClassLoader().getResourceAsStream("20240609_bucket_274.txt");
|
|
|
|
+ if (resourceStream != null) {
|
|
|
|
+ try (BufferedReader reader = new BufferedReader(new InputStreamReader(resourceStream))) {
|
|
|
|
+ Map<String, double[]> bucketsMap = new HashMap<>();
|
|
|
|
+ Map<String, Double> bucketsLen = new HashMap<>();
|
|
|
|
+ String line;
|
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
|
+ // 替换空格和换行符,过滤空行
|
|
|
|
+ line = line.replace(" ", "").replaceAll("\n", "");
|
|
|
|
+ if (!line.isEmpty()) {
|
|
|
|
+ String[] rList = line.split("\t");
|
|
|
|
+ if (rList.length == 3) {
|
|
|
|
+ String key = rList[0];
|
|
|
|
+ double value1 = Double.parseDouble(rList[1]);
|
|
|
|
+ bucketsLen.put(key, value1);
|
|
|
|
+ double[] value2 = Arrays.stream(rList[2].split(","))
|
|
|
|
+ .mapToDouble(Double::valueOf)
|
|
|
|
+ .toArray();
|
|
|
|
+ bucketsMap.put(key, value2);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ this.bucketsMap = bucketsMap;
|
|
|
|
+ this.bucketsLen = bucketsLen;
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ log.error("something is wrong in parse bucket file:" + e);
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ log.error("no bucket file");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private Map<String, String> featureBucket(Map<String, String> featureMap) {
|
|
|
|
+ Map<String, String> newFeatureMap = new HashMap<>(featureMap.size());
|
|
|
|
+ for (Map.Entry<String, String> entry : featureMap.entrySet()) {
|
|
|
|
+ String name = entry.getKey();
|
|
|
|
+ double score = Double.parseDouble(entry.getValue());
|
|
|
|
+ // 注意:0值、不在分桶文件中的特征,会被过滤掉。
|
|
|
|
+ if (score > 1E-8 && this.bucketsMap.containsKey(name)) {
|
|
|
|
+ double[] buckets = this.bucketsMap.get(name);
|
|
|
|
+ int bucketNum = buckets.length + 1;
|
|
|
|
+ Double scoreNew = 1.0 / bucketNum * (ExtractorUtils.findInsertPosition(buckets, score) + 1.0);
|
|
|
|
+ newFeatureMap.put(name, String.valueOf(scoreNew));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return newFeatureMap;
|
|
|
|
+ }
|
|
|
|
+
|
|
public static class Tuple5 {
|
|
public static class Tuple5 {
|
|
public String f1;
|
|
public String f1;
|
|
public String f2;
|
|
public String f2;
|