敏感词文本文件 filtered_words.txt,当用户输入敏感词语,则用 星号 * 替换,例如当用户输入「北京是个好城市」,则变成「**是个好城市」

思路:

1. 读取敏感词汇文件,把词汇以list储存。

2. 匹配用户输入的内容是否含有敏感词汇,

import re

def is_Chinese(word):
    for ch in word:
        if '\u4e00' <= ch <= '\u9fff':
            return True
    return False
with open('./vocabulary.txt', encoding="utf-8") as ff:
	text = ff.read()
	text = re.sub('\s', ' ', text).split(' ')
	name = input()
	for x in text:
		if re.findall(x, name):
			for item in re.findall(x, name):
				if is_Chinese(item):
					name = re.sub(x, '*' * len(x), name)
				else:
					name = re.sub(x, '*', name)
	print(name)

猜你喜欢

转载自blog.csdn.net/Mrceel/article/details/82216710