屏蔽敏感词汇&KMP
来源:互联网 发布:布加迪chiron尺寸数据 编辑:程序博客网 时间:2024/05/09 00:38
在互联网上,由于一些政策法规,需要对一些帖子或者聊天记录进行关键字屏蔽,本文使用KMP模式匹配算法,将目标串S中的敏感词汇全部替换成*。
实际应用中需要一个屏蔽词库,需将每个屏蔽词条作为模式串T,分别过滤一次S串。
考虑到屏蔽词库中有相同前缀的屏蔽词,比如:“朱令”和“朱令案”,所以可以预先将屏蔽词库中的屏蔽词按照长度做由大到小的排序。先过滤长词,再过滤短词即可。
int getNext(char* s, int next[]) { next[0] = -1; int i = -1; int j = 0; while (j < strlen(s)) { if (i == -1 || s[i] == s[j]) { i++; j++; next[j] = i; } else { i = next[i]; } } return 0;}int kmp(char* str, char* t, int next[]) { int j = 0; int i = 0; while (str[i] != '\0' && t[j] != '\0' && strlen(str) - i >= strlen(t) - j) { if (str[i] == t[j]) { i++; j++; } else { j = next[j]; if (j == -1) { i++; j++; } } if (t[j] == '\0') { i = i - j; return i; } } return -1;}int fillStar(char* p, int len) { int i = 0; while (i < len) { p[i] = '*'; i++; } return 0;}int filter(char* s, char* t) { int len = strlen(t); int next[LEN]; getNext(t, next); char* p = s; while (p != '\0') { int loc = kmp(p, t, next); if (loc >= 0) { fillStar(p + loc, len); p = (p + loc + len); } else { return 0; } }}
转载自http://blog.csdn.net/li4951/article/details/8962056
- 屏蔽敏感词汇&KMP
- 屏蔽敏感词汇&KMP
- 通过过滤器进行敏感词汇的屏蔽
- 过滤器-过滤敏感词汇
- PHP 敏感词汇过滤
- 过滤敏感词汇
- 过滤敏感词汇
- 过滤敏感词汇
- 过滤敏感词汇
- 过滤敏感词汇
- 过滤敏感词汇
- 过滤敏感词汇
- 敏感词屏蔽
- 敏感信息屏蔽工具
- Unity敏感词屏蔽
- C++ 敏感词屏蔽
- Python屏蔽敏感字
- Msyql过滤敏感词汇语句
- ASP.NET 实现Base64加密和解密
- uva 11624 - Fire!(bfs,3级)
- ORA-00054: resource busy and acquire with NOWAIT specified or timeout expired
- source insight clf
- 快三十了,在计算机方面还是刚入门。
- 屏蔽敏感词汇&KMP
- js 写一个字符串转成驼峰的方法
- iOS中使用RegexKitLite来试用正则表达式
- 修改UISearchBar背景
- C++ UTF8编码转换 CChineseCode
- Delphi 应用 openmotif 库实例
- 开机后自动重启的功能
- Oracle基本概念
- org.hibernate.MappingException: Could not determine type for