java过滤灵敏字体的方法ITeye - 乐橙lc8

java过滤灵敏字体的方法ITeye

2019-01-09 16:36:15 | 作者: 宛秋 | 标签: 灵敏,匹配,规矩 | 浏览: 1515

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 :zys
* @Date : 2018年 下午16:11:34
* @version
*/
public class SensitiveWordInit {
private String ENCODING = "UTF-8";  //字符编码
public HashMap sensitiveWordMap;

public SensitiveWordInit(){
super();
}

/**
* @version 1.0
*/
public Map initKeyWord(){
try {
//读取灵敏词库
Set String keyWordSet = readSensitiveWordFile();
//将灵敏词库参加到HashMap中
addSensitiveWordToHashMap(keyWordSet);
//spring获取application,然后application.setAttribute("sensitiveWordMap",sensitiveWordMap);
} catch (Exception e) {
e.printStackTrace();
}
return sensitiveWordMap;
}

/**
* 读取灵敏词库,将灵敏词放入HashSet中,构建一个DFA算法模型: br

* @param keyWordSet  灵敏词库
* @version 1.0
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
private void addSensitiveWordToHashMap(Set String keyWordSet) {
sensitiveWordMap = new HashMap(keyWordSet.size());  //初始化灵敏词容器,削减扩容操作
String key = null; 
Map nowMap = null;
Map String, String newWorMap = null;
//迭代keyWordSet
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);  //转换成char型
Object wordMap = nowMap.get(keyChar);  //获取

if(wordMap != null){  //假如存在该key,直接赋值
nowMap = (Map) wordMap;
}
else{  //不存在则,则构建一个map,一起将isEnd设置为0,由于他不是最终一个
newWorMap = new HashMap String,String
newWorMap.put("isEnd", "0");  //不是最终一个
nowMap.put(keyChar, newWorMap);
nowMap = newWorMap;
}

if(i == key.length() - 1){
nowMap.put("isEnd", "1");  //最终一个
}
}
}
}

/**
* 读取灵敏词库中的内容,将内容添加到set调集中

* @date 2014年 下午2:31:18
* @return
* @version 1.0
* @throws Exception
*/
@SuppressWarnings("resource")
private Set String readSensitiveWordFile() throws Exception{
Set String set = null;

File file = new File("D:\\01.txt");  //读取文件
InputStreamReader read = new InputStreamReader(new FileInputStream(file),ENCODING);
try {
if(file.isFile() file.exists()){  //文件流是否存在
set = new HashSet String
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: 灵敏词过滤
* @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();
}

/**
* 判别文字是否包括灵敏字符
* @param txt  文字
* @param matchType  匹配规矩 nbsp;1:最小匹配规矩,2:最大匹配规矩
* @return 若包括回来true,不然回来false
* @version 1.0
*/
public boolean isContaintSensitiveWord(String txt,int 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 匹配规矩 nbsp;1:最小匹配规矩,2:最大匹配规矩
* @return
* @version 1.0
*/
public 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){  //存在,参加list中
sensitiveWordList.add(txt.substring(i, i+length));
i = i + length - 1;  //减1的原因,是由于for会自增
}
}

return sensitiveWordList;
}

/**
* 替换灵敏字字符

* @param replaceChar 替换字符,默许*
* @version 1.0
*/
public 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
* @version 1.0
*/
private String getReplaceChars(String replaceChar,int length){
String resultReplace = replaceChar;
for(int i = 1 ; i length ; i++){
resultReplace += replaceChar;
}

return resultReplace;
}

/**
* 查看文字中是否包括灵敏字符,查看规矩如下: br
* @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 String set = filter.getSensitiveWord(string, 1);
System.out.println(filter.replaceSensitiveWord(string, 1, "*"));
//long endTime = System.currentTimeMillis();
System.out.println("句子中包括灵敏词的个数为:" + set.size() + "。包括:" + set);
//System.out.println("一共耗费时刻为:" + (endTime - beginTime));
}
}
=====================
新建01.txt
扯淡
几把
废物

版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表乐橙lc8立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章

阅读排行

  • 1
  • 2

    SSL1ITeye

    证书,后边,途径
  • 3

    discuz 单点登录 ucenterITeye

    项目,下载,文件
  • 4
  • 5

    spring+mybatis装备ITeye

    装备,一下,需求
  • 6

    单例形式-线程安全ITeye

    线程,安全,形式
  • 7

    java数据存储ITeye

    数据,存储,分配
  • 8
  • 9

    httpclient 恳求 post、getITeye

    恳求,使用,进行
  • 10

    Redis的耐久化机制ITeye

    耐久,方法,内存