How to filter sensitive word in Python

Topics requirements are as follows:

  From the file parsing sensitive words, get user input from the terminal. The user input is filtered based on sensitive words. Here filtration filter need to consider more than one word: read all about to filter words, into a list, shielded word to retrieve user input, if stop words, it is replaced with a *, and if not, does not perform any input. Shield traversed until all words is completed, the filtered output string.

  Filter words are listed below:

  

  Specific steps are as follows:

    1. Read the demand from txt file mask sensitive word list

    2. Get input from the terminal user, then the list of statements sensitive input word matches, if the matching succeeds, with '*' alternative

  Code is implemented as follows:

import re

def read_txt(file_name):
    # 读取txt文件
    with open(file_name, 'r') as file_to_read:
        lines = list()
        for line in file_to_read.readlines():
            if line is not None:
                lines.append(line.strip('\n'))
    return lines

def shield_sensitive_word(file_name):
    # 屏蔽敏感词
    sensitive_word = read_txt(file_name)
    text = input('请输入:')
for pattern in sensitive_word: match = re.search(pattern, text) if match is not None: text = text.replace(pattern, '*') print(text) if __name__ == "__main__": path = r"C:\Users\Administrator\Desktop\Python_Study\python_small_program\filtered_words.txt" shield_sensitive_word(path)

 

Guess you like

Origin www.cnblogs.com/Summerhack/p/11387809.html