基于JAVA语言开发的一个高效的敏感词过滤工具
经实测,敏感词数量为5000个,待检测文本长度为200时,此工具类可毫秒级高效检索敏感词
各位同学需要自取,转载请标明出处
package com.wgh.common.utils;
import com.google.common.collect.Lists;
import java.util.*;
/**
* @desc 一个用于敏感词检测的高效工具类
* @author 王冠华
* @date 2024-07-17
*/
public class SensitiveWordFilter {
private static class TrieNode {
Map<Character, TrieNode> children; // 使用 Map 来存储子节点
TrieNode fail; // 失败指针
boolean isEndOfWord; // 标记是否是敏感词的结尾
String word; // 存储完整的敏感词
public TrieNode() {
this.children = new HashMap<>(); // 使用 HashMap 存储子节点
this.fail = null;
this.isEndOfWord = false;
this.word = null;
}
}
private TrieNode root;
/**
* 构造函数,用于初始化敏感词库。
* @param words 敏感词集合,集合确保敏感词不会重复
*/
public SensitiveWordFilter(Set<String> words) {
root = new TrieNode();
addWords(words);
buildFailurePointers();
}
/**
* 将敏感词集合添加到 Trie 树中。
* @param words 敏感词集合
*/
private void addWords(Set<String> words) {
for (String word : words) {
addWord(word.toLowerCase()); // 转换为小写
}
}
/**
* 将单个敏感词添加到 Trie 树中。
* @param word 敏感词
*/
private void addWord(String word) {
TrieNode current = root;
for (char c : word.toCharArray()) {
current.children.putIfAbsent(c, new TrieNode());
current = current.children.get(c);
}
current.isEndOfWord = true; // 标记敏感词的结尾
current.word = word; // 存储完整的敏感词
}
/**
* 构建失败指针,用于加速匹配过程。
*/
private void buildFailurePointers() {
Queue<TrieNode> queue = new LinkedList<>();
root.fail = null; // 根节点的失败指针应为 null
queue.add(root);
while (!queue.isEmpty()) {
TrieNode current = queue.poll();
for (Map.Entry<Character, TrieNode> entry : current.children.entrySet()) {
char c = entry.getKey();
TrieNode child = entry.getValue();
TrieNode fail = current.fail;
while (fail != null && !fail.children.containsKey(c)) {
fail = fail.fail;
}
if (fail == null) {
child.fail = root;
} else {
child.fail = fail.children.get(c);
}
queue.add(child);
}
}
}
/**
* 检查文本中是否包含敏感词,并返回 true 或 false。
* @param text 要检查的文本
* @return 如果包含敏感词,返回 true;否则返回 false
*/
public boolean containsSensitiveWord(String text) {
TrieNode current = root;
TrieNode node;
for (int i = 0; i < text.length(); i++) {
node = current;
for (int j = i; j < text.length(); j++) {
char c = Character.toLowerCase(text.charAt(j)); // 转换为小写
while (node != null && !node.children.containsKey(c)) {
node = node.fail; // 使用失败指针进行跳转
}
if (node == null) {
break;
} else {
node = node.children.get(c); // 跳转到匹配的子节点
}
// 直接在当前节点判断是否为敏感词结尾
if (node != null && node.isEndOfWord) {
return true;
}
}
}
return false; // 未找到敏感词
}
/**
* 检查文本中是否包含敏感词,并返回命中的第一个敏感词及其位置。
* @param text 要检查的文本
* @return 包含敏感词及其位置的 Map.Entry 对象
*/
public Map.Entry<String, Integer> findSensitiveWord(String text) {
TrieNode current = root;
TrieNode node;
for (int i = 0; i < text.length(); i++) {
node = current;
for (int j = i; j < text.length(); j++) {
char c = Character.toLowerCase(text.charAt(j)); // 转换为小写
while (node != null && !node.children.containsKey(c)) {
node = node.fail; // 使用失败指针进行跳转
}
if (node == null) {
break;
} else {
node = node.children.get(c); // 跳转到匹配的子节点
}
// 直接在当前节点判断是否为敏感词结尾
if (node != null && node.isEndOfWord) {
return new AbstractMap.SimpleEntry<>(node.word, i);
}
}
}
return null; // 未找到敏感词
}
/**
* 检查文本中是否包含敏感词,并返回所有命中的敏感词及其位置。
* @param text 要检查的文本
* @return 包含敏感词及其位置的列表
*/
public List<Map.Entry<String, Integer>> findAllSensitiveWords(String text) {
List<Map.Entry<String, Integer>> result = new ArrayList<>();
TrieNode current = root;
TrieNode node;
for (int i = 0; i < text.length(); i++) {
node = current;
for (int j = i; j < text.length(); j++) {
char c = Character.toLowerCase(text.charAt(j)); // 转换为小写
while (node != null && !node.children.containsKey(c)) {
node = node.fail; // 使用失败指针进行跳转
}
if (node == null) {
break;
} else {
node = node.children.get(c); // 跳转到匹配的子节点
}
// 直接在当前节点判断是否为敏感词结尾
if (node != null && node.isEndOfWord) {
result.add(new AbstractMap.SimpleEntry<>(node.word, i));
// Continue searching in this segment to find all overlapping sensitive words
}
}
}
return result; // 返回所有找到的敏感词
}
public static void main(String[] args) {
// 初始化含有5000个敏感词的HashSet
ArrayList<String> list = Lists.newArrayList("敏感词1","敏感词2","敏感词3"));
Set<String> set = new HashSet<>(list);
// 创建 SensitiveWordFilter 对象
SensitiveWordFilter filter = new SensitiveWordFilter(set);
// 要检查的文本
String text1 = "位于固原市原州区开城镇的中庄水库,敏感词1是宁夏中南部城乡敏感词2饮水安全工程的主敏感词3调节水库";
String text2 = "位于固原市原州区开城镇的中庄水库,是宁夏中南部城乡饮水安全工程的主调节水库";
// 使用过滤器进行检查
boolean containsSensitiveWord1 = filter.containsSensitiveWord(text1);
boolean containsSensitiveWord2 = filter.containsSensitiveWord(text2);
System.out.println("检测结果1: " + containsSensitiveWord1); // true
System.out.println("检测结果2: " + containsSensitiveWord2); // false
Map.Entry<String, Integer> sensitiveWord1 = filter.findSensitiveWord(text1);
Map.Entry<String, Integer> sensitiveWord2 = filter.findSensitiveWord(text2);
System.out.println("查找结果1: " + sensitiveWord1); // true
System.out.println("查找结果2: " + sensitiveWord2); // false
List<Map.Entry<String, Integer>> allSensitiveWords = filter.findAllSensitiveWords(text1);
System.out.println("查找所有结果: " + allSensitiveWords);
}
}