1. 简易Demo
通过简易的Demo机制了解基本原理
import java.util.HashSet;
import java.util.Set;
public class test {
private Set<String> sensitiveWords;
public test(Set<String> sensitiveWords) {
this.sensitiveWords = sensitiveWords;
}
public String filter(String text) {
for (String word : sensitiveWords) {
text = text.replaceAll(word, "***"); // 替换敏感词为 ***
}
return text;
}
public static void main(String[] args) {
// 构建敏感词库
Set<String> sensitiveWords = new HashSet<>();
sensitiveWords.add("敏感词1");
sensitiveWords.add("敏感词2");
sensitiveWords.add("敏感词3");
// 创建敏感词过滤器
test filter = new test(sensitiveWords);
// 测试文本
String text = "这是一个包含敏感词1和敏感词2的文本。";
// 进行过滤
String filteredText = filter.filter(text);
// 输出过滤后的文本
System.out.println("过滤后的文本:");
System.out.println(filteredText);
}
}
截图如下:
2. 进阶Demo
主要丰富敏感词的一些方法功能
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class test {
private Set<String> sensitiveWords;
public test(Set<String> sensitiveWords) {
this.sensitiveWords = sensitiveWords;
}
public String filter(String text) {
for (String word : sensitiveWords) {
text = text.replaceAll(word, "***"); // 替换敏感词为 ***
}
return text;
}
public boolean contains(String text, String word) {
return text.contains(word);
}
public boolean findAll(String text) {
for (String word : sensitiveWords) {
if (text.contains(word)) {
return true;
}
}
return false;
}
public String findFirst(String text) {
for (String word : sensitiveWords) {
if (text.contains(word)) {
return word;
}
}
return null;
}
public String replace(String text, String replacement) {
for (String word : sensitiveWords) {
text = text.replace(word, replacement);
}
return text;
}
public String replaceChar(String text, char replacement) {
for (String word : sensitiveWords) {
text = text.replace(word, String.valueOf(replacement));
}
return text;
}
public boolean ignoreCase(String text, String word) {
for (String sensitiveWord : sensitiveWords) {
if (sensitiveWord.equalsIgnoreCase(word)) {
return true;
}
}
return false;
}
public boolean ignoreWidth(String text, String word) {
for (String sensitiveWord : sensitiveWords) {
if (text.contains(sensitiveWord) || text.contains(word)) {
return true;
}
}
return false;
}
public void config(Set<String> newSensitiveWords) {
this.sensitiveWords = newSensitiveWords;
}
public static void main(String[] args) {
// 构建敏感词库
Set<String> sensitiveWords = new HashSet<>();
sensitiveWords.add("敏感词1");
sensitiveWords.add("敏感词2");
sensitiveWords.add("敏感词3");
// 创建敏感词过滤器
test filter = new test(sensitiveWords);
// 测试文本
String text = "这是一个包含敏感词1和敏感词2的文本。";
// 测试 contains 方法
testContains(filter, text);
// 测试 findAll 方法
testFindAll(filter, text);
// 测试 findFirst 方法
testFindFirst(filter, text);
// 测试 replace 方法
testReplace(filter, text);
// 测试 replaceChar 方法
testReplaceChar(filter, text, '*'); // 传入替换字符
// 测试 ignoreCase 方法
testIgnoreCase(filter, text);
// 测试 ignoreWidth 方法
testIgnoreWidth(filter, text);
// 测试 config 方法
testConfig(filter, new HashSet<>(Arrays.asList("新敏感词1", "新敏感词2")));
// 输出过滤后的文本
System.out.println("过滤后的文本:");
System.out.println(filter.filter(text));
}
private static void testContains(test filter, String text) {
System.out.println("测试 contains 方法:");
System.out.println("文本是否包含敏感词1? " + filter.contains(text, "敏感词1"));
System.out.println("文本是否包含敏感词4? " + filter.contains(text, "敏感词4"));
}
// 添加其他测试方法...
private static void testFindAll(test filter, String text) {
System.out.println("测试 findAll 方法:");
System.out.println("文本是否包含任何敏感词? " + filter.findAll(text));
}
private static void testFindFirst(test filter, String text) {
System.out.println("测试 findFirst 方法:");
System.out.println("文本中第一个敏感词是: " + filter.findFirst(text));
}
private static void testReplace(test filter, String text) {
System.out.println("测试 replace 方法:");
System.out.println("过滤后的文本: " + filter.replace(text, "***"));
}
private static void testReplaceChar(test filter, String text, char replacement) {
System.out.println("测试 replaceChar 方法:");
System.out.println("过滤后的文本: " + filter.replaceChar(text, replacement));
}
private static void testIgnoreCase(test filter, String text) {
System.out.println("测试 ignoreCase 方法:");
System.out.println("文本是否包含忽略大小写的敏感词? " + filter.ignoreCase(text, "敏感词1"));
}
private static void testIgnoreWidth(test filter, String text) {
System.out.println("测试 ignoreWidth 方法:");
System.out.println("文本是否包含忽略宽度的敏感词? " + filter.ignoreWidth(text, "敏感词1"));
}
private static void testConfig(test filter, Set<String> newSensitiveWords) {
System.out.println("测试 config 方法:");
filter.config(newSensitiveWords);
System.out.println("已更新敏感词库。");
}
}
截图如下: