python - 从坏词列表创建审查函数

标签 python python-2.7

我正在尝试创建一个函数来审查字符串中的单词。它有点工作,有一些怪癖。

这是我的代码:

def censor(sentence):
    badwords = 'apple orange banana'.split()
    sentence = sentence.split()

    for i in badwords:
        for words in sentence:
            if i in words:
                pos = sentence.index(words)
                sentence.remove(words)
                sentence.insert(pos, '*' * len(i))

    print " ".join(sentence)

sentence = "you are an appletini and apple. new sentence: an orange is a banana. orange test."

censor(sentence)

输出:

you are an ***** and ***** new sentence: an ****** is a ****** ****** test.

一些标点符号消失了,并且单词“appletini”被错误地替换。

如何解决这个问题?

还有,有没有更简单的方法来做这种事情?

最佳答案

具体问题是:

  1. 你根本不考虑标点符号;和
  2. 插入 '*' 时,您使用的是“坏词”的长度,而不是单词的长度。

我会切换循环顺序,因此您只需处理该句子一次,并使用 enumerate而不是删除插入:

def censor(sentence):
    badwords = ("test", "word") # consider making this an argument too
    sentence = sentence.split()

    for index, word in enumerate(sentence):
        if any(badword in word for badword in badwords):
            sentence[index] = "".join(['*' if c.isalpha() else c for c in word])

    return " ".join(sentence) # return rather than print

测试str.isalpha将仅用星号替换大写和小写字母。演示:

>>> censor("Censor these testing words, will you? Here's a test-case!")
"Censor these ******* *****, will you? Here's a ****-****!"
            # ^ note length                         ^ note punctuation

关于python - 从坏词列表创建审查函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24738016/

相关文章:

python - 根据列表中的条件合并列表项

python - numpy 数组的追加与调整大小

python - 使用 doctesting 对类内部定义的方法进行单元测试

python - 有条件地加载模块 Python

python - 如何在事件回调之间保持 python 生成器的状态

python - getopt.getopt 中的 temp 保持为空

python-2.7 - 动态上下文无关语法NLTK

python - 如何在 Windows 10 中安装 Django

python - Flask 重启/关闭回调

python - 使用键作为标题和值作为列将字典写入 csv 时出现问题