今天的项目是与完成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. struts2文件上传限制大小问题

    struts2默认文件上传大小为2M,如需改动默认大小,解决方法例如以下: <struts> <constant name="struts.multipart.maxSiz ...

  2. HDU 5107 线段树扫描线

    给出N个点(x,y).每一个点有一个高度h 给出M次询问.问在(x,y)范围内第k小的高度是多少,没有输出-1 (k<=10) 线段树扫描线 首先离散化Y坐标,以Y坐标建立线段树 对全部的点和询 ...

  3. Windows 8.1下 MySQL绿色版安装配置与使用

    原文:Windows 8.1下 MySQL绿色版安装配置与使用 Mysql-5.6.17-winx64操作步骤: 一.安装MySQL数据库 1.下载. 下载地址:http://downloads.my ...

  4. 使用 sphinx 制作简洁而又美观的文档

    使用 sphinx 制作简洁而又美观的文档 使用 sphinx 制作简洁而又美观的文档

  5. Amazon.com : The Odyssey of the Manual Toothbrusher

    Amazon.com : The Odyssey of the Manual Toothbrusher The Odyssey of the Manual Toothbrusher

  6. Deep Learning(深度学习) 学习笔记(四)

    神经概率语言模型,内容分为三块:问题,模型与准则,实验结果.[此节内容未完待续...] 1,语言模型问题 语言模型问题就是给定一个语言词典包括v个单词,对一个字串做出二元推断,推断其是否符合该语言表达 ...

  7. iOS发展 ---- 至iPhone 6自适应布局设计 Auto Layout

    Apple从iOS 6增加了Auto Layout后開始就比較委婉的開始鼓舞.建议开发人员使用自适应布局,可是到眼下为止,我感觉大多数开发人员一直在回避这个问题,无论是不是因为历史原因造成的,至少他们 ...

  8. Unity3D ITween!

    percentage +=0.001f; iTween.PutOnPath(gameObject,path,percentage); //You can cause the object to ori ...

  9. Wix打包系列(二)用户界面和本地化操作

    原文:Wix打包系列(二)用户界面和本地化操作 上一章节,我们已经大概知道如何对文件进行打包安装,不过我们也注意到,通过对Sample.wxs的编译链接,生成的msi安装包没有任何用户界面,只有一个安 ...

  10. 智能指针 shared_ptr 解析

    近期正在进行<Effective C++>的第二遍阅读,书里面多个条款涉及到了shared_ptr智能指针,介绍的太分散,学习起来麻烦.写篇blog整理一下. LinJM   @HQU s ...