今天的项目是与完成python开展,需要使用做关键词检查,筛选分类,使用前c语言做这种事情。有了线索,非常高效,内存小了,检查快。

到达python在,第一个想法是pip基于外观的c语言python特里模块。不幸的是,我们没有找到一个合适的,假设我会用c书写python模块的话。就自己写一个了,可惜我还不具备这个能力。

仅仅能用python写了,性能差一点就差点吧,内存多一点也无所谓了。

用搜索引擎看CSDN上的网友的用python实现的DFA,再參照自己曾经用c语言写过的字典树。有些不大对,就自己写了一个。想象一下假设用C语言是会很高效,并且空间也特别小。

某位网友的:DFA 算法实现敏感词过滤(python 实现)

以下是python代码:

class cNode(object):
def __init__(self):
self.children = None # The encode of word is UTF-8
# The encode of message is UTF-8
class cDfa(object):
def __init__(self,lWords):
self.root=None
self.root=cNode()
for sWord in lWords:
self.addWord(sWord) # The encode of word is UTF-8
def addWord(self,word):
node = self.root
iEnd=len(word)-1
for i in xrange(len(word)):
if node.children == None:
node.children = {}
if i!=iEnd:
node.children[word[i]]=(cNode(),False)
else:
node.children[word[i]]=(cNode(),True) elif word[i] not in node.children:
if i!=iEnd:
node.children[word[i]]=(cNode(),False)
else:
node.children[word[i]]=(cNode(),True)
else: #word[i] in node.children:
if i==iEnd:
Next,bWord=node.children[word[i]]
node.children[word[i]]=(Next,True) node=node.children[word[i]][0] def isContain(self,sMsg):
root=self.root
iLen=len(sMsg)
for i in xrange(iLen):
p = root
j = i
while (j<iLen and p.children!=None and sMsg[j] in p.children):
(p,bWord) = p.children[sMsg[j]]
if bWord:
return True
j = j + 1
return False def filter(self,sMsg):
lNew=[]
root=self.root
iLen=len(sMsg)
i=0
bContinue=False
while i<iLen:
p=root
j=i
while (j<iLen and p.children!=None and sMsg[j] in p.children):
(p,bWord) = p.children[sMsg[j]]
if bWord:
#print sMsg[i:j+1]
lNew.append(u'*'*(j-i+1))#keyword替换
i=j+1
bContinue=True
break
j=j+1
if bContinue:
bContinue=False
continue
lNew.append(sMsg[i])
i=i+1
return ''.join(lNew)

以下是c语言代码trie_tree.h:

#ifndef _TRIE_TREE_H_INCLUDED_
#define _TRIE_TREE_H_INCLUDED_ #define WORD_NUM 256
struct trie_node {
struct trie_node *node[WORD_NUM];
int value;
int exist;
}; struct trie_node *create_trie_node(int value);
void trie_tree_insert_word(struct trie_node *root, unsigned char *word);
/* return 1 表示存在, return 0表示不存在 */
int tire_word_is_exist(struct trie_node *root, unsigned char *word);
void destory_trie_tree(struct trie_node *root);
void update_trie_tree(struct trie_node **root, const char *filename); #endif

