关键字过滤实现

高效过滤,有兴趣的可以看看《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);

	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值