(转)两种高效过滤敏感词算法--DFA算法和AC自动机算法

摘要:
returnlevel=self.keyword_chainsforiinrange(len(chars)):len(char)):编码='utf-8')asf:step_ins+=1ifself.delimitioninlevel[char]:ret.append(repl*step_ins)start+=step_ins-1breakelse:

原文:https://blog.csdn.net/u013421629/article/details/83178970

在这里插入图片描述

一道bat面试题:快速替换10亿条标题中的5万个敏感词,有哪些解决思路?
有十亿个标题,存在一个文件中,一行一个标题。有5万个敏感词,存在另一个文件。写一个程序过滤掉所有标题中的所有敏感词,保存到另一个文件中。

1、DFA过滤敏感词算法

在实现文字过滤的算法中,DFA是比较好的实现算法。DFA即Deterministic Finite Automaton,也就是确定有穷自动机。
算法核心是建立了以敏感词为基础的许多敏感词树。

python 实现DFA算法:

# -*- coding:utf-8 -*-

import time
time1=time.time()

# DFA算法
class DFAFilter():
    def __init__(self):
        self.keyword_chains = {}
        self.delimit = 'x00'

    def add(self, keyword):
        keyword = keyword.lower()
        chars = keyword.strip()
        if not chars:
            return
        level = self.keyword_chains
        for i in range(len(chars)):
            if chars[i] in level:
                level = level[chars[i]]
            else:
                if not isinstance(level, dict):
                    break
                for j in range(i, len(chars)):
                    level[chars[j]] = {}
                    last_level, last_char = level, chars[j]
                    level = level[chars[j]]
                last_level[last_char] = {self.delimit: 0}
                break
        if i == len(chars) - 1:
            level[self.delimit] = 0

    def parse(self, path):
        with open(path,encoding='utf-8') as f:
            for keyword in f:
                self.add(str(keyword).strip())

    def filter(self, message, repl="*"):
        message = message.lower()
        ret = []
        start = 0
        while start < len(message):
            level = self.keyword_chains
            step_ins = 0
            for char in message[start:]:
                if char in level:
                    step_ins += 1
                    if self.delimit not in level[char]:
                        level = level[char]
                    else:
                        ret.append(repl * step_ins)
                        start += step_ins - 1
                        break
                else:
                    ret.append(message[start])
                    break
            else:
                ret.append(message[start])
            start += 1

        return ''.join(ret)


if __name__ == "__main__":
    gfw = DFAFilter()
    path="F:/文本反垃圾算法/sensitive_words.txt"
    gfw.parse(path)
    text="新疆骚乱苹果新品发布会雞八"
    result = gfw.filter(text)

    print(text)
    print(result)
    time2 = time.time()
    print('总共耗时:' + str(time2 - time1) + 's')
 

 

运行效果:

E:laidefapython.exe "E:/Program Files/pycharmproject/敏感词过滤算法/敏感词过滤算法DFA.py"
新疆骚乱苹果新品发布会雞八
****苹果新品发布会**
总共耗时:0.0010344982147216797s

Process finished with exit code 0
 

 

2、AC自动机过滤敏感词算法

AC自动机:一个常见的例子就是给出n个单词,再给出一段包含m个字符的文章,让你找出有多少个单词在文章里出现过。
简单地讲,AC自动机就是字典树+kmp算法+失配指针

# -*- coding:utf-8 -*-

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='F:/文本反垃圾算法/sensitive_words.txt'
    ah.parse(path)
    text1="新疆骚乱苹果新品发布会雞八"
    text2=ah.words_replace(text1)

    print(text1)
    print(text2)

    time2 = time.time()
    print('总共耗时:' + str(time2 - time1) + 's')
 
E:laidefapython.exe "E:/Program Files/pycharmproject/敏感词过滤算法/AC自动机过滤敏感词算法.py"
新疆骚乱苹果新品发布会雞八
****苹果新品发布会**
总共耗时:0.0010304450988769531s

Process finished with exit code 0
 

 

3、java 实现参考链接:
https://www.cnblogs.com/AlanLee/p/5329555.html

4、敏感词生成

# -*- coding:utf-8 -*-

path = 'F:/文本反垃圾算法/sensitive_worlds7.txt'
from 敏感词过滤算法.langconv import *
import pandas as pd
import pypinyin


# 文本转拼音
def pinyin(text):
    """
    :param text: 文本
    :return: 文本转拼音
    """
    gap = ' '
    piny = gap.join(pypinyin.lazy_pinyin(text))
    return piny


# 繁体转简体
def tradition2simple(text):
    """
    :param text: 要过滤的文本
    :return: 繁体转简体函数
    """
    line = Converter('zh-hans').convert(text)
    return line



data=pd.read_csv(path,sep='	')

chinise_lable=[]
chinise_type=data['type']

for i in data['lable']:
    line=tradition2simple(i)
    chinise_lable.append(line)


chg_data=pd.DataFrame({'lable':chinise_lable,'type':chinise_type})



eng_lable=[]
eng_type=data['type']
for i in data['lable']:
    # print(i)
    piny=pinyin(i)
    # print(piny)
    eng_lable.append(piny)

eng_data=pd.DataFrame({'lable':eng_lable,'type':eng_type})
# print(eng_data)
# 合并
result=chg_data.append(eng_data,ignore_index=True)

# 数据框去重

res = result.drop_duplicates()
print(res)

# 输出
res.to_csv('F:/文本反垃圾算法/中英混合的敏感词10.txt',header=True,index=False,sep='	',encoding='utf-8')

免责声明:文章转载自《(转)两种高效过滤敏感词算法--DFA算法和AC自动机算法》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇ArcGIS的网络分析【转】.Net Core微服务入门全纪录(二)——Consul-服务注册与发现(上)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

[Matlab] 画信号的CWT,S变换,STFT时频图

先上效果图:  Matlab框架代码: %% Author Information % Author: Guoyang Liu % Email: virter1995@outlook.com % Date: 2019-11-28 (Update 1: 2020-12-14) % Function: Plot CWT and ST and STFT % O...

win10 安装wsl2 centos

win10 powershell(管理员身份)操作 安装choco(windows的包管理工具类似于brew) Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServiceP...

Python爬取酷狗飙升榜前十首(100)首,写入CSV文件

酷狗飙升榜,写入CSV文件 爬取酷狗音乐飙升榜的前十首歌名、歌手、时间,是一个很好的爬取网页内容的例子,对爬虫不熟悉的读者可以根据这个例子熟悉爬虫是如何爬取网页内容的。 需要用到的库:requests库、BeautifulSoup库、time库; 请求头:'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) A...

Vue中使用openlayer做风场图

<template> <div class="box"> <div ref="emap" id="map"></div> <div id="popup" class="ol-popup"> <a href="#" id="popup-closer" class...

Python与微信——itchat包

目录 itchat itchat 一安装itchat pip install itchat pip install echarts-python 二登陆并向文件传输助手发消息 ``` import itchat 登录 itchat.login() 发送消息,filehelper是文件传输助手 itchat.send(u'hello', 'fileh...

python_way ,day26 django_admin 自定义

1、想在admin中增加新的字段如图:    默认django只显示 def __str__(self)里面的return的值 from django.contrib import admin # Register your models here. from accets import models class NewAssetApprovalZ...