|
@@ -1,46 +1,60 @@
|
|
|
package com.tzld.piaoquan.recommend.server.service.rank.processor;
|
|
|
import com.tzld.piaoquan.recommend.server.model.Video;
|
|
|
+import org.apache.commons.lang3.tuple.Pair;
|
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.*;
|
|
|
-
|
|
|
public class RankProcessorTagFilter {
|
|
|
|
|
|
- public static void tagFitlter(List<Video> rov, List<Video> flow, Map<String, Map<String, String>> rule) {
|
|
|
+ public static void processor(List<Video> rov, List<Video> flow, Map<String, Map<String, String>> rule) {
|
|
|
|
|
|
- Map<String, List<String>> tag2Dates = new HashMap<>();
|
|
|
-// Map<String, List<String>> tag2Apps = new HashMap<>();
|
|
|
- Map<String, Integer> tag2Hour = new HashMap<>();
|
|
|
+ Map<String, Double> tag2Rate = new HashMap<>();
|
|
|
+ Map<String, List<Pair<String, String>>> tag2Dates = new HashMap<>();
|
|
|
for (Map.Entry<String, Map<String, String>> entry : rule.entrySet()){
|
|
|
String key = entry.getKey();
|
|
|
Map<String, String> value = entry.getValue();
|
|
|
- if (value.containsKey("date")){
|
|
|
- tag2Dates.put(key, Arrays.asList(value.get("date").split(",")));
|
|
|
+ if (value.containsKey("filter_rate")){
|
|
|
+ tag2Rate.put(key, Double.valueOf(value.get("filter_rate")));
|
|
|
+ }
|
|
|
+ if (value.containsKey("filter_date")){
|
|
|
+ List<Pair<String, String>> tmpList = new ArrayList<>();
|
|
|
+ for (String tmp : value.get("filter_date").split(",")){
|
|
|
+ String start = tmp.split("-")[0];
|
|
|
+ String end = tmp.split("-")[1];
|
|
|
+ tmpList.add(Pair.of(start, end));
|
|
|
+ }
|
|
|
+ tag2Dates.put(key, tmpList);
|
|
|
}
|
|
|
-// if (value.containsKey("app")){
|
|
|
-// tag2Apps.put(key, Arrays.asList(value.get("app").split(",")));
|
|
|
-// }
|
|
|
- if (value.containsKey("hour")){
|
|
|
- tag2Hour.put(key, Integer.valueOf(value.get("hour")));
|
|
|
+ }
|
|
|
+
|
|
|
+ // 通过过滤概率获取本轮过滤tag集合
|
|
|
+ Set<String> filterTags = new HashSet<>();
|
|
|
+ for (Map.Entry<String, Double> entry : tag2Rate.entrySet()) {
|
|
|
+ String key = entry.getKey();
|
|
|
+ Double value = entry.getValue();
|
|
|
+ if (Math.random() <= value){
|
|
|
+ filterTags.add(key);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ // 获取系统时间
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
|
- String date = new SimpleDateFormat("yyyyMMdd").format(calendar.getTime());
|
|
|
- Integer hour = Integer.valueOf(new SimpleDateFormat("HH").format(calendar.getTime()));
|
|
|
+ String dateHour = new SimpleDateFormat("yyyyMMddHH").format(calendar.getTime());
|
|
|
|
|
|
+ //执行过滤
|
|
|
Iterator<Video> iterator = rov.iterator();
|
|
|
while (iterator.hasNext()) {
|
|
|
Video video = iterator.next();
|
|
|
List<String> tags = video.getTags();
|
|
|
boolean filter = false;
|
|
|
for (String tag : tags){
|
|
|
- if (tag2Dates.containsKey(tag) && !tag2Dates.get(tag).isEmpty() && !tag2Dates.get(tag).contains(date)){
|
|
|
- // 如果有日期存在,但日期没有命中,则不过滤,跳过即可。
|
|
|
+ if (!filterTags.contains(tag)){
|
|
|
continue;
|
|
|
}
|
|
|
- if (tag2Hour.containsKey(tag) && hour >= tag2Hour.get(tag)){
|
|
|
+ boolean flag = ifFiter(tag2Dates, tag, dateHour);
|
|
|
+ if (flag){
|
|
|
filter = true;
|
|
|
+ break;
|
|
|
}
|
|
|
}
|
|
|
if (filter){
|
|
@@ -54,12 +68,13 @@ public class RankProcessorTagFilter {
|
|
|
List<String> tags = video.getTags();
|
|
|
boolean filter = false;
|
|
|
for (String tag : tags){
|
|
|
- if (tag2Dates.containsKey(tag) && !tag2Dates.get(tag).isEmpty() && !tag2Dates.get(tag).contains(date)){
|
|
|
- // 如果有日期存在,但日期没有命中,则不过滤,跳过即可。
|
|
|
+ if (!filterTags.contains(tag)){
|
|
|
continue;
|
|
|
}
|
|
|
- if (tag2Hour.containsKey(tag) && hour >= tag2Hour.get(tag)){
|
|
|
+ boolean flag = ifFiter(tag2Dates, tag, dateHour);
|
|
|
+ if (flag){
|
|
|
filter = true;
|
|
|
+ break;
|
|
|
}
|
|
|
}
|
|
|
if (filter){
|
|
@@ -68,5 +83,16 @@ public class RankProcessorTagFilter {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public static boolean ifFiter(Map<String, List<Pair<String, String>>> tag2Dates, String tag, String dateHour){
|
|
|
+ if (!tag2Dates.containsKey(tag) || tag2Dates.get(tag).isEmpty()){
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ for (Pair<String, String> d: tag2Dates.get(tag)){
|
|
|
+ if (dateHour.compareTo(d.getLeft()) >= 0 && dateHour.compareTo(d.getRight()) < 0){
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
|
|
|
}
|