• 大小: 14KB
    文件类型: .zip
    金币: 2
    下载: 1 次
    发布日期: 2021-06-17
  • 语言: Java
  • 标签: java  

资源简介

用java实现根据用户传进来的话语是否包含敏感词,如果包含敏感词用*来替代输出。

资源截图

代码片段和文件信息

package com.chenssy.keyword;

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

/**
 * @Description: 敏感词过滤
 * @Project:test
 */
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();
}

/**
 * 判断文字是否包含敏感字符
 * @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;
}

/**
 * 获取文字中的敏感词
 * @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;
}

/**
 * 替换敏感字字符
 * @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;
}

/**
 * 获取替换字符串
 * @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;
}

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

 * @param txt
 * @param beginIndex
 * @param matchType
 * @return,如果存在,则返回敏感词字符的长度,不存在返回0
 * @version 1.0
 */
@SuppressWarnings({ “rawtypes“})
public int CheckSensitiveWord(String txtint beginIndexint matchType){
boolean  flag = false;    //敏感词结束标识位:用于敏感词只有1位的情况
int matchFlag = 0;     //匹配标识数默认为0
char word = 0;
Map nowMap = sensitiveWordMap;
for(int i = beginIndex; i < txt.length() ; i++){
word = txt.charAt(i);
nowMap = (Map) nowMap.ge

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-08-29 21:15  敏感词过滤\
     目录           0  2018-08-29 21:15  敏感词过滤\.git\
     文件          23  2018-08-29 21:15  敏感词过滤\.git\HEAD
     文件         130  2018-08-29 21:15  敏感词过滤\.git\config
     文件          73  2018-08-29 21:15  敏感词过滤\.git\description
     目录           0  2018-08-29 21:15  敏感词过滤\.git\hooks\
     文件         478  2018-08-29 21:15  敏感词过滤\.git\hooks\applypatch-msg.sample
     文件         896  2018-08-29 21:15  敏感词过滤\.git\hooks\commit-msg.sample
     文件         189  2018-08-29 21:15  敏感词过滤\.git\hooks\post-update.sample
     文件         424  2018-08-29 21:15  敏感词过滤\.git\hooks\pre-applypatch.sample
     文件        1642  2018-08-29 21:15  敏感词过滤\.git\hooks\pre-commit.sample
     文件        1348  2018-08-29 21:15  敏感词过滤\.git\hooks\pre-push.sample
     文件        4951  2018-08-29 21:15  敏感词过滤\.git\hooks\pre-rebase.sample
     文件        1239  2018-08-29 21:15  敏感词过滤\.git\hooks\prepare-commit-msg.sample
     文件        3611  2018-08-29 21:15  敏感词过滤\.git\hooks\update.sample
     目录           0  2018-08-29 21:15  敏感词过滤\.git\info\
     文件         240  2018-08-29 21:15  敏感词过滤\.git\info\exclude
     目录           0  2018-08-29 21:15  敏感词过滤\.git\objects\
     目录           0  2018-08-29 21:19  敏感词过滤\.git\objects\info\
     目录           0  2018-08-29 21:19  敏感词过滤\.git\objects\pack\
     目录           0  2018-08-29 21:15  敏感词过滤\.git\refs\
     目录           0  2018-08-29 21:19  敏感词过滤\.git\refs\heads\
     目录           0  2018-08-29 21:19  敏感词过滤\.git\refs\tags\
     文件        3745  2018-08-29 21:05  敏感词过滤\SensitiveWordInit.java
     文件        4794  2018-08-29 21:04  敏感词过滤\SensitivewordFilter.java

评论

共有 条评论