关键字过滤实现
程序员文章站
2022-03-03 10:21:35
...
高效过滤,有兴趣的可以看看《Java系统性能优化》性能优化这本书,以下是书中部分代码,做个笔记
package com.golconda.tree;
import java.util.HashMap;
import java.util.Map;
public class Node {
private Map<Character, Node> nextNodes = new HashMap<>();
public void addNextNode(Character key, Node node) {
nextNodes.put(key, node);
}
public Node getNextNode(Character key) {
return nextNodes.get(key);
}
public boolean isLastCharacter() {
return nextNodes.isEmpty();
}
public Map<Character, Node> getNextNodes() {
return nextNodes;
}
public void setNextNodes(Map<Character, Node> nextNodes) {
this.nextNodes = nextNodes;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((nextNodes == null) ? 0 : nextNodes.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Node other = (Node) obj;
if (nextNodes == null) {
if (other.nextNodes != null)
return false;
} else if (!nextNodes.equals(other.nextNodes))
return false;
return true;
}
}
package com.golconda.tree;
import com.alibaba.fastjson.JSON;
public class KeywordSearch {
static Node root = new Node();
String sensitiveWords = "***";
public void addWord(String word) {
Node tempNode = root;
for (int i = 0; i < word.length(); i++) {
Character c = word.charAt(i);
Node node = tempNode.getNextNode(c);
if (node == null) {
node = new Node();
tempNode.addNextNode(c, node);
}
// 移动到下一个字
tempNode = node;
}
}
public String filter(String text) {
StringBuilder result = new StringBuilder(text.length());
Node tempNode = root;
int begin = 0;
int position = 0;
while (position < text.length()) {
Character c = text.charAt(position);
tempNode = tempNode.getNextNode(c);
if (tempNode == null) {
如果匹配失败,合法
result.append(text.charAt(begin));
begin = begin + 1;
position = begin;
//从新匹配
tempNode = root;
continue;
} else if (tempNode.isLastCharacter()) {
//匹配结束,替换敏感词
result.append(sensitiveWords);
position++;
begin = position;
tempNode = root;
} else {
position++;
}
}
//添加剩下的内容
result.append(text.substring(begin));
return result.toString();
}
public static void main(String[] args) {
KeywordSearch ts = new KeywordSearch();
ts.addWord("猪狗");
ts.addWord("小猫");
ts.addWord("天气预报");
System.out.println(JSON.toJSONString(root.getNextNodes()));
String ret = ts.filter("你好,小猫");
System.out.println(ret);
}
}