|
@@ -0,0 +1,46 @@
|
|
|
+package com.tzld.longarticle.recommend.server.service.filter.strategy;
|
|
|
+
|
|
|
+import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValue;
|
|
|
+import com.tzld.longarticle.recommend.server.model.Content;
|
|
|
+import com.tzld.longarticle.recommend.server.service.filter.FilterParam;
|
|
|
+import com.tzld.longarticle.recommend.server.service.filter.FilterResult;
|
|
|
+import com.tzld.longarticle.recommend.server.service.filter.FilterStrategy;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+@Component
|
|
|
+@Slf4j
|
|
|
+public class KeywordStrategy implements FilterStrategy {
|
|
|
+
|
|
|
+ @ApolloJsonValue("${keywords:[]}")
|
|
|
+ private static List<String> keywords;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public FilterResult filter(FilterParam param) {
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
+ FilterResult filterResult = new FilterResult();
|
|
|
+ List<String> result = new ArrayList<>();
|
|
|
+ List<Content> contents = param.getContents();
|
|
|
+ List<Content> filterContents = new ArrayList<>();
|
|
|
+ for (Content content : contents) {
|
|
|
+ for (String keyword : keywords) {
|
|
|
+ if (content.getTitle().contains(keyword)) {
|
|
|
+ content.setFilterReason("关键词过滤");
|
|
|
+ filterContents.add(content);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!StringUtils.hasText(content.getFilterReason())) {
|
|
|
+ result.add(content.getId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ filterResult.setContentIds(result);
|
|
|
+ filterResult.setFilterContent(filterContents);
|
|
|
+ log.info("KeywordStrategy cost:{}", System.currentTimeMillis() - start);
|
|
|
+ return filterResult;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|