视频70(敏感词校验交集校验)

master
Administrator 2 years ago
parent 5f5dfcd7c4
commit e90cbff146

@ -3,8 +3,11 @@ package com.mashibing.cache.controller;
import com.msb.framework.redis.RedisClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
@ -20,6 +23,9 @@ public class CacheController {
@Autowired
private RedisClient redisClient;
@Resource
private RedisTemplate<String, Object> redisTemplate;
@PostMapping(value = "/cache/hmset/{key}")
public void hmset(@PathVariable(value = "key") String key, @RequestBody Map<String, Object> map) {
log.info("【缓存模块】 hmset方法存储key = {}存储value = {}", key, map);
@ -44,6 +50,19 @@ public class CacheController {
redisClient.sAdd(key,value);
}
@PostMapping(value = "/cache/sinterstr/{key}/{sinterKey}")
public Set<Object> sinterStr(@PathVariable(value = "key")String key, @PathVariable String sinterKey,@RequestBody String... value){
log.info("【缓存模块】 sinterStr的交集方法存储key = {}sinterKey = {}存储value = {}", key, sinterKey,value);
//1、 存储数据到set集合
redisClient.sAdd(key,value);
//2、 需要将key和sinterKey做交集操作并拿到返回的set
Set<Object> result = redisTemplate.opsForSet().intersect(key, sinterKey);
//3、 将key删除
redisClient.delete(key);
//4、 返回交集结果
return result;
}
@GetMapping("/cache/hgetall/{key}")
public Map hGetAll(@PathVariable(value = "key")String key){

@ -44,6 +44,12 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- IK分词器-->
<dependency>
<groupId>com.janeluo</groupId>
<artifactId>ikanalyzer</artifactId>
<version>2012_u6</version>
</dependency>
</dependencies>

@ -4,6 +4,9 @@ import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import java.util.Set;
/**
* @author zjw
@ -17,4 +20,7 @@ public interface BeaconCacheClient {
@GetMapping(value = "/cache/get/{key}")
String getString(@PathVariable(value = "key") String key);
@PostMapping(value = "/cache/sinterstr/{key}/{sinterKey}")
Set<Object> sinterStr(@PathVariable(value = "key")String key, @PathVariable String sinterKey, @RequestBody String... value);
}

@ -1,20 +1,63 @@
package com.mashibing.strategy.filter.impl;
import com.mashibing.common.constant.CacheConstant;
import com.mashibing.common.model.StandardSubmit;
import com.mashibing.strategy.client.BeaconCacheClient;
import com.mashibing.strategy.filter.StrategyFilter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;
import java.io.IOException;
import java.io.StringReader;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
/**
*
* @author zjw
* @description
*/
@Service(value = "dirtyword")
@Slf4j
public class DirtyWordStrategyFilter implements StrategyFilter {
@Service(value = "dirtyword")
@Slf4j
public class DirtyWordStrategyFilter implements StrategyFilter {
@Autowired
private BeaconCacheClient cacheClient;
@Override
public void strategy(StandardSubmit submit) {
log.info("【策略模块-敏感词校验】 校验ing…………");
//1、 获取短信内容
String text = submit.getText();
//2、 对短信内容进行分词,并且将分析内容存储到集合中
Set<String> contents = new HashSet<>();
StringReader reader = new StringReader(text);
IKSegmenter ik = new IKSegmenter(reader,false);
Lexeme lex = null;
while(true){
try {
if ((lex = ik.next()) == null) break;
} catch (IOException e) {
log.info("【策略模块-敏感词校验】 IK分词器在处理短信内容时出现异常 e = {}" ,e.getMessage());
}
contents.add(lex.getLexemeText());
}
//3、 调用Cache缓存模块的交集方法拿到结果
Set<Object> dirtyWords = cacheClient.sinterStr(UUID.randomUUID().toString(), CacheConstant.DIRTY_WORD, contents.toArray(new String[]{}));
//4、 根据返回的set集合判断是否包含敏感词
if(dirtyWords != null && dirtyWords.size() > 0){
//5、 如果有敏感词,抛出异常 / 其他操作。。
log.info("【策略模块-敏感词校验】 短信内容包含敏感词信息, dirtyWords = {}",dirtyWords);
// 还需要做其他处理
}
}
}

Loading…
Cancel
Save