• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Java使用DFA算法实现过滤多家公司自定义敏感字功能详解

java 搞代码 4年前 (2022-01-05) 25次浏览 已收录 0个评论

这篇文章主要介绍了Java使用DFA算法实现过滤多家公司自定义敏感字功能,结合实例形式分析了DFA算法的实现原理及过滤敏感字的相关操作技巧,需要的朋友可以参考下

本文实例讲述了Java使用DFA算法实现过滤多家公司自定义敏感字功能。分享给大家供大家参考,具体如下:

背景

因为最近有通讯有个需求,说需要让多家客户公司可以自定义敏感词过滤掉他们自定义的规则,选择了DFA算法来做,不过和以前传统了DFA写法不太一样了

模式图

直接上代码

 public class KeywordFilter { //  private static ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); public static Map currentMap = new ConcurrentHashMap(); public static Map nowhash = null; public static Object wordMap;// map子节点 // 不建立对象 private KeywordFilter() { } private static String getKey(int companyId) { return "companyId" + companyId; } /* * <p>说明:清扫内容</p> * * @author:姚旭民 * * @data:2017-8-22 上午10:13:11 */ public static void clear() { try { currentMap.clear(); } catch (Exception e) { e.printStackTrace(); } finally { } } /* * <p>说明:各个渠道的过滤字符</p> * * @author:姚旭民 * * @data:2017-8-20 下午2:55:06 */ public static void saveKeywords(int companyId, List keywords) { try { Map tempAllMap = currentMap; String key = getKey(companyId); int l = keywords.size(); int il; Map tempMap; for (int i = 0; i <l; i++) { String key2 = keywords.get(i).trim();// 去掉空白 nowhash = currentMap; il = key2.length(); for (int j = 0; j <il; j++) { char word = key2.charAt(j); tempMap = (Map) nowhash.get(word); wordMap = nowhash.get(word); if (wordMap != null) {// 检查数据 if (!tempMap.containsKey(key)) { nowhash.put(key, 0); } nowhash = (HashMap) wordMap; } else { HashMap newWordHash = new HashMap(); newWordHash.put(key, "0"); nowhash.put(word, newWordHash); nowhash = newWordHash; } if (j == il - 1) { nowhash.put(key, "1"); } } } } catch (Exception e) { e.printStackTrace(); } finally { nowhash = null; wordMap = null; } } /* * <p>说明:替换掉对应的渠道规定掉敏感字</p> * * @author:姚旭民 * * @data:2017-8-20 上午11:41:47 */ public static List repword(int companyId, String txt) { Map tempMap = currentMap; List result = new ArrayList(); String key = getKey(companyId); nowhash = currentMap; int l = txt.length(); char word; String keywordStr = ""; String keyStatu; StringBuilder keyword = new StringBuilder();// 敏感字 for (int i = 0; i <l; i++) { word = txt.charAt(i); wordMap = nowhash.get(word); if (wordMap != null) {// 找到类似敏感字的字体,开始查询 keyword.append(word); Object te = nowhash = (HashMap) wordMap; // 遍历到这一步,就符合完整的关键字模板 if (nowhash.get(key) != null && nowhash.get(key).toString().equals("1")) {// 确定是敏感字,开始替换 if (i  0 ? keywordStr.substring(0, keywordStr.length() - 1) : keywordStr); return result; } /* * <p>说明:检查是否存在敏感字</p> * * @author:姚旭民 * * @data:2017-8-20 下午3:00:06 专门设计成私有的,如果没有理由,别改动他 */ private static int checkKeyWords(String txt, int companyId, int begin) { int result = 0; String key = getKey(companyId); try { nowhash = currentMap; int l = txt.length(); char word = 0; for (int i = begin; i <l; i++) { word = txt.charAt(i); wordMap = nowhash.get(word); if (wordMap != null) { result++; nowhash = (HashMap) wordMap; if (((String) nowhash.get(key)).equals("<p style="color:transparent">来源gao!%daima.com搞$代*!码网</p>1")) { nowhash = null; wordMap = null; return result; } } else { result = 0; break; } } } catch (Exception e) { e.printStackTrace(); } finally { nowhash = null; wordMap = null; return result; } } /* * <p>说明:返回检查的文本中包含的敏感字</p> * * @author:姚旭民 * * @data:2017-8-20 下午3:32:53 */ public static String getTxtKeyWords(String txt, int companyId) { String result = null; StringBuilder temp = new StringBuilder(); String key; int l = txt.length(); for (int i = 0; i  0) { key = (txt.substring(i, i + len));// 挑选出来的关键字 temp.append(key + ","); txt = txt.replaceAll(key, "");// 挑选出来的关键字替换成空白,加快挑选速度 l = txt.length(); } else { i++; } } if (temp.length() > 0) { result = temp.substring(0, temp.length() - 1); } return result; } /* * <p>说明:判断文中是否包含渠道规定的敏感字</p> * * @author:姚旭民 * * @data:2017-8-20 下午3:33:19 */ public boolean isKeyWords(String txt, int companyId) { for (int i = 0; i  0) { return true; } } return false; } public static void main(String[] arg) { List keywords = new ArrayList(); keywords.add("傻×"); keywords.add("汉奸"); keywords.add("草"); keywords.add("草泥马"); KeywordFilter.saveKeywords(1, keywords); String txt = "是傻×汉奸傻A傻B傻C傻D汉奸傻×草泥马"; List list = repword(1, txt); System.out.println("文中包含的敏感字为:" + list.get(1)); System.out.println("原文:" + txt); System.out.println("敏感字过滤后:" + list.get(0)); } } 

更多关于java算法相关内容感兴趣的读者可查看本站专题:《Java数据结构与算法教程》、《Java字符与字符串操作技巧总结》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》

希望本文所述对大家java程序设计有所帮助。

以上就是Java使用DFA算法实现过滤多家公司自定义敏感字功能详解的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Java使用DFA算法实现过滤多家公司自定义敏感字功能详解

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址