java DFA算法模型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; /** * 敏感词过滤器 */ public class Sensitive { /** 敏感词匹配规则 */ public final static int MINIMUM = 1 ; // 最小匹配规则 public final static int MAXIMUM = 2 ; // 最大匹配规则 private final Map<String, Object> sensitive; // 敏感词库 public Sensitive(Set<String> keywords) { // 初始化敏感词容器,减少扩容操作 this .sensitive = new HashMap<>(keywords.size()); // 将敏感词加入到HashMap中,构建DFA算法模型 this .initialize(keywords); } /** 初始化敏感词库 */ @SuppressWarnings ( "unchecked" ) private void initialize( final Set<String> keywords) { for (String keyword : keywords) { Map nowMap = this .sensitive; for ( int i = 0 ; i < keyword.length(); i++) { char keyChar = keyword.charAt(i); // 转换成char型 Object wordMap = nowMap.get(keyChar); if (wordMap != null ) { // 存在该key则直接赋值 nowMap = (Map) wordMap; } else { // 不存在则则构建一个map,同时将isEnd设置为0,因为他不是最后一个 Map<String, String> newWordMap = new HashMap<>(( 1 )); newWordMap.put( "isEnd" , "0" ); // 不是最后一个 nowMap.put(keyChar, newWordMap); nowMap = newWordMap; } if (i == keyword.length() - 1 ) { nowMap.put( "isEnd" , "1" ); // 最后一个 } } } } /** * 检查文字中是否包含敏感字符 * @param text 待检测的文本 * @param starting 开始位置 * @param match 匹配规则(1最小匹配规则;2最大匹配规则) * @return 存在则返回敏感词字符的长度,不存在返回0 */ private int checking( final String text, final int starting , final int match) { boolean ending = false ; // 敏感词结束标识位(用于敏感词只有1位的情况) int matching = 0 ; // 匹配标识数默认为0 Map nowMap = this .sensitive; for ( int index = starting ; index < text.length(); index++) { char word = text.charAt(index); nowMap = (Map) nowMap.get(word); // 获取指定key if (nowMap == null ) break ; // 不存在则直接返回 matching++; // 找到相应key,匹配标识+1 // 存在则判断是否为最后一个,是则结束循环,返回匹配标识数 if ( "1" .equals(nowMap.get( "isEnd" ))) { ending = true ; // 结束标志位为true if (Sensitive.MINIMUM == match) { break ; // 最小规则,直接返回,最大规则还需继续查找 } } } if (matching < 1 || !ending) { // 长度必须大于等于1 matching = 0 ; } return matching; } /** * 获取文字中的敏感词 * @param text 待查找的字符串 * @param match 匹配规则(1最小匹配规则;2最大匹配规则) */ private Set<String> searching( final String text, final int match) { final Set<String> sensitive = new HashSet<>(text.length()); for ( int index = 0 , length = text.length(); index < length ; index++) { final int matching = this .checking(text, index, match); // 敏感字符检查 if (matching > 0 ) { // 存在则加入集合中 sensitive.add(text.substring(index, index + matching)); index = index + matching - 1 ; // 减1的原因是因为for会自增 } } return sensitive; } /** * 判断文字是否包含敏感字符 * @param text 待检测的文本 * @param match 匹配规则(1最小匹配规则;2最大匹配规则) */ public final boolean contains( final String text, final int match) { for ( int index = 0 , length = text.length(); index < length; index++) { final int matching = this .checking(text, index, match); if (matching > 0 ) { return true ; // 包含 } } return false ; // 不包含 } /** * 使用“*”替换敏感字字符 * @param text 待替换的字符 * @param match 匹配规则(1最小匹配规则;2最大匹配规则) */ public final String replace(String text, int match) { final String replace = "*" ; return replace(text, match, replace); } /** * 替换敏感字字符 * @param text 待替换的字符 * @param match 匹配规则(1最小匹配规则;2最大匹配规则) * @param replace 替换字符,默认* */ public final String replace( final String text, final int match, final String replace) { String result = text; // 获取所有的敏感词 final Set<String> sensitive = this .searching(text, match); // 替换同等长度的字符 for (String element : sensitive) { final int length = element.length(); final String newing = this .newing(replace, length); result = result.replaceAll(element, newing); } return result; } /** * 获取替换字符串 * @param replace 替换字符 * @param length 替换长度 */ private String newing( final String replace, final int length) { final StringBuilder builder = new StringBuilder(); for ( int index = 0 ; index < length; index++) { builder.append(replace); } return builder.toString(); } } |
java 得dfa敏感词过滤算法!供大家参考!
分类:
java
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!