trie_tree.c:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <trie_tree.h> struct trie_node *create_trie_node(int value)
{
struct trie_node * node = calloc(1, sizeof(struct trie_node));
node->value = value;
return node;
} int tire_word_is_exist(struct trie_node *root, unsigned char *word)
{
struct trie_node *n = NULL;
unsigned char *p = NULL; if (root == NULL) {
return 0;
} while (*word != 0) {
p = word++;
n = root;
while (*p != 0) {
n = n->node[*p];
if (n == NULL) {
break;
}
else if (n->exist == 1) {
return 1;
}
p++;
}
} return 0;
} void trie_tree_insert_word(struct trie_node *root, unsigned char *word)
{
struct trie_node *n;
while (*word != 0) {
n = root->node[*word];
if (n == NULL) {
n = create_trie_node(*word);
root->node[*word] = n;
}
root = n;
word++;
}
root->exist = 1;
} void destroy_trie_tree(struct trie_node *root)
{
int i;
if (root == NULL) {
return;
}
for (i = 0; i < WORD_NUM; i++) {
destroy_trie_tree(root->node[i]);
}
free(root);
} void update_trie_tree(struct trie_node **root, const char *filename)
{
char word[1024];
FILE *fp;
char *p; if (*root != NULL) {
destroy_trie_tree(*root);
} *root = calloc(sizeof(**root),1); fp = fopen(filename, "r");
if (fp == NULL) {
printf("file can't open %s\n", filename);
return;
} while (fgets(word, sizeof(word), fp)) {
p = word; while (*p != 0) {
if (*p == '\r' || *p == '\n' || *p == ' ') {
*p = 0;
break;
}
p++;
}
trie_tree_insert_word(*root, (unsigned char *)word);
}
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

DFA和trie特里实现敏感词过滤(python和c语言)的更多相关文章

  1. [原创] Trie树 php 实现敏感词过滤

    目录 背景 简介 存储结构 PHP 其他语言 字符串分割 示例代码 php 优化 缓存字典树 常驻服务 参考文章 背景 项目中需要过滤用户发送的聊天文本, 由于敏感词有将近2W条, 如果用 str_r ...

  2. java实现敏感词过滤(DFA算法)

    小Alan在最近的开发中遇到了敏感词过滤,便去网上查阅了很多敏感词过滤的资料,在这里也和大家分享一下自己的理解. 敏感词过滤应该是不用给大家过多的解释吧?讲白了就是你在项目中输入某些字(比如输入xxo ...

  3. Java实现敏感词过滤 - DFA算法

    Java实现DFA算法进行敏感词过滤 封装工具类如下: 使用前需对敏感词库进行初始化: SensitiveWordUtil.init(sensitiveWordSet); package cn.swf ...

  4. 转,敏感词过滤,PHP实现的Trie树

    原文地址:http://blog.11034.org/2012-07/trie_in_php.html 项目需求,要做敏感词过滤,对于敏感词本身就是一个CRUD的模块很简单,比较麻烦的就是对各种输入的 ...

  5. [转载]敏感词过滤,PHP实现的Trie树

    原文地址:http://blog.11034.org/2012-07/trie_in_php.html 项目需求,要做敏感词过滤,对于敏感词本身就是一个CRUD的模块很简单,比较麻烦的就是对各种输入的 ...

  6. 敏感词过滤的算法原理之DFA算法

    参考文档 http://blog.csdn.net/chenssy/article/details/26961957 敏感词.文字过滤是一个网站必不可少的功能,如何设计一个好的.高效的过滤算法是非常有 ...

  7. 基于DFA算法、RegExp对象和vee-validate实现前端敏感词过滤

    面临敏感词过滤的问题,最简单的方案就是对要检测的文本,遍历所有敏感词,逐个检测输入的文本是否包含指定的敏感词. 很明显上面这种实现方法的检测时间会随着敏感词库数量的增加而线性增加.系统会因此面临性能和 ...

  8. DFA敏感词过滤实现

    package test.java.com.odianyun.util.sensi; import java.util.*; /** * 敏感词处理工具 - DFA算法实现 * * @author s ...

  9. 用php实现一个敏感词过滤功能

    周末空余时间撸了一个敏感词过滤功能,下边记录下实现过程. 敏感词,一方面是你懂的,另一方面是我们自己可能也要过滤一些人身攻击或者广告信息等,具体词库可以google下,有很多. 过滤敏感词,使用简单的 ...

随机推荐

  1. XMPP协议简介

    XMPP(息处理现场协议)是基于可扩展标记语言(XML)的协议.它用于即时消息(IM)以及在线现场探測.XMPP协议採用的是client-server架构,全部从一个client发到还有一个clien ...

  2. 如果在线显示php源代码

    原文:如果在线显示php源代码 通过php提供的函数highlight_file和highlight_string实现

  3. 算法起步之Kruskal算法

    原文:算法起步之Kruskal算法 说完并查集我们接着再来看这个算法,趁热打铁嘛.什么是最小生成树呢,很形象的一个形容就是铺自来水管道,一个村庄有很多的农舍,其实这个村庄我们可以看成一个图,而农舍就是 ...

  4. 1.SQL统计某张表的列数。

    select   count(syscolumns.name)    from   syscolumns   ,   sysobjects       where   syscolumns.id    ...

  5. QT4和QT3的区别

    著名的QT库前一阵子升级到4.xx版本了,我目前在开发的一个基于QT3的软件,由于受到QThread的各种困扰,因此打算尝试将代码升级到QT4, 但是当我实际开始升级工作后,才发现QT3和QT4的变化 ...

  6. 属性“dataProvider”有多个初始值设定项。(注意:“dataProvider”是“mx.charts.BarChart”的默认属性)。

    1.错误描写叙述 属性"dataProvider"有多个初始值设定项.(注意:"dataProvider"是"mx.charts.BarChart&q ...

  7. 【Unity 3D】学习笔记三十七:物理引擎——碰撞与休眠

    碰撞与休眠 上一篇笔记说过,当给予游戏对象刚体这个组件以后,那么这个组件将存在碰撞的可能性.一旦刚体開始运动,那么系统方法便会监视刚体的碰撞状态.一般刚体的碰撞分为三种:进入碰撞,碰撞中,和碰撞结束. ...

  8. 《算法导论》 — Chapter 7 高速排序

    序 高速排序(QuickSort)也是一种排序算法,对包括n个数组的输入数组.最坏情况执行时间为O(n^2). 尽管这个最坏情况执行时间比較差.可是高速排序一般是用于排序的最佳有用选择.这是由于其平均 ...

  9. Deploy Oracle 10.2.0.5 DataGuard on Red Hat Enterprise Linux 6.4

    系统:Red Hat Enterprise Linux 6.4 数据库:Oracle 10.2.0.5.0 Patch Set 4 主机:10dg1 192.168.1.91 10dg2192.168 ...

  10. Verifying Checksum ... Bad Data CRC 错误解决

    1.问题描述:使用SAM9X25  内核版本是2.6.39  在启动内核时会出现Verifying Checksum ... Bad Data CRC 错误 2.解决办法: 查看原先uboot参数: ...