我正在寻找一些关于如何完成这些任务的想法:
- 允许第一次出现problem_word,但禁止任何后续使用它和其余的问题词。
- 不修改原始文档(.txt 文件)。仅针对 print() 进行修改。
- 保持电子邮件的相同结构。如果有换行符、制表符或奇怪的间距,让它们保持完整性。
这是代码示例:
import re
# Sample email is "Hello, banned1. This is banned2. What is going on with
# banned 3? Hopefully banned1 is alright."
sample_email = open('email.txt', 'r').read()
# First use of any of these words is allowed; those following are banned
problem_words = ['banned1', 'banned2', 'banned3']
# TODO: Filter negative_words into overused_negative_words
banned_problem_words = []
for w in problem_words:
if sample_email.count(f'\\b{w}s?\\b') > 1:
banned_problem_words.append(w)
pattern = '|'.join(f'\\b{w}s?\\b' for w in banned_problem_words)
def list_check(email, pattern):
return re.sub(pattern, 'REDACTED', email, flags=re.IGNORECASE)
print(list_check(sample_email, pattern))
# Result should be: "Hello, banned1. This is REDACTED. What is going on with
# REDACTED? Hopefully REDACTED is alright."