本文章摘编、转载需要注明来源 http://blog.csdn.net/shadowsick/article/details/8891939
在应用项目中很多时候都需要用到过滤敏感词的功能,自己写个遍历明显在小数据的时候还能凑合下,但是对于大数据的时候就有点力不从心了,这里推荐使用ac多模匹配算法
先来写个应用类
/**
* AC多模匹配敏感字符工具类实现类
*
* @author shadow
* @email [email protected]
* @create 2012.04.28
*/
public class AcUtilImpl implements AcUtil {
public String contrast(String filters, String word, String regex) {
if (null == filters || "".equals(filters) || null == word
|| "".equals(word))
return "";
AhoCorasick ac = new AhoCorasick();
String[] strings = StringUtils.split(filters, regex);
for (String string : strings)
ac.add(string.getBytes(), string);
ac.prepare();
return matching(ac, word);
}
public String contrast(String[] filters, String word) {
if (null == filters || filters.length