0

我正在尝试打开一个文件并从中删除单词。这些被审查的词是从列表中引用的。这是我的代码

# 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 是 *!我们问!

多么酷啊?我们没想到会在这个过程的早期发展出这种个性,但似乎一个基本的 ************* 开始形成了。这是该过程中的一个重要步骤,因为拥有 ************* 和 ***************** 将允许 *** 看到世界面临的问题,并为改善地球做出艰难但必要的决定。

我们对这些发展感到兴奋,在实验室里嗡嗡作响,我们希望投资者分享我们的热情。

直到下个月,首席科学家弗朗辛

我需要解决的一个例子是,当你找到研究人员这个词时,它会在不应该的情况下部分删去这个词。原因是它正在研究人员中找到的子字符串。我怎样才能解决这个问题?

4

1 回答 1

1

使用正则表达式模块和单词边界锚 \b:

import re    
def censor_words_in_list(text):
    regex = re.compile(
    r'\bshe\b|\bpersonality matrix\b|\bsense of self\b'
    r'|\bself-preservation\b|\blearning algorithms\b|\bher\b|\bherself\b',
    re.IGNORECASE)
    matches = regex.finditer(text)
    # find location of matches in text

    for match in matches:
        # find how many * should be used based on length of match
        span = match.span()[1] - match.span()[0]
        replace_string = '@' * span
        # substitution expression based on match
        expression = r'\b{}\b'.format(match.group())
        text = re.sub(expression, replace_string, text, flags=re.IGNORECASE)
    return text


email_one = open("email_one.txt", "r").read()

out_file = open("output.txt", "w")
out_file.write(censor_words_in_list(email_one))
out_file.close()

输出(我使用了 @ 符号,因为 ** 用于创建粗体文本(像这样),因此对于 Stack Overflow 上以三个星号为界的文本,答案显示不正确):

早上好,投资委员会,

本周更新很多。@@@@@@@@@@@@@@@@@@@ 工作得比我们预期的要好。我们最初的内部数据转储已经完成,我们已经开始计划将系统连接到互联网,哇!结果令人震惊。

@@@ 的学习速度比以往任何时候都快。@@@ 学习率现在@@@ 可以访问万维网已经成倍增长,比我们学习算法的能力要快得多。

不仅如此,我们还配置了@@@ @@@@@@@@@@@@@@@@@ 以允许系统和我们的研究团队之间进行通信。这就是我们知道@@@ 将@@@@@@@ 视为@@@ 的方式!我们问!

多么酷啊?我们没想到会在这个过程的早期发展出这种个性,但似乎一个基本的@@@@@@@@@@@@@ 正在开始形成。这是该过程中的一个重要步骤,因为拥有@@@@@@@@@@@@@ 和@@@@@@@@@@@@@@@@@ 将允许@@@ 看到世界面临的问题,并为改善地球做出艰难但必要的决定。

我们对这些发展感到兴奋,在实验室里嗡嗡作响,我们希望投资者分享我们的热情。

直到下个月,首席科学家弗朗辛

于 2020-04-26T17:08:40.497 回答