java使用hutool的dfa过滤敏感词
public int keyWordsFind(String content) {List<String> list = null;list = redisTemplate.opsForList().range("words_filter", 0, -1);if (CollectionUtils.isEmpty(list)) {try {org.springframework.core
·
public int keyWordsFind(String content) { List<String> list = null; list = redisTemplate.opsForList().range("words_filter", 0, -1); if (CollectionUtils.isEmpty(list)) { try { org.springframework.core.io.Resource resource = new ClassPathResource("data/words.txt"); InputStream is = resource.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String data = null; while ((data = br.readLine()) != null) { if (StringUtils.isNotEmpty(data)) { list.add(data); } } br.close(); isr.close(); is.close(); redisTemplate.opsForList().rightPushAll("words_filter", list); } catch (Exception e) { e.printStackTrace(); } } WordTree tree = new WordTree(); tree.addWords(list); //密集匹配原则:假如关键词有 ab,b,文本是abab,将匹配 [ab,b,ab] //贪婪匹配(最长匹配)原则:假如关键字a,ab,最长匹配将匹配[a, ab] List<String> matchAll = tree.matchAll(content,-1, false, false); if(matchAll.size()>0){ log.info("敏感词 {}",matchAll.toString()); } return matchAll.size(); }
更多推荐
所有评论(0)