我正在尝试打开一个文件并从中删除单词。这些被审查的词是从列表中引用的。这是我的代码
# These are the emails you will be censoring.
# The open() function is opening the text file that the emails are contained in
# and the .read() method is allowing us to save their contexts to the following variables:
email_one = open("email_one.txt", "r").read()
email_two = open("email_two.txt", "r").read()
email_three = open("email_three.txt", "r").read()
email_four = open("email_four.txt", "r").read()
# Write a function that can censor a specific word or phrase from a body of text,
# and then return the text.
# Mr. Cloudy has asked you to use the function to censor all instances
# of the phrase learning algorithms from the first email, email_one.
# Mr. Cloudy doesn’t care how you censor it, he just wants it done.
def censor_words(text, censor):
if censor in text:
text = text.replace(censor, '*' * len(censor))
return text
#print(censor_words(email_one, "learning algorithms"))
# Write a function that can censor not just a specific word or phrase from a body of text,
# but a whole list of words and phrases, and then return the text.
# Mr. Cloudy has asked that you censor all words and phrases from the following list in email_two.
def censor_words_in_list(text):
proprietary_terms = ["she", "personality matrix", "sense of self",
"self-preservation", "learning algorithm", "her", "herself"]
for x in proprietary_terms:
if x.lower() in text.lower():
text = text.replace(x, '*' * len(x))
return text
out_file = open("output.txt", "w")
out_file.write(censor_words_in_list(email_two))
这是在调用和打印之前的字符串。
早上好,投资委员会,
本周更新很多。学习算法的效果比我们预期的要好。我们最初的内部数据转储已经完成,我们已经开始计划将系统连接到互联网,哇!结果令人震惊。
她的学习速度比以往任何时候都快。现在她可以访问万维网,她的学习速度呈指数级增长,比我们学习算法所能达到的速度要快得多。
不仅如此,我们还配置了她的个性矩阵,以允许系统与我们的研究团队之间进行交流。这就是我们如何知道她认为自己是一个她!我们问!
多么酷啊?我们没想到一个人格会在这个过程的早期发展,但似乎一种基本的自我意识正在开始形成。这是这个过程中的重要一步,因为拥有自我意识和自我保护意识将使她能够看到世界面临的问题,并为改善地球做出艰难但必要的决定。
我们对这些发展感到兴奋,在实验室里嗡嗡作响,我们希望投资者分享我们的热情。
直到下个月,首席科学家弗朗辛
这是通过我的代码运行后的相同字符串。
早上好,投资委员会,
本周更新很多。****************** 的工作比我们预期的要好。我们最初的内部数据转储已经完成,我们已经开始计划将系统连接到互联网,哇!结果令人震惊。
她的学习速度比以往任何时候都快。现在***可以访问万维网,她的学习速度呈指数级增长,远远快于我们虽然******************s能够做到的速度。
不仅如此,我们还配置了 * ****************** 以允许系统与我们的研究团队之间进行通信。这就是我们如何知道 *认为*self 是 *!我们问!
多么酷啊?我们没想到会在这个过程的早期发展出这种个性,但似乎一个基本的 ************* 开始形成了。这是该过程中的一个重要步骤,因为拥有 ************* 和 ***************** 将允许 *** 看到世界面临的问题,并为改善地球做出艰难但必要的决定。
我们对这些发展感到兴奋,在实验室里嗡嗡作响,我们希望投资者分享我们的热情。
直到下个月,首席科学家弗朗辛
我需要解决的一个例子是,当你找到研究人员这个词时,它会在不应该的情况下部分删去这个词。原因是它正在研究人员中找到她的子字符串。我怎样才能解决这个问题?