一个简单高效的禁词过滤类

使用方法:

public static void main(String[] args) {
SimpleTreeFilter filter = new SimpleTreeFilter();
filter.addKeyword("禁词1");
filter.addKeyword("禁词2");
filter.addKeyword("其它禁词");

System.out.println(filter.contains("我是合法的"));
System.out.println(filter.contains("我包含禁词1"));
System.out.println(filter.contains("我包含禁词2"));
System.out.println(filter.contains("我包含其它禁词1"));
System.out.println(filter.contains("来个别的吧"));
System.out.println(filter.contains("再见"));
}

输出:
null
禁词1
禁词2
其它禁词
null
null



import java.util.HashMap;

public class SimpleTreeFilter{

public class TreeNode{
public char c;
public HashMap next;
public boolean isEnd=false;
}
HashMap head = new HashMap();

public void addKeyword(String word) {
word=word.toLowerCase();
int len = word.length();
if(len==0){
return;
}
char firstChar = word.charAt(0);
TreeNode node;
if(head.containsKey(firstChar)){
node = head.get(firstChar);
}else{
node = new TreeNode();
node.c=firstChar;
head.put(firstChar, node);
}
for(int i=1;i char c=word.charAt(i);
if(node.next==null){
node.next = new HashMap();
}
if(node.next.containsKey(c)){
node = node.next.get(c);
}else{
TreeNode tNode = new TreeNode();
tNode.c=c;
node.next.put(c, tNode);
node = tNode;
}
}
node.isEnd=true;
}

public String contains(String line) {
int len = line.length();
line=line.toLowerCase();
for(int i=0;i char c=line.charAt(i);
if(head.containsKey(c)){
TreeNode node = head.get(c);
if(node.isEnd==true){
return (c+"").toLowerCase();
}
int j=i+1;
while(j char cTemp = line.charAt(j);
if(node.next.containsKey(cTemp)){
node = node.next.get(cTemp);
if(node.isEnd==true){
return line.substring(i,j+1).toLowerCase();
}
}else{
break;
}
j++;
}
}
}
return null;
}
}

你可能感兴趣的:(java)