• 大小: 0.03M
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-14
  • 语言: 其他
  • 标签: 其他  

资源简介

fafb53a84c697f74f53ba9cdfb39bd29.rar

资源截图

代码片段和文件信息

package com.chenssy.keyword;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @Description: 敏感词过滤
 * @Project:test
 * @Author : chenming
 * @Date : 2014年4月20日 下午4:17:15
 * @version 1.0
 */
public class SensitivewordFilter {
@SuppressWarnings(“rawtypes“)
private Map sensitiveWordMap = null;
public static int minMatchTYpe = 1;      //最小匹配规则
public static int maxMatchType = 2;      //最大匹配规则

/**
 * 构造函数,初始化敏感词库
 */
public SensitivewordFilter(){
sensitiveWordMap = new SensitiveWordInit().initKeyWord();
}

/**
 * 判断文字是否包含敏感字符
 * @author chenming 
 * @date 2014年4月20日 下午4:28:30
 * @param txt  文字
 * @param matchType  匹配规则 1:最小匹配规则,2:最大匹配规则
 * @return 若包含返回true,否则返回false
 * @version 1.0
 */
public boolean isContaintSensitiveWord(String txtint matchType){
boolean flag = false;
for(int i = 0 ; i < txt.length() ; i++){
int matchFlag = this.CheckSensitiveWord(txt i matchType); //判断是否包含敏感字符
if(matchFlag > 0){    //大于0存在,返回true
flag = true;
}
}
return flag;
}

/**
 * 获取文字中的敏感词
 * @author chenming 
 * @date 2014年4月20日 下午5:10:52
 * @param txt 文字
 * @param matchType 匹配规则 1:最小匹配规则,2:最大匹配规则
 * @return
 * @version 1.0
 */
public Set getSensitiveWord(String txt  int matchType){
Set sensitiveWordList = new HashSet();

for(int i = 0 ; i < txt.length() ; i++){
int length = CheckSensitiveWord(txt i matchType);    //判断是否包含敏感字符
if(length > 0){    //存在加入list中
sensitiveWordList.add(txt.substring(i i+length));
i = i + length - 1;    //减1的原因,是因为for会自增
}
}

return sensitiveWordList;
}

/**
 * 替换敏感字字符
 * @author chenming 
 * @date 2014年4月20日 下午5:12:07
 * @param txt
 * @param matchType
 * @param replaceChar 替换字符,默认*
 * @version 1.0
 */
public String replaceSensitiveWord(String txtint matchTypeString replaceChar){
String resultTxt = txt;
Set set = getSensitiveWord(txt matchType);     //获取所有的敏感词
Iterator 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;
}

/**
 * 获取替换字符串
 * @author chenming 
 * @date 2014年4月20日 下午5:21:19
 * @param replaceChar
 * @param length
 * @return
 * @version 1.0
 */
private String getReplaceChars(String replaceCharint length){
String resultReplace = replaceChar;
for(int i = 1 ; i < length ; i++){
resultReplace += replaceChar;
}

return resultReplace;
}

/**
 * 检查文字中是否包含敏感字符,检查规则如下:

 * @author chenming 
 * @date 2014年4月20日 下午4:31:03
 * @param txt
 * @param beginIndex
 * @param matchType
 * @return,如果存在,则返回敏感词字符的长度,不存在返回0
 * @version 1.0
 */
@Suppres

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       5173  2014-05-25 14:47  filter\SensitivewordFilter.java

     文件       4006  2014-05-25 14:47  filter\SensitiveWordInit.java

     文件     176089  2018-09-11 14:09  filter\tb_filter.sql

     目录          0  2018-09-11 15:08  filter

----------- ---------  ---------- -----  ----

               185268                    4


评论

共有 条评论