zoukankan      html  css  js  c++  java
  • 简单的敏感词汇实现

    import java.util.HashMap;
    import java.util.Map;
    
    public class Main {
        class TreeNode {
    
            //当前节点的所有子节点
            public Map<Character, TreeNode> subNodes = new HashMap();
            //判断当前树的结尾,即敏感词汇的结束
            private boolean end = false;//如果是true 就结束,否则继续往下判断
    
            //添加敏感词构造的的树
            public void addSubNodes(Character c, TreeNode node) {
                subNodes.put(c, node);
            }
    
            //获取树节点
            public TreeNode getTreeNode(Character c) {
                return subNodes.get(c);
            }
    
            //判断当前字符是否为敏感词
            public boolean iskeyWords(Character c) {
                return end;
            }
    
            //设置敏感词的结束
            public void setKeyWords(boolean end) {
                this.end = end;
            }
    
            //获取当前结点子节点的数量
            public int getSubNodeCount() {
                return subNodes.size();
            }
    
        }
    
        TreeNode rootNode = new TreeNode();//建立树,空树
    
        /**
         * 将敏感词汇添加到树中
         */
        public void addWords(String TextLine) {
            TreeNode tempNode = rootNode;
    
        /* 这里面可以添加一些对传入字符串的过滤*/
            for (int i = 0; i < TextLine.length(); ++i) {
                Character c = TextLine.charAt(i);
                TreeNode node = tempNode.getTreeNode(c);
                if (node == null) {//树的初始化
                    node = new TreeNode();
                    tempNode.addSubNodes(c, node);
                }
                tempNode = node;
                if (TextLine.length() - 1 == i) {
                    //如果敏感词汇结束,就是设置end为ture
                    tempNode.setKeyWords(true);
                }
            }
        }
        /**
         * 过滤敏感词的方法
         */
        public String filter(String message) {
    
            String REPLACEMENT = "*";//替换词
            TreeNode tempNode = rootNode;
            StringBuilder sb = new StringBuilder();//输出容器
    
            int begin = 0; //起始点
            int postion = 0;//当前活动节点
    
            //开始过滤
            while (postion < message.length()) {
                char c = message.charAt(postion);
                tempNode = tempNode.getTreeNode(c);
                if (tempNode == null) {
                    //说明当前匹配已经结束
                    sb.append(c);
                    postion = begin + 1;
                    begin = postion;
                    tempNode = rootNode;
                } else if (tempNode.iskeyWords(c)) {
                    for(int i = begin;i<=postion;++i){
                        sb.append(REPLACEMENT);
                    }
                    postion++;
                    begin = postion;
                } else {
                    ++postion;
                }
    
            }
            sb.append(message.substring(begin));
            return sb.toString();
        }
    
        public static void main(String args[]) {
    
            Main s = new Main();
            s.addWords("色情");//输入敏感词汇
            s.addWords("好色");
            System.out.print(s.filter("你好色情"));//输入要过滤的语句
    
        }
    }
    

  • 相关阅读:
    xtrabackup执行备份要拥有的权限
    CentOS 7 下yum安装xtrabackup备份工具
    MySQL read_only选项的作用
    Linux进程管理命令
    MySQL二进制日志中保存的上下文信息
    MySQLdb的安装
    MySQL The password hash doesn't have the expected format.
    web框架本质
    进程和线程
    good blog
  • 原文地址:https://www.cnblogs.com/shuoli/p/7425376.html
Copyright © 2011-2022 走看看