今天的项目是与完成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. Cocos2d-x 3.1.1 学习日志8--2分钟让你知道cocos2d-x3.1.1 文本类别

    实际上文本经常使用的三个,LabelTTF,LabelBMF和LabelAtlas.而他们使用非常相似.所以,你会只举一反三,非常快就能够掌握了. <span style="font- ...

  2. UVA - 11388 GCD LCM

    II U C   ONLINE   C ON TEST  Problem D: GCD LCM Input: standard input Output: standard output The GC ...

  3. JavaScript 中的事件类型4(读书笔记思维导图)

    Web 浏览器中可能发生的事件有很多类型.如前所述,不同的事件类型具有不同的信息,而“ DOM3级事件”规定了以下几类事件. UI(User Interface,用户界面)事件:当用户与页面上的元素交 ...

  4. Json for Java API学习

    首先声明:本文来个非常多网友的博客,我通过參考了他们的博客,大致的了解了一些项目中经常使用的Json in java 类和方法,以及关于json的个人理解 个人对json的一些简单理解 在近期的学习中 ...

  5. BZOJ 3112 Zjoi2013 防守战线 单纯形

    题目大意: 单纯形*2.. . #include <cmath> #include <cstdio> #include <cstring> #include < ...

  6. OC-Protocol实现业务代理

    创建一个Protocol,相当于java的接口,但,有些方法不必实现,例如以下 #import <Foundation/Foundation.h> @protocol MyProtocol ...

  7. NYOJ 12 喷水装置(二)

    pid=12">喷水装置(二) 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描写叙述 有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n( ...

  8. Android中Broadcast Receiver组件具体解释

    BroadcastReceiver(广播接收器)是Android中的四大组件之中的一个. 以下是Android Doc中关于BroadcastReceiver的概述: ①广播接收器是一个专注于接收广播 ...

  9. C++基础学习教程(八)

    转载请注明出处:http://blog.csdn.net/suool/article/details/38300117 引入 在进行下一步的学习之前,我们须要厘清几个概念. RAII 首先介绍一个编程 ...

  10. java 字符串 asc 加密解密

    package com; public class MD5Test { /** * @param args */ public static void main(String[] args) { Sy ...