这篇文章主要讲解了“Python敏感词过滤的实现方法”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Python敏感词过滤的实现方法”吧!
一个简单的实现
使用BSF(宽度优先搜索)进行实现
使用DFA(Deterministic Finite Automaton)进行实现
主要是通过循环和replace的方式进行敏感词的替换
class NaiveFilter(): '''Filter Messages from keywords very simple filter implementation >>> f = NaiveFilter() >>> f.parse("filepath") >>> f.filter("hello sexy baby") hello **** baby ''' def __init__(self): self.keywords = set([]) def parse(self, path): for keyword in open(path): self.keywords.add(keyword.strip().decode('utf-8').lower()) def filter(self, message, repl="*"): message = str(message).lower() for kw in self.keywords: message = message.replace(kw, repl) return message
对于搜索查找进行了优化,对于英语单词,直接进行了按词索引字典查找。对于其他语言模式,我们采用逐字符查找匹配的一种模式。
BFS:宽度优先搜索方式
class BSFilter: '''Filter Messages from keywords Use Back Sorted Mapping to reduce replacement times >>> f = BSFilter() >>> f.add("sexy") >>> f.filter("hello sexy baby") hello **** baby ''' def __init__(self): self.keywords = [] self.kwsets = set([]) self.bsdict = defaultdict(set) self.pat_en = re.compile(r'^[0-9a-zA-Z]+$') # english phrase or not def add(self, keyword): if not isinstance(keyword, str): keyword = keyword.decode('utf-8') keyword = keyword.lower() if keyword not in self.kwsets: self.keywords.append(keyword) self.kwsets.add(keyword) index = len(self.keywords) - 1 for word in keyword.split(): if self.pat_en.search(word): self.bsdict[word].add(index) else: for char in word: self.bsdict[char].add(index) def parse(self, path): with open(path, "r") as f: for keyword in f: self.add(keyword.strip()) def filter(self, message, repl="*"): if not isinstance(message, str): message = message.decode('utf-8') message = message.lower() for word in message.split(): if self.pat_en.search(word): for index in self.bsdict[word]: message = message.replace(self.keywords[index], repl) else: for char in word: for index in self.bsdict[char]: message = message.replace(self.keywords[index], repl) return message
DFA即Deterministic Finite Automaton,也就是确定有穷自动机。
使用了嵌套的字典来实现。
class DFAFilter(): '''Filter Messages from keywords Use DFA to keep algorithm perform constantly >>> f = DFAFilter() >>> f.add("sexy") >>> f.filter("hello sexy baby") hello **** baby ''' def __init__(self): self.keyword_chains = {} self.delimit = '\x00' def add(self, keyword): if not isinstance(keyword, str): keyword = keyword.decode('utf-8') 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(keyword.strip()) def filter(self, message, repl="*"): if not isinstance(message, str): message = message.decode('utf-8') 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)
感谢各位的阅读,以上就是“Python敏感词过滤的实现方法”的内容了,经过本文的学习后,相信大家对Python敏感词过滤的实现方法这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。