IKAnalyzer 是一个开源的,基于java语言开发的轻量级的中文分词工具包。
本用例借助 IKAnalyzer 进行分词,通过遍历分词集合进行敏感词过滤。
使用前需对敏感词库进行初始化: SensitiveWordUtil.init(sensitiveWordSet);
1、pom.xml 引入maven依赖
<dependency>
<groupId>com.janeluo</groupId>
<artifactId>ikanalyzer</artifactId>
<version>2012_u6</version>
</dependency>
2、工具类
package cn.swfilter.util;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;
import java.io.IOException;
import java.io.StringReader;
import java.util.*;
/**
* 敏感词处理工具 - IKAnalyzer中文分词工具 - 借助分词进行敏感词过滤
*
* @author sam
* @since 2017/9/4
*/
public class SensitiveWordUtil2 {
/**
* 敏感词集合
*/
public static HashMap sensitiveWordMap;
/**
* 初始化敏感词库
*
* @param sensitiveWordSet 敏感词库
*/
public static synchronized void init(Set<String> sensitiveWordSet) {
sensitiveWordMap = new HashMap(sensitiveWordSet.size());
for (String sensitiveWord : sensitiveWordSet) {
sensitiveWordMap.put(sensitiveWord, sensitiveWord);
}
}
/**
* 判断文字是否包含敏感字符
*
* @param txt 文字
* @return 若包含返回true,否则返回false
*/
public static boolean contains(String txt) throws Exception {
boolean flag = false;
List<String> wordList = segment(txt);
for (String word : wordList) {
if (sensitiveWordMap.get(word) != null) {
return true;
}
}
return flag;
}
/**
* 获取文字中的敏感词
*
* @param txt 文字
* @return
*/
public static Set<String> getSensitiveWord(String txt) throws IOException {
Set<String> sensitiveWordList = new HashSet<>();
List<String> wordList = segment(txt);
for (String word : wordList) {
if (sensitiveWordMap.get(word) != null) {
sensitiveWordList.add(word);
}
}
return sensitiveWordList;
}
/**
* 替换敏感字字符
*
* @param txt 文本
* @param replaceChar 替换的字符,匹配的敏感词以字符逐个替换,如 语句:我爱中国人 敏感词:中国人,替换字符:*, 替换结果:我爱***
* @return
*/
public static String replaceSensitiveWord(String txt, char replaceChar) throws IOException {
String resultTxt = txt;
Set<String> sensitiveWordList = getSensitiveWord(txt);
String replaceString;
for (String sensitiveWord : sensitiveWordList) {
replaceString = getReplaceChars(replaceChar, sensitiveWord.length());
resultTxt = resultTxt.replaceAll(sensitiveWord, replaceString);
}
return resultTxt;
}
/**
* 替换敏感字字符
*
* @param txt 文本
* @param replaceStr 替换的字符串,匹配的敏感词以字符逐个替换,如 语句:我爱中国人 敏感词:中国人,替换字符串:[屏蔽],替换结果:我爱[屏蔽]
* @return
*/
public static String replaceSensitiveWord(String txt, String replaceStr) throws IOException {
String resultTxt = txt;
Set<String> sensitiveWordList = getSensitiveWord(txt);
for (String sensitiveWord : sensitiveWordList) {
resultTxt = resultTxt.replaceAll(sensitiveWord, replaceStr);
}
return resultTxt;
}
/**
* 获取替换字符串
*
* @param replaceChar
* @param length
* @return
*/
private static String getReplaceChars(char replaceChar, int length) {
String resultReplace = String.valueOf(replaceChar);
for (int i = 1; i < length; i++) {
resultReplace += replaceChar;
}
return resultReplace;
}
/**
* 对语句进行分词
*
* @param text 语句
* @return 分词后的集合
* @throws IOException
*/
private static List segment(String text) throws IOException {
List<String> list = new ArrayList<>();
StringReader re = new StringReader(text);
IKSegmenter ik = new IKSegmenter(re, true);
Lexeme lex;
while ((lex = ik.next()) != null) {
list.add(lex.getLexemeText());
}
return list;
}
public static void main(String[] args) throws IOException {
Set<String> sensitiveWordSet = new HashSet<>();
sensitiveWordSet.add("太多");
sensitiveWordSet.add("爱恋");
sensitiveWordSet.add("静静");
sensitiveWordSet.add("哈哈");
sensitiveWordSet.add("啦啦");
sensitiveWordSet.add("感动");
sensitiveWordSet.add("发呆");
SensitiveWordUtil2.init(sensitiveWordSet);
/**
* 需要进行处理的目标字符串
*/
System.out.println("敏感词的数量:" + SensitiveWordUtil2.sensitiveWordMap.size());
String string = "太多的伤感情怀也许只局限于饲养基地 荧幕中的情节。"
+ "然后 我们的扮演的角色就是跟随着主人公的喜红客联盟 怒哀乐而过于牵强的把自己的情感也附加于银幕情节中,然后感动就流泪,"
+ "难过就躺在某一个人的怀里尽情的阐述心扉或者手机卡复制器一个贱人一杯红酒一部电影在夜 深人静的晚上,关上电话静静的发呆着。";
System.out.println("待检测语句字数:" + string.length());
/**
* 是否含有关键字
*/
try {
boolean result = SensitiveWordUtil2.contains(string);
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
/**
* 获取语句中的敏感词
*/
Set<String> set = SensitiveWordUtil2.getSensitiveWord(string);
System.out.println("语句中包含敏感词的个数为:" + set.size() + "。包含:" + set);
/**
* 替换语句中的敏感词
*/
String filterStr = SensitiveWordUtil2.replaceSensitiveWord(string, '*');
System.out.println(filterStr);
String filterStr2 = SensitiveWordUtil2.replaceSensitiveWord(string, "[*敏感词*]");
System.out.println(filterStr2);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
以上,使用 IKAnalyzer 可以很轻松的实现敏感词过滤功能。
缺点:使用 IKAnalyzer 进行分词,有时候分词结果并不是很理想。如:发呆着,分词结果是 [“发”,”呆着”],而我们的敏感词是发呆,这种情况就会造成敏感词过滤不完整。
所有评论(0)