欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實現敏感詞過濾的五種方法

 更新時間:2025年04月04日 09:21:57   作者:IT之一小佬  
在我們生活中的一些場合經常會有一些不該出現的敏感詞,我們通常會使用*去屏蔽它,一些罵人的敏感詞和一些政治敏感詞都不應該出現在一些公共場合中,這個時候我們就需要一定的手段去屏蔽這些敏感詞,下面我來介紹一些簡單版本的Python敏感詞屏蔽的方法,需要的朋友可以參考下

1、replace替換

replace就是最簡單的字符串替換,當一串字符串中有可能會出現的敏感詞時,我們直接使用相應的replace方法用*替換出敏感詞即可。

缺點:

文本和敏感詞少的時候還可以,多的時候效率就比較差了。

示例代碼:

text = '我是一個來自星星的超人,具有超人本領!'
text = text.replace("超人", '*' * len("超人")).replace("星星", '*' * len("星星"))
print(text)  # 我是一個來自***的***,具有***本領!

運行結果:

如果是多個敏感詞可以用列表進行逐一替換。

示例代碼:

text = '我是一個來自星星的超人,具有超人本領!'
words = ['超人', '星星']
 
for word in words:
    text = text.replace(word, '*' * len(word))
print(text)  # 我是一個來自***的***,具有***本領!

運行效果:

2、正則表達式

使用正則表達式是一種簡單而有效的方法,可以快速地匹配敏感詞并進行過濾。在這里我們主要是使用“|”來進行匹配,“|”的意思是從多個目標字符串中選擇一個進行匹配。

示例代碼:

import re
 
 
def filter_words(text, words):
    pattern = '|'.join(words)
    return re.sub(pattern, '***', text)
 
 
if __name__ == '__main__':
    text = '我是一個來自星星的超人,具有超人本領!'
    words = ['超人', '星星']
    res = filter_words(text, words)
    print(res)  # 我是一個來自***的***,具有***本領!

運行結果:

3、使用ahocorasick第三方庫

ahocorasick庫安裝:

pip install pyahocorasick

示例代碼:

import ahocorasick
 
 
def filter_words(text, words):
    A = ahocorasick.Automaton()
    for index, word in enumerate(words):
        A.add_word(word, (index, word))
    A.make_automaton()
 
    result = []
    for end_index, (insert_order, original_value) in A.iter(text):
        start_index = end_index - len(original_value) + 1
        result.append((start_index, end_index))
 
    for start_index, end_index in result[::-1]:
        text = text[:start_index] + '*' * (end_index - start_index + 1) + text[end_index + 1:]
    return text
 
 
if __name__ == '__main__':
    text = '我是一個來自星星的超人,具有超人本領!'
    words = ['超人', '星星']
    res = filter_words(text, words)
    print(res)  # 我是一個來自***的***,具有***本領!

運行結果:

4、字典樹

使用字典樹是一種高效的方法,可以快速地匹配敏感詞并進行過濾。

示例代碼:

class TreeNode:
    def __init__(self):
        self.children = {}
        self.is_end = False
 
 
class Tree:
    def __init__(self):
        self.root = TreeNode()
 
    def insert(self, word):
        node = self.root
        for char in word:
            if char not in node.children:
                node.children[char] = TreeNode()
            node = node.children[char]
        node.is_end = True
 
    def search(self, word):
        node = self.root
        for char in word:
            if char not in node.children:
                return False
            node = node.children[char]
        return node.is_end
 
 
def filter_words(text, words):
    tree = Tree()
    for word in words:
        tree.insert(word)
 
    result = []
    for i in range(len(text)):
        node = tree.root
        for j in range(i, len(text)):
            if text[j] not in node.children:
                break
            node = node.children[text[j]]
            if node.is_end:
                result.append((i, j))
    for start_index, end_index in result[::-1]:
        text = text[:start_index] + '*' * (end_index - start_index + 1) + text[end_index + 1:]
    return text
 
 
if __name__ == '__main__':
    text = '我是一個來自星星的超人,具有超人本領!'
    words = ['超人', '星星']
    res = filter_words(text, words)
    print(res)  # 我是一個來自***的***,具有***本領!

運行結果:

5、DFA算法

使用DFA算法是一種高效的方法,可以快速地匹配敏感詞并進行過濾。DFA的算法,即Deterministic Finite Automaton算法,翻譯成中文就是確定有窮自動機算法。它的基本思想是基于狀態(tài)轉移來檢索敏感詞,只需要掃描一次待檢測文本,就能對所有敏感詞進行檢測。

示例代碼:

