JavaShuo
栏目
标签
DFA算法过滤敏感词,替换为*
时间 2019-11-19
标签
dfa
算法
过滤
敏感
替换
繁體版
原文
原文链接
import
java.io.InputStream;
import
java.io.UnsupportedEncodingException;
import
java.nio.ByteBuffer;
import
java.util.ArrayList;
import
java.util.Enumeration;
import
java.util.List;
import
java.util.Properties;
public
class
test {
/**
* 根节点
*/
private
TreeNode rootNode =
new
TreeNode();
/**
* 关键词缓存
*/
private
ByteBuffer keywordBuffer = ByteBuffer.allocate(
1024
);
/**
* 关键词编码
*/
private
String charset =
"utf-8"
;
/**
* 建立DFA
* @param keywordList
* @throws UnsupportedEncodingException
*/
public
void
createKeywordTree(List<String> keywordList)
throws
UnsupportedEncodingException{
for
(String keyword : keywordList) {
if
(keyword ==
null
)
continue
;
keyword = keyword.trim();
byte
[] bytes = keyword.getBytes(charset);
TreeNode tempNode = rootNode;
for
(
int
i =
0
; i < bytes.length; i++) {
int
index = bytes[i] &
0xff
;
TreeNode node = tempNode.getSubNode(index);
if
(node ==
null
){
node =
new
TreeNode();
tempNode.setSubNode(index, node);
}
tempNode = node;
if
(i == bytes.length -
1
){
tempNode.setKeywordEnd(
true
);
}
}
}
}
public
String searchKeyword(String text)
throws
UnsupportedEncodingException{
return
searchKeyword(text.getBytes(charset));
}
public
String searchKeyword(
byte
[] bytes){
StringBuilder words =
new
StringBuilder();
if
(bytes ==
null
|| bytes.length ==
0
){
return
words.toString();
}
TreeNode tempNode = rootNode;
int
rollback =
0
;
int
position =
0
;
while
(position < bytes.length) {
int
index = bytes[position] &
0xFF
;
keywordBuffer.put(bytes[position]);
tempNode = tempNode.getSubNode(index);
if
(tempNode ==
null
){
position = position - rollback;
rollback =
0
;
tempNode = rootNode;
keywordBuffer.clear();
}
else
if
(tempNode.isKeywordEnd()){
keywordBuffer.flip();
for
(
int
i =
0
; i <= rollback; i++) {
bytes[position-i] =
42
;
}
keywordBuffer.limit(keywordBuffer.capacity());
rollback =
1
;
}
else
{
rollback++;
}
position++;
}
String result =
null
;
try
{
result =
new
String(bytes,
"utf-8"
);
}
catch
(Exception e) {
e.printStackTrace();
}
return
result;
}
public
void
setCharset(String charset) {
this
.charset = charset;
}
}
[java]
view plain
copy
import
java.util.ArrayList;
import
java.util.List;
public
class
TreeNode {
private
static
final
int
NODE_LEN =
256
;
/**
* true 关键词的终结 ; false 继续
*/
private
boolean
end =
false
;
private
List<TreeNode> subNodes =
new
ArrayList<TreeNode>(NODE_LEN);
public
TreeNode(){
for
(
int
i =
0
; i < NODE_LEN; i++) {
subNodes.add(i,
null
);
}
}
/**
* 向指定位置添加节点树
* @param index
* @param node
*/
public
void
setSubNode(
int
index, TreeNode node){
subNodes.set(index, node);
}
public
TreeNode getSubNode(
int
index){
return
subNodes.get(index);
}
public
boolean
isKeywordEnd() {
return
end;
}
public
void
setKeywordEnd(
boolean
end) {
this
.end = end;
}
}
相关文章
1.
DFA算法实现敏感词过滤
2.
敏感词过滤-DFA算法
3.
算法-DFA算法-敏感词过滤算法(OC、Swift、Python)
4.
Java过滤敏感词算法--DFA算法
5.
敏感词过滤算法
6.
Java实现敏感词过滤 - DFA算法
7.
基于DFA算法实现的敏感词过滤
8.
java实现敏感词过滤(DFA算法)
9.
Java利用DFA算法实现敏感词过滤
10.
敏感词替换
更多相关文章...
•
XML DOM 替换节点
-
XML DOM 教程
•
XSD 元素替换(Element Substitution)
-
XML Schema 教程
•
算法总结-广度优先算法
•
算法总结-深度优先算法
相关标签/搜索
dfa
替换
过滤
敏感
过敏
换算
词法
可替换
全局替换
PHP 7 新特性
PHP教程
NoSQL教程
算法
计算
0
分享到微博
分享到微信
分享到QQ
每日一句
每一个你不满意的现在,都有一个你没有努力的曾经。
最新文章
1.
安装cuda+cuDNN
2.
GitHub的使用说明
3.
phpDocumentor使用教程【安装PHPDocumentor】
4.
yarn run build报错Component is not found in path “npm/taro-ui/dist/weapp/components/rate/index“
5.
精讲Haproxy搭建Web集群
6.
安全测试基础之MySQL
7.
C/C++编程笔记:C语言中的复杂声明分析,用实例带你完全读懂
8.
Python3教程(1)----搭建Python环境
9.
李宏毅机器学习课程笔记2:Classification、Logistic Regression、Brief Introduction of Deep Learning
10.
阿里云ECS配置速记
本站公众号
欢迎关注本站公众号,获取更多信息
相关文章
1.
DFA算法实现敏感词过滤
2.
敏感词过滤-DFA算法
3.
算法-DFA算法-敏感词过滤算法(OC、Swift、Python)
4.
Java过滤敏感词算法--DFA算法
5.
敏感词过滤算法
6.
Java实现敏感词过滤 - DFA算法
7.
基于DFA算法实现的敏感词过滤
8.
java实现敏感词过滤(DFA算法)
9.
Java利用DFA算法实现敏感词过滤
10.
敏感词替换
>>更多相关文章<<