AC多模匹配算法过滤敏感词实例

  1. 本文章摘编、转载需要注明来源 http://blog.csdn.net/shadowsick/article/details/8891939  

在应用项目中很多时候都需要用到过滤敏感词的功能,自己写个遍历明显在小数据的时候还能凑合下,但是对于大数据的时候就有点力不从心了,这里推荐使用ac多模匹配算法


先来写个应用类

[java]  view plain copy
  1. /** 
  2.  * AC多模匹配敏感字符工具类实现类 
  3.  *  
  4.  * @author shadow 
  5.  * @email 124010356@qq.com 
  6.  * @create 2012.04.28 
  7.  */  
  8. public class AcUtilImpl implements AcUtil {  
  9.   
  10.     public String contrast(String filters, String word, String regex) {  
  11.   
  12.         if (null == filters || "".equals(filters) || null == word  
  13.                 || "".equals(word))  
  14.             return "";  
  15.   
  16.         AhoCorasick ac = new AhoCorasick();  
  17.         String[] strings = StringUtils.split(filters, regex);  
  18.         for (String string : strings)  
  19.             ac.add(string.getBytes(), string);  
  20.         ac.prepare();  
  21.         return matching(ac, word);  
  22.     }  
  23.   
  24.     public String contrast(String[] filters, String word) {  
  25.   
  26.         if (null == filters || filters.length <= 0 || null == word  
  27.                 || "".equals(word))  
  28.             return "";  
  29.   
  30.         AhoCorasick ac = new AhoCorasick();  
  31.         for (int i = 0, len = filters.length; i < len; i++) {  
  32.             ac.add(filters[i].getBytes(), filters[i]);  
  33.         }  
  34.         ac.prepare();  
  35.         return matching(ac, word);  
  36.     }  
  37.   
  38.     public String contrast(List<String> filters, String word) {  
  39.   
  40.         if (null == filters || filters.size() <= 0 || null == word  
  41.                 || "".equals(word))  
  42.             return "";  
  43.   
  44.         AhoCorasick ac = new AhoCorasick();  
  45.         for (int i = 0, len = filters.size(); i < len; i++) {  
  46.             ac.add(filters.get(i).getBytes(), filters.get(i));  
  47.         }  
  48.         ac.prepare();  
  49.         return matching(ac, word);  
  50.     }  
  51.   
  52.     private String matching(AhoCorasick ac, String word) {  
  53.         StringBuffer buffer = new StringBuffer();  
  54.         Iterator<?> iterator = ac.search(word.getBytes());  
  55.         while (iterator.hasNext()) {  
  56.             SearchResult result = (SearchResult) iterator.next();  
  57.             buffer.append(result.getOutputs()).append(",");  
  58.         }  
  59.         return buffer.length() > 0 ? buffer.substring(0, buffer.length() - 1)  
  60.                 : "";  
  61.     }  
  62.   
  63.     public static void main(String[] args) {  
  64.         String filters = "or,world,33,dd,test";  
  65.         String word = "hello world, how are you!";  
  66.         String regex = ",";  
  67.         String result = new AcUtilImpl().contrast(filters, word, regex);  
  68.         System.out.println(result);  
  69.     }  
  70. }  

然后运行main函数测试下,获得的结果是

[or],[world]


这个插件的性能,匹配度也灰常不错,AhoCorasick这个类自己下载放到项目里就可以了


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值