您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这期内容当中小编将会给大家带来有关如何在Java项目中利用DFA算法实现一个过滤敏感字功能,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
模式图
直接上代码
public class KeywordFilter { // private static ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); public static Map<String, HashMap> currentMap = new ConcurrentHashMap<String, HashMap>(); public static Map nowhash = null; public static Object wordMap;// map子节点 // 不建立对象 private KeywordFilter() { } private static String getKey(int companyId) { return "companyId" + companyId; } /* * <p>说明:清扫内容</p> * * * @data:2017-8-22 上午10:13:11 */ public static void clear() { try { currentMap.clear(); } catch (Exception e) { e.printStackTrace(); } finally { } } /* * <p>说明:各个渠道的过滤字符</p> * * * @data:2017-8-20 下午2:55:06 */ public static void saveKeywords(int companyId, List<String> 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<String, String> newWordHash = new HashMap<String, String>(); 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> * * * @data:2017-8-20 上午11:41:47 */ public static List<String> repword(int companyId, String txt) { Map tempMap = currentMap; List<String> result = new ArrayList<String>(); 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 < l - 1 && nowhash.get(txt.charAt(i + 1)) != null) {// 优先过滤长敏感词,去掉就槟城了优先过滤段敏感词 continue; } txt = txt.replaceAll(keyword.toString(), "*"); nowhash = currentMap; keywordStr += keyword.toString() + ","; i = i - keyword.length() + 1; l = txt.length();// 重新获取字符长度 keyword.delete(0, keyword.length());// 清空数据 } } else {// 这个字不是敏感字,直接排除 nowhash = currentMap; keyword.delete(0, keyword.length());// 清空数据 continue; } } // 清除内存指向 nowhash = null; wordMap = null; result.add(txt); result.add(keywordStr.length() - 1 > 0 ? keywordStr.substring(0, keywordStr.length() - 1) : keywordStr); return result; } /* * <p>说明:检查是否存在敏感字</p> * * * @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("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> * * * @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 < l;) { int len = checkKeyWords(txt, companyId, i); if (len > 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> * * @data:2017-8-20 下午3:33:19 */ public boolean isKeyWords(String txt, int companyId) { for (int i = 0; i < txt.length(); i++) { int len = checkKeyWords(txt, companyId, i); if (len > 0) { return true; } } return false; } public static void main(String[] arg) { List<String> keywords = new ArrayList<String>(); keywords.add("傻×"); keywords.add("汉*"); keywords.add("草"); keywords.add("草泥马"); KeywordFilter.saveKeywords(1, keywords); String txt = "是傻×汉*傻A傻B傻C傻D汉*傻×草泥马"; List<String> list = repword(1, txt); System.out.println("文中包含的敏感字为:" + list.get(1)); System.out.println("原文:" + txt); System.out.println("敏感字过滤后:" + list.get(0)); } }
上述就是小编为大家分享的如何在Java项目中利用DFA算法实现一个过滤敏感字功能了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。