class DFA:
    def __init__(self, words):
        self.words = words
        self.build()
 
    def build(self):
        self.transitions = {}
        self.fails = {}
        self.outputs = {}
        state = 0
        for word in self.words:
            current_state = 0
            for char in word:
                next_state = self.transitions.get((current_state, char), None)
                if next_state is None:
                    state += 1
                    self.transitions[(current_state, char)] = state
                    current_state = state
                else:
                    current_state = next_state
            self.outputs[current_state] = word
        queue = []
        for (start_state, char), next_state in self.transitions.items():
            if start_state == 0:
                queue.append(next_state)
                self.fails[next_state] = 0
        while queue:
            r_state = queue.pop(0)
            for (state, char), next_state in self.transitions.items():
                if state == r_state:
                    queue.append(next_state)
                    fail_state = self.fails[state]
                    while (fail_state, char) not in self.transitions and fail_state != 0:
                        fail_state = self.fails[fail_state]
                    self.fails[next_state] = self.transitions.get((fail_state, char), 0)
                    if self.fails[next_state] in self.outputs:
                        self.outputs[next_state] += ', ' + self.outputs[self.fails[next_state]]
 
    def search(self, text):
        state = 0
        result = []
        for i, char in enumerate(text):
            while (state, char) not in self.transitions and state != 0:
                state = self.fails[state]
            state = self.transitions.get((state, char), 0)
            if state in self.outputs:
                result.append((i - len(self.outputs[state]) + 1, i))
        return result
 
 
def filter_words(text, words):
    dfa = DFA(words)
    result = []
    for start_index, end_index in dfa.search(text):
        result.append((start_index, end_index))
    for start_index, end_index in result[::-1]:
        text = text[:start_index] + '*' * (end_index - start_index + 1) + text[end_index + 1:]
    return text
 
 
if __name__ == '__main__':
    text = '我是一個來自星星的超人,具有超人本領!'
    words = ['超人', '星星']
    res = filter_words(text, words)
    print(res)  # 我是一個來自***的***,具有***本領!

運行結果:

到此這篇關于Python實現敏感詞過濾的五種方法的文章就介紹到這了,更多相關Python敏感詞過濾內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 用python實現海龜賽跑小游戲

    用python實現海龜賽跑小游戲

    大家好,本篇文章主要講的是用python實現海龜賽跑小游戲,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01
  • ubuntu系統(tǒng)下使用pm2設置nodejs開機自啟動的方法

    ubuntu系統(tǒng)下使用pm2設置nodejs開機自啟動的方法

    今天小編就為大家分享一篇ubuntu系統(tǒng)下使用pm2設置nodejs開機自啟動的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • 將Django框架和遺留的Web應用集成的方法

    將Django框架和遺留的Web應用集成的方法

    這篇文章主要介紹了將Django框架和遺留的Web應用集成的方法,Django是Python百花齊放的web開發(fā)框架中人氣最高的一個,需要的朋友可以參考下
    2015-07-07
  • Python使用pycharm導入pymysql教程

    Python使用pycharm導入pymysql教程

    這篇文章主要介紹了Python使用pycharm導入pymysql教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • Python的gevent框架的入門教程

    Python的gevent框架的入門教程

    這篇文章主要介紹了Python的gevent框架的入門教程,示例代碼基于Python2.x版本,需要的朋友可以參考下
    2015-04-04
  • Python使用三種方法實現PCA算法

    Python使用三種方法實現PCA算法

    這篇文章主要介紹了Python使用三種方法實現PCA算法,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • Python的凈值數據接口調用示例分享

    Python的凈值數據接口調用示例分享

    這篇文章主要介紹了Python的凈值數據接口調用示例分享的相關資料,需要的朋友可以參考下
    2016-03-03
  • python游戲測試工具自動化遍歷游戲中所有關卡

    python游戲測試工具自動化遍歷游戲中所有關卡

    這篇文章主要為大家介紹了python游戲測試工具自動化遍歷游戲中所有關卡示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-06-06
  • Pandas實現Excel文件讀取,增刪,打開,保存操作

    Pandas實現Excel文件讀取,增刪,打開,保存操作

    Pandas?是一種基于?NumPy?的開源數據分析工具,用于處理和分析大量數據。本文將通過Pandas實現對Excel文件進行讀取、增刪、打開、保存等操作,需要的可以參考一下
    2023-04-04
  • Python jieba結巴分詞原理及用法解析

    Python jieba結巴分詞原理及用法解析

    這篇文章主要介紹了Python jieba結巴分詞原理及用法解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-11-11

最新評論