网上搜的过滤敏感词的一段代码,测试后发现在过

网上搜的过滤敏感词的一段代码,测试后发现在过滤不干净。各位大神有空帮看下哪里的问题吧。

[Python] 纯文本查看 复制代码

?

001

002

003

004

005

006

007

008

009

010

011

012

013

014

015

016

017

018

019

020

021

022

023

024

025

026

027

028

029

030

031

032

033

034

035

036

037

038

039

040

041

042

043

044

045

046

047

048

049

050

051

052

053

054

055

056

057

058

059

060

061

062

063

064

065

066

067

068

069

070

071

072

073

074

075

076

077

078

079

080

081

082

083

084

085

086

087

088

089

090

091

092

093

094

095

096

097

098

099

100

101

102

103

104

105

106

107

108

109

110

import time

time1=time.time()

 

# AC自动机算法

class node(object):

    def __init__(self):

        self.next = {}

        self.fail = None

        self.isWord = False

        self.word = ""

 

class ac_automation(object):

 

    def __init__(self):

        self.root = node()

 

    # 添加敏感词函数

    def addword(self, word):

        temp_root = self.root

        for char in word:

            if char not in temp_root.next:

                temp_root.next[char] = node()

            temp_root = temp_root.next[char]

        temp_root.isWord = True

        temp_root.word = word

 

    # 失败指针函数

    def make_fail(self):

        temp_que = []

        temp_que.append(self.root)

        while len(temp_que) != 0:

            temp = temp_que.pop(0)

            p = None

            for key,value in temp.next.item():

                if temp == self.root:

                    temp.next[key].fail = self.root

                else:

                    p = temp.fail

                    while p is not None:

                        if key in p.next:

                            temp.next[key].fail = p.fail

                            break

                        p = p.fail

                    if p is None:

                        temp.next[key].fail = self.root

                temp_que.append(temp.next[key])

 

    # 查找敏感词函数

    def search(self, content):

        p = self.root

        result = []

        currentposition = 0

 

        while currentposition < len(content):

            word = content[currentposition]

            while word in p.next == False and p != self.root:

                p = p.fail

 

            if word in p.next:

                p = p.next[word]

            else:

                p = self.root

 

            if p.isWord:

                result.append(p.word)

                p = self.root

            currentposition += 1

        return result

 

    # 加载敏感词库函数

    def parse(self, path):

        with open(path,encoding='utf-8') as f:

            for keyword in f:

                self.addword(str(keyword).strip())

 

    # 敏感词替换函数

    def words_replace(self, text):

        """

        :param ah: AC自动机

        :param text: 文本

        :return: 过滤敏感词之后的文本

        """

        result = list(set(self.search(text)))

        for x in result:

            m = text.replace(x, '*' * len(x))

            text = m

        return text

 

 

 

 

 

if __name__ == '__main__':

 

    ah = ac_automation()

    path='e:/baidu_filter.txt'

    ah.parse(path)

    filename="e:/lbj.txt"

    fp=open(filename,'r')

    data=fp.read()

     

    text1=data

    text2=ah.words_replace(text1)

    rs=open("e:/rs.txt","w")

    rs.write(text2)

    rs.close()

    print(text1)

    print(text2)

 

    time2 = time.time()


下面是词库 [词库](链接:pan.baidu.com/s/1QYviLOHIkxKiDUYCf... 提取码:tf5q 复制这段内容后打开百度网盘手机 App,操作更方便哦–来自百度网盘超级会员 V4 的分享 “词库”)
这里是原文链接:pan.baidu.com/s/193rF_C7fg5W_hFR6T...
提取码:xojo
原文里的 非典 两个词不知道为什么 过滤不掉

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值