- SensitiveWordInit
- SensitivewordEngine
public class SensitiveWordInit
{
/**
* 敏感词库
*/
public HashMap sensitiveWordMap;
/**
* 初始化敏感词
*
* @return
*/
public Map initKeyWord(List<String> sensitiveWords)
{
try
{
Set<String> keyWordSet = new HashSet<String>();
for (String s : sensitiveWords)
{
keyWordSet.add(s.trim());
}
addSensitiveWordToHashMap(keyWordSet);
}
catch (Exception e)
{
e.printStackTrace();
}
return sensitiveWordMap;
}
/**
* 封装敏感词库
*
* @param keyWordSet
*/
@SuppressWarnings("rawtypes")
private void addSensitiveWordToHashMap(Set<String> keyWordSet)
{
sensitiveWordMap = new HashMap(keyWordSet.size());
String key = null;
Map nowMap = null;
Map<String, String> newWorMap = null;
Iterator<String> iterator = keyWordSet.iterator();
while (iterator.hasNext())
{
key = iterator.next();
nowMap = sensitiveWordMap;
for (int i = 0; i < key.length(); i++)
{
char keyChar = key.charAt(i);
Object wordMap = nowMap.get(keyChar);
if (wordMap != null)
{
nowMap = (Map) wordMap;
}
else
{
newWorMap = new HashMap<String, String>();
newWorMap.put("isEnd", "0");
nowMap.put(keyChar, newWorMap);
nowMap = newWorMap;
}
if (i == key.length() - 1)
{
nowMap.put("isEnd", "1");
}
System.out.println("封装敏感词库过程:"+sensitiveWordMap);
}
System.out.println("查看敏感词库数据:" + sensitiveWordMap);
}
}
}
public class SensitivewordEngine
{
/**
* 敏感词库
*/
public static Map sensitiveWordMap = null;
/**
* 只过滤最小敏感词
*/
public static int minMatchTYpe = 1;
/**
* 过滤所有敏感词
*/
public static int maxMatchType = 2;
/**
* 敏感词库敏感词数量
*
* @return
*/
public static int getWordSize()
{
if (SensitivewordEngine.sensitiveWordMap == null)
{
return 0;
}
return SensitivewordEngine.sensitiveWordMap.size();
}
/**
* 是否包含敏感词
*
* @param txt
* @param matchType
* @return
*/
public static boolean isContaintSensitiveWord(String txt, int matchType)
{
boolean flag = false;
for (int i = 0; i < txt.length(); i++)
{
int matchFlag = checkSensitiveWord(txt, i, matchType);
if (matchFlag > 0)
{
flag = true;
}
}
return flag;
}
/**
* 获取敏感词内容
*
* @param txt
* @param matchType
* @return 敏感词内容
*/
public static Set<String> getSensitiveWord(String txt, int matchType)
{
Set<String> sensitiveWordList = new HashSet<String>();
for (int i = 0; i < txt.length(); i++)
{
int length = checkSensitiveWord(txt, i, matchType);
if (length > 0)
{
sensitiveWordList.add(txt.substring(i, i + length));
i = i + length - 1;
}
}
return sensitiveWordList;
}
/**
* 替换敏感词
*
* @param txt
* @param matchType
* @param replaceChar
* @return
*/
public static String replaceSensitiveWord(String txt, int matchType, String replaceChar)
{
String resultTxt = txt;
Set<String> set = getSensitiveWord(txt, matchType);
Iterator<String> iterator = set.iterator();
String word = null;
String replaceString = null;
while (iterator.hasNext())
{
word = iterator.next();
replaceString = getReplaceChars(replaceChar, word.length());
resultTxt = resultTxt.replaceAll(word, replaceString);
}
return resultTxt;
}
/**
* 替换敏感词内容
*
* @param replaceChar
* @param length
* @return
*/
private static String getReplaceChars(String replaceChar, int length)
{
String resultReplace = replaceChar;
for (int i = 1; i < length; i++)
{
resultReplace += replaceChar;
}
return resultReplace;
}
/**
* 检查敏感词数量
*
* @param txt
* @param beginIndex
* @param matchType
* @return
*/
public static int checkSensitiveWord(String txt, int beginIndex, int matchType)
{
boolean flag = false;
int matchFlag = 0;
char word = 0;
Map nowMap = SensitivewordEngine.sensitiveWordMap;
for (int i = beginIndex; i < txt.length(); i++)
{
word = txt.charAt(i);
nowMap = (Map) nowMap.get(word);
if (nowMap != null)
{
matchFlag++;
if ("1".equals(nowMap.get("isEnd")))
{
flag = true;
if (SensitivewordEngine.minMatchTYpe == matchType)
{
break;
}
}
}
else
{
break;
}
}
if (!flag)
{
matchFlag = 0;
}
return matchFlag;
}
public static void main(String[] args){
List<String> sensitiveWords = new ArrayList<>();
sensitiveWords.add("王八");
sensitiveWords.add("王八蛋");
sensitiveWords.add("王八羔子");
sensitiveWords.add("黄网");
sensitiveWords.add("图片");
sensitiveWordMap = new SensitiveWordInit().initKeyWord(sensitiveWords);
System.out.println(isContaintSensitiveWord("艹尼玛",1));
System.out.println(replaceSensitiveWord("你个王八羔子哦",1,"*"));
System.out.println(replaceSensitiveWord("你个王八羔子哦",2,"*"));
}