您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Java实现敏感词过滤代码

2024/5/4 20:43:46发布30次查看
import java.io.bufferedreader; import java.io.file; import java.io.fileinputstream; import java.io.inputstreamreader; import java.util.hashmap; import java.util.hashset; import java.util.iterator; import java.util.map; import java.util.set; /** * @description: 初始化敏感词库,将敏感词加入到hashmap中,构建dfa算法模型 * @project:test * @author : chenming * @date : 2014年4月20日 下午2:27:06 * @version 1.0 */ public class sensitivewordinit { private string encoding = gbk; //字符编码 @suppresswarnings(rawtypes) public hashmap sensitivewordmap; public sensitivewordinit(){ super(); } /** * @author chenming * @date 2014年4月20日 下午2:28:32 * @version 1.0 */ @suppresswarnings(rawtypes) public map initkeyword(){ try { //读取敏感词库 set keywordset = readsensitivewordfile(); //将敏感词库加入到hashmap中 addsensitivewordtohashmap(keywordset); //spring获取application,然后application.setattribute(sensitivewordmap,sensitivewordmap); } catch (exception e) { e.printstacktrace(); } return sensitivewordmap; } /** * 读取敏感词库,将敏感词放入hashset中,构建一个dfa算法模型:
* 中 = { * isend = 0 * 国 = {
* isend = 1 * 人 = {isend = 0 * 民 = {isend = 1} * } * 男 = { * isend = 0 * 人 = { * isend = 1 * } * } * } * } * 五 = { * isend = 0 * 星 = { * isend = 0 * 红 = { * isend = 0 * 旗 = { * isend = 1 * } * } * } * } * @author chenming * @date 2014年4月20日 下午3:04:20 * @param keywordset 敏感词库 * @version 1.0 */ @suppresswarnings({ rawtypes, unchecked }) private void addsensitivewordtohashmap(set keywordset) { sensitivewordmap = new hashmap(keywordset.size()); //初始化敏感词容器,减少扩容操作 string key = null; map nowmap = null; map newwormap = null; //迭代keywordset iterator iterator = keywordset.iterator(); while(iterator.hasnext()){ key = iterator.next(); //关键字 nowmap = sensitivewordmap; for(int i = 0 ; i < key.length() ; i++){ char keychar = key.charat(i); //转换成char型 object wordmap = nowmap.get(keychar); //获取 if(wordmap != null){ //如果存在该key,直接赋值 nowmap = (map) wordmap; } else{ //不存在则,则构建一个map,同时将isend设置为0,因为他不是最后一个 newwormap = new hashmap(); newwormap.put(isend, 0); //不是最后一个 nowmap.put(keychar, newwormap); nowmap = newwormap; } if(i == key.length() - 1){ nowmap.put(isend, 1); //最后一个 } } } } public static void main(string[] args) { set set = new hashset(); set.add(中国); set.add(中国人民); set.add(中国人); new sensitivewordinit().addsensitivewordtohashmap(set); } /** * 读取敏感词库中的内容,将内容添加到set集合中 * @author chenming * @date * @return * @version 1.0 * @throws exception */ @suppresswarnings(resource) private set readsensitivewordfile() throws exception{ set set = null; file file = new file(d:\\sensitiveword.txt); //读取文件 inputstreamreader read = new inputstreamreader(new fileinputstream(file),encoding); try { if(file.isfile() && file.exists()){ //文件流是否存在 set = new hashset(); bufferedreader bufferedreader = new bufferedreader(read); string txt = null; while((txt = bufferedreader.readline()) != null){ //读取文件,将文件内容放入到set中 set.add(txt); } } else{ //不存在抛出异常信息 throw new exception(敏感词库文件不存在); } } catch (exception e) { throw e; }finally{ read.close(); //关闭文件流 } return set; } }
import java.util.hashset; import java.util.iterator; import java.util.map; import java.util.set; /** * @description: 敏感词过滤 * @project:test * @author : chenming * @date : * @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 txt,int matchtype){ boolean flag = false; for(int i = 0 ; i 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 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 txt,int matchtype,string 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 replacechar,int 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 */ @suppresswarnings({ rawtypes}) public int checksensitiveword(string txt,int beginindex,int 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.get(word); //获取指定key if(nowmap != null){ //存在,则判断是否为最后一个 matchflag++; //找到相应key,匹配标识+1 if(1.equals(nowmap.get(isend))){ //如果为最后一个匹配规则,结束循环,返回匹配标识数 flag = true; //结束标志位为true if(sensitivewordfilter.minmatchtype == matchtype){ //最小规则,直接返回,最大规则还需继续查找 break; } } } else{ //不存在,直接返回 break; } } if(matchflag < 2 || !flag){ //长度必须大于等于1,为词 matchflag = 0; } return matchflag; } public static void main(string[] args) { sensitivewordfilter filter = new sensitivewordfilter(); system.out.println(敏感词的数量: + filter.sensitivewordmap.size()); string string = 太多的伤感情怀也许只局限于饲养基地 荧幕中的情节,主人公尝试着去用某种方式渐渐的很潇洒地释自杀指南怀那些自己经历的伤感。 + 然后法轮功 我们的扮演的角色就是跟随着主人公的喜红客联盟 怒哀乐而过于牵强的把自己的情感也附加于银幕情节中,然后感动就流泪, + 难过就躺在某一个人的怀里尽情的阐述心扉或者手机卡复制器一个人一杯红酒一部电影在夜三级片 深人静的晚上,关上电话静静的发呆着。; system.out.println(待检测语句字数: + string.length()); long begintime = system.currenttimemillis(); set set = filter.getsensitiveword(string, 1); long endtime = system.currenttimemillis(); system.out.println(语句中包含敏感词的个数为: + set.size() + 。包含: + set); system.out.println(总共消耗时间为: + (endtime - begintime)); } }
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product