【python 走进NLP】两种高效过滤敏感词算法--DFA算法和AC自动机算法

2023-10-13 22:59

本文主要是介绍【python 走进NLP】两种高效过滤敏感词算法--DFA算法和AC自动机算法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在这里插入图片描述

一道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:returnlevel = self.keyword_chainsfor i in range(len(chars)):if chars[i] in level:level = level[chars[i]]else:if not isinstance(level, dict):breakfor 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}breakif i == len(chars) - 1:level[self.delimit] = 0def 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 = 0while start < len(message):level = self.keyword_chainsstep_ins = 0for char in message[start:]:if char in level:step_ins += 1if self.delimit not in level[char]:level = level[char]else:ret.append(repl * step_ins)start += step_ins - 1breakelse:ret.append(message[start])breakelse:ret.append(message[start])start += 1return ''.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:\laidefa\python.exe "E:/Program Files/pycharmproject/敏感词过滤算法/敏感词过滤算法DFA.py"
新疆骚乱苹果新品发布会雞八
****苹果新品发布会**
总共耗时:0.0010344982147216797sProcess 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 = Noneself.isWord = Falseself.word = ""class ac_automation(object):def __init__(self):self.root = node()# 添加敏感词函数def addword(self, word):temp_root = self.rootfor char in word:if char not in temp_root.next:temp_root.next[char] = node()temp_root = temp_root.next[char]temp_root.isWord = Truetemp_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 = Nonefor key,value in temp.next.item():if temp == self.root:temp.next[key].fail = self.rootelse:p = temp.failwhile p is not None:if key in p.next:temp.next[key].fail = p.failbreakp = p.failif p is None:temp.next[key].fail = self.roottemp_que.append(temp.next[key])# 查找敏感词函数def search(self, content):p = self.rootresult = []currentposition = 0while currentposition < len(content):word = content[currentposition]while word in p.next == False and p != self.root:p = p.failif word in p.next:p = p.next[word]else:p = self.rootif p.isWord:result.append(p.word)p = self.rootcurrentposition += 1return 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 = mreturn textif __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:\laidefa\python.exe "E:/Program Files/pycharmproject/敏感词过滤算法/AC自动机过滤敏感词算法.py"
新疆骚乱苹果新品发布会雞八
****苹果新品发布会**
总共耗时:0.0010304450988769531sProcess 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 linedata=pd.read_csv(path,sep='\t')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='\t',encoding='utf-8')

这篇关于【python 走进NLP】两种高效过滤敏感词算法--DFA算法和AC自动机算法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/206435

相关文章

使用Python的requests库调用API接口的详细步骤

《使用Python的requests库调用API接口的详细步骤》使用Python的requests库调用API接口是开发中最常用的方式之一,它简化了HTTP请求的处理流程,以下是详细步骤和实战示例,涵... 目录一、准备工作:安装 requests 库二、基本调用流程(以 RESTful API 为例)1.

Python清空Word段落样式的三种方法

《Python清空Word段落样式的三种方法》:本文主要介绍如何用python-docx库清空Word段落样式,提供三种方法:设置为Normal样式、清除直接格式、创建新Normal样式,注意需重... 目录方法一:直接设置段落样式为"Normal"方法二:清除所有直接格式设置方法三:创建新的Normal样

Python调用LibreOffice处理自动化文档的完整指南

《Python调用LibreOffice处理自动化文档的完整指南》在数字化转型的浪潮中,文档处理自动化已成为提升效率的关键,LibreOffice作为开源办公软件的佼佼者,其命令行功能结合Python... 目录引言一、环境搭建:三步构建自动化基石1. 安装LibreOffice与python2. 验证安装

把Python列表中的元素移动到开头的三种方法

《把Python列表中的元素移动到开头的三种方法》在Python编程中,我们经常需要对列表(list)进行操作,有时,我们希望将列表中的某个元素移动到最前面,使其成为第一项,本文给大家介绍了把Pyth... 目录一、查找删除插入法1. 找到元素的索引2. 移除元素3. 插入到列表开头二、使用列表切片(Lis

Python按照24个实用大方向精选的上千种工具库汇总整理

《Python按照24个实用大方向精选的上千种工具库汇总整理》本文整理了Python生态中近千个库,涵盖数据处理、图像处理、网络开发、Web框架、人工智能、科学计算、GUI工具、测试框架、环境管理等多... 目录1、数据处理文本处理特殊文本处理html/XML 解析文件处理配置文件处理文档相关日志管理日期和

Python标准库datetime模块日期和时间数据类型解读

《Python标准库datetime模块日期和时间数据类型解读》文章介绍Python中datetime模块的date、time、datetime类,用于处理日期、时间及日期时间结合体,通过属性获取时间... 目录Datetime常用类日期date类型使用时间 time 类型使用日期和时间的结合体–日期时间(

使用Python开发一个Ditto剪贴板数据导出工具

《使用Python开发一个Ditto剪贴板数据导出工具》在日常工作中,我们经常需要处理大量的剪贴板数据,下面将介绍如何使用Python的wxPython库开发一个图形化工具,实现从Ditto数据库中读... 目录前言运行结果项目需求分析技术选型核心功能实现1. Ditto数据库结构分析2. 数据库自动定位3

Python yield与yield from的简单使用方式

《Pythonyield与yieldfrom的简单使用方式》生成器通过yield定义,可在处理I/O时暂停执行并返回部分结果,待其他任务完成后继续,yieldfrom用于将一个生成器的值传递给另一... 目录python yield与yield from的使用代码结构总结Python yield与yield

python使用Akshare与Streamlit实现股票估值分析教程(图文代码)

《python使用Akshare与Streamlit实现股票估值分析教程(图文代码)》入职测试中的一道题,要求:从Akshare下载某一个股票近十年的财务报表包括,资产负债表,利润表,现金流量表,保存... 目录一、前言二、核心知识点梳理1、Akshare数据获取2、Pandas数据处理3、Matplotl

Django开发时如何避免频繁发送短信验证码(python图文代码)

《Django开发时如何避免频繁发送短信验证码(python图文代码)》Django开发时,为防止频繁发送验证码,后端需用Redis限制请求频率,结合管道技术提升效率,通过生产者消费者模式解耦业务逻辑... 目录避免频繁发送 验证码1. www.chinasem.cn避免频繁发送 验证码逻辑分析2. 避免频繁