1. 初始化敏感词库,将敏感词加入到HashMap中,构建DFA算法模型

package com.datago.common.utils.sensitive;import java.util.*;/*** @ProjectName innovate  初始化敏感词库,将敏感词加入到HashMap中,构建DFA算法模型* @Package com.datago.common.utils.sensitive* @Name SensitiveWordInit* @Author HB* @Date 2022/1/25 18:12* @Version 1.0*/public class SensitiveWordInit {@SuppressWarnings("rawtypes")public static HashMap sensitiveWordMap;public SensitiveWordInit() {super();}/*** 初始化词库** @param datas 敏感词集合* @return*/public static HashMap init(String datas) {addSensitiveWord(datas);return sensitiveWordMap;}private static void addSensitiveWord(String word) {sensitiveWordMap = new HashMap(word.length());Map<String, Object> now = null;Map now2 = null;now2 = sensitiveWordMap;for (int i = 0; i < word.length(); i++) {char key_word = word.charAt(i);Object obj = now2.get(key_word);if (obj != null) { //存在now2 = (Map) obj;} else { //不存在now = new HashMap<>();now.put("isEnd", "0");now2.put(key_word, now);now2 = now;}if (i == word.length() - 1) {now2.put("isEnd", "1");}}}/*** 获取内容中的敏感词** @param text      内容* @param matchType 匹配规则 1=不最佳匹配,2=最佳匹配* @return*/public static List<String> getSensitiveWord(String text, int matchType) {List<String> words = new ArrayList<String>();Map now = sensitiveWordMap;int count = 0;  //初始化敏感词长度int start = 0; //标志敏感词开始的下标for (int i = 0; i < text.length(); i++) {char key = text.charAt(i);now = (Map) now.get(key);if (now != null) { //存在count++;if (count == 1) {start = i;}if ("1".equals(now.get("isEnd"))) { //敏感词结束now = sensitiveWordMap; //重新获取敏感词库words.add(text.substring(start, start + count)); //取出敏感词,添加到集合count = 0; //初始化敏感词长度}} else { //不存在now = sensitiveWordMap;//重新获取敏感词库if (count == 1 && matchType == 1) { //不最佳匹配count = 0;} else if (count == 1 && matchType == 2) { //最佳匹配words.add(text.substring(start, start + count));count = 0;}}}return words;}
}

2. 敏感词过滤

package com.datago.common.utils.sensitive;import com.datago.common.core.redis.RedisCache;
import com.datago.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import java.util.*;/*** @ProjectName innovate 敏感词过滤* @Package com.datago.common.utils.sensitive* @Name SensitivewordFilter* @Author HB* @Date 2022/1/25 18:14* @Version 1.0*/@Component
public class SensitivewordFilter {private static RedisCache redisCache;@Autowiredpublic void setRedisCache(RedisCache redisCache) {SensitivewordFilter.redisCache = redisCache;}@SuppressWarnings("rawtypes")private static Map sensitiveWordMap = null;public static void initSensitiveWord(String datas) {sensitiveWordMap = SensitiveWordInit.init(datas);}/*** 替换敏感字字符** @param txt* @param matchType* @param replaceChar 替换字符,默认** @author HB* @version 1.0*/public static String replaceSensitiveWord(String datas, String txt, int matchType, String replaceChar) {if (sensitiveWordMap == null) {initSensitiveWord(datas);}String resultTxt = txt;//matchType = 1;      //最小匹配规则//matchType= 2;      //最大匹配规则List<String> set = SensitiveWordInit.getSensitiveWord(txt, matchType);     //获取所有的敏感词Iterator<String> iterator = set.iterator();String word = null;String replaceString = null;while (iterator.hasNext()) {word = iterator.next();replaceString = getReplaceChars(replaceChar, word.length());resultTxt = resultTxt.replaceAll(word, replaceString);}return resultTxt;}/*** 获取替换字符串** @param replaceChar* @param length* @return* @author HB* @version 1.0*/private static String getReplaceChars(String replaceChar, int length) {String resultReplace = replaceChar;if (length > 6) {length = 6;}for (int i = 1; i < length; i++) {resultReplace += replaceChar;}return resultReplace;}/*** 过滤敏感词汇** @param sensitiveTxt 输入数据* @return com.datago.common.core.domain.AjaxResult* @Author HB* @Date 2022/1/27 10:03**/public static String filterSensitive(String sensitiveTxt) {//从缓存中提取数据敏感词汇Map<String, String> datas = redisCache.getCacheObject("treeSensitive");//替换敏感词汇String updateTxt = null;for (Map.Entry<String, String> entry : datas.entrySet()) {SensitivewordFilter.initSensitiveWord(entry.getKey());if (StringUtils.isNotEmpty(updateTxt)) {updateTxt = replaceSensitiveWord(entry.getKey(), updateTxt, 1, entry.getValue());} else {updateTxt = replaceSensitiveWord(entry.getKey(), sensitiveTxt, 1, entry.getValue());}}return updateTxt;}}

3.应用

   /*** 过滤datago_sensitive敏感词汇* sensitiveTxt  传参*/@Log(title = "过滤敏感词汇")@GetMapping("/filterSensitive/{sensitiveTxt}")public AjaxResult filterSensitive(@PathVariable(value = "sensitiveTxt") String sensitiveTxt) {String s = SensitivewordFilter.filterSensitive(sensitiveTxt);return AjaxResult.success(s);}

4.参考文献

https://www.hutool.cn/docs/#/dfa/DFA%E6%9F%A5%E6%89%BE

Java使用DFA算法处理敏感词汇相关推荐

  1. java使用DFA算法实现敏感词过滤

    Java使用DFA算法实现敏感词过滤 DFA,全称 Deterministic Finite Automaton 即确定有穷自动机. 其特征为:有一个有限状态集合和一些从一个状态通向另一个状态的边,每 ...

  2. spring boot 使用DFA算法实现敏感词过滤

    spring boot 使用DFA算法实现敏感词过滤 敏感词.文字过滤是一个网站必不可少的功能,如何设计一个好的.高效的过滤算法是非常有必要的. DFA算法简介 DFA全称为:Deterministi ...

  3. 一种基于DFA算法的敏感词检测JAVA程序片段

    本文章提供一种基于DFA算法的敏感词检测JAVA程序片段,如下: 1.构造多叉树数据结构 import org.jetbrains.annotations.NotNull;/*** 多叉树* @aut ...

  4. 基于PHP的DFA算法(敏感词过滤)

    基于PHP的DFA算法(敏感词过滤) 看到网上很多的DFA算法,很多都有不同程度的问题,自己修改了一下,亲测没有问题,用在系统中过滤敏感词汇,比正则匹配的速度快很多. class DFA {priva ...

  5. java dfa 敏感词_java利用DFA算法实现敏感词过滤功能

    前言 敏感词过滤应该是不用给大家过多的解释吧?讲白了就是你在项目中输入某些字(比如输入xxoo相关的文字时)时要能检 测出来,很多项目中都会有一个敏感词管理模块,在敏感词管理模块中你可以加入敏感词,然 ...

  6. dfa算法 java_Java实现DFA算法对敏感词、广告词过滤功能示例

    一.前言 开发中经常要处理用户一些文字的提交,所以涉及到了敏感词过滤的功能,参考资料中DFA有穷状态机算法的实现,创建有向图.完成了对敏感词.广告词的过滤,而且效率较好,所以分享一下. 具体实现: 1 ...

  7. java dfa_基于java实现DFA算法代码实例

    DFA简介 DFA全称为:Deterministic Finite Automaton,即确定有穷自动机.(自己百度吧) 直接代码: 敏感词实体类 package com.nopsmile.dfa; ...

  8. DFA算法实现敏感词过滤

    写项目时,得到一个新的需求,即实现敏感词的过滤,上网查了下,有几种实现方法,采取了DFA算法,即确定的有穷自动机算法,当初学习编译原理的时候为啥没想到DFA还能这么做,看来眼界和意识还是不够,得锻炼. ...

  9. DFA算法进行敏感词过滤

    1.新建敏感词文本new_adress.txt,进行添加敏感词 2.代码 # -*- coding:utf-8 -*- import timetime1 = time.time() "&qu ...

  10. 下发策略,DFA算法优化---敏感词查询

    1.定义 有穷自动机FA(Finite Automaton)的每一步操作都是确定的,因此可称为确定型有穷自动机.确定有穷自动机DFA(Deterministic Finite Automaton)就是 ...

最新文章

  1. 数据库内核月报 - 2017年12月
  2. python怎么输入代码-如何编写python代码
  3. 配置Java_Home,临时环境变量信息
  4. 小学数学加减法测试软件,小学生数学加减测试题
  5. SQLite剖析之异步IO模式、共享缓存模式和解锁通知
  6. 编译lua5.3.2报错提示libreadline.so存在未定义的引用解决方法
  7. 电信光猫の破解使用路由
  8. android 蓝牙连接苹果手机号码,苹果与android蓝牙连接怎么实现
  9. chrome浏览器恢复书签方法
  10. android webview 真正实现---保存整个网页源码
  11. 【知识点和练习题】心田花开:二年级语文汉语拼音补习
  12. speedoffice(PPT)怎么设置文字竖向
  13. RBP系统管理之业务角色管理
  14. 【烈日炎炎战后端】Nginx(0.3万字)
  15. 利用MAPI实现邮件收发(VC++)
  16. 网站建设需要多少钱 开发一个网站有哪些费用
  17. 光场相机重聚焦原理②——Lytro Illum记录光场
  18. 微信小程序wxss公共样式
  19. java官网以及java官网下载地址
  20. 删除Symbian模拟器中测试程序的方法

热门文章

  1. 蒙提霍尔问题(三门问题)的思考与贝叶斯分析
  2. python echarts接口_GitHub - jllan/pyecharts: Python Echarts Plotting Library
  3. 超定方程组的最小二乘解
  4. GoldenGate Enterprise Manager Plug-In(12.1.0.3.0) 部署文档
  5. TDA2030功放电路图
  6. 微信小程序开挂模式即将启动
  7. 手机死机短信 死机,狂震,黑屏短信下载,能让mtk手机震动黑屏死机
  8. 公安机关计算机网络安全协议,计算机信息网络国际联网单位网络安全协议合同范本...
  9. WidsMob ImageConvert for Mac(图片格式转换器)
  10. MTK 6589 native exif generation