敏感词过滤功能实现
1.GitHub上下载敏感词文件
2.将敏感词文件放在resources目录下
在业务中可以将文本中的敏感词写入数据库便于管理。
3.提供实现类demo
代码编写思路如下:1.将敏感词加载到list中,2.添加到StringSearch中,3.校验,判断文本是否属于敏感词汇。文章来源:https://www.toymoban.com/news/detail-656852.html
import toolgood.words.StringSearch;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class TestWords {
public static void main(String[] args) {
String filePath = "src/main/resources/sensi_words.txt";
try(BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
List<String> list = new ArrayList<>();
StringSearch search = new StringSearch();
while ((line = reader.readLine()) != null){
if (!list.contains(line)){
list.add(line);
}
}
search.SetKeywords(list);
String[] words = new String[]{"草","草他妈","你是狗","你是做过鸡嘛","干你妈","煞笔","你是shabi","我有点呆"};
for (int i = 0; i < words.length; i++) {
boolean res = search.ContainsAny(words[i]);
System.out.println(res == true ? words[i]+":敏感词": words[i]+":非敏感词");
}
System.out.println("---------------------------");
for (int i = 0; i < words.length; i++) {//脱敏处理
boolean res = search.ContainsAny(words[i]);
System.out.println(res == true ? search.Replace(words[i],'*') : words[i]);
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
4.输出结果展示
注意:输出结果中,敏感词已经进行*号处理文章来源地址https://www.toymoban.com/news/detail-656852.html
到了这里,关于Java实现敏感词过滤功能的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!