zoukankan      html  css  js  c++  java
  • 敏感字符过滤算法

    package com.lala.teamtoneback.code.common.util;

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    /**
    * 算法思路:把敏感词的第一个字符取出来,作为比较对象。
    * 遍历整个字符串,如果发现字符跟敏感词第一个字符相同,
    * 就从字符串取出跟关键词相同长度的子串比较,如果相同就替换
    * @author Administrator
    *
    */
    public class WordFilter {
    private static Map<Character,List<String>> wordListToMap(List<String> sensitiveWordList){
    Map<Character,List<String>> result=new HashMap<Character,List<String>>();
    for (String s:sensitiveWordList){
    char c=s.charAt(0);
    List<String> strs=result.get(c);
    if (strs==null){
    strs=new ArrayList<String>();
    result.put(c,strs);
    }
    strs.add(s);
    }
    return result;
    }

    public static String filter(String src,List<String> sensitiveWordList){
    Map<Character,List<String>> wordMap=wordListToMap(sensitiveWordList);

    StringBuilder strb=new StringBuilder();
    String temp="";
    for (int i=0;i<src.length();i++){
    char c=src.charAt(i);
    String find=null;
    if (wordMap.containsKey(c)){
    List<String> words=wordMap.get(c);
    for (String s:words){
    temp=src.substring(i,(s.length()<=(src.length()-i))?i+s.length():i);
    if (s.equals(temp)){
    find=s;
    break;
    }
    }
    }
    if (find!=null){
    strb.append("****");

    i+=(find.length()-1);

    } else {
    strb.append(c);
    }
    }
    return strb.toString();
    }
    }

  • 相关阅读:
    多数据源 + Configuration中bean依赖注入顺序问题
    mysql 示例数据库安装
    mysql隔离级别与锁,接口并发响应速度的关系(1)
    management & Actuator
    SpEL
    N/A的含义
    设置U盘图标
    c语言指针
    Decorator(装饰器)
    Lambda Expression
  • 原文地址:https://www.cnblogs.com/zengdingxin/p/3183939.html
Copyright © 2011-2022 走看看