python的其中一个强大之处就是它可以方便的集成很多的非标准库,今天在GitHub上溜达又发现了一个脏话处理神器,导入better_profanity库后,只需要几行代码就能搞定了,相当nice!
使用pip的方式将better_profanity非标准库安装好,这个库好像在清华大学的镜像站中没有,其他镜像站不知道有没有,于是下载时没有使用镜像站,默认到官方去下载即可。
pip install better_profanity
# 将处理模块直接导入到代码块中
from better_profanity import profanity
1、默认脏话库/敏感词库处理
默认情况下就只能处理英文的脏话。
censored_text = profanity.censor("you are bitch")
print(censored_text)
# you are ****
可以看到其中bitch字符被认为是脏话已经处理成****字符了。
当然,还可以将处理后的脏话字符换成别的字符代替,比如下面这样处理。
censored_text = profanity.censor("you are bitch",'-')
print(censored_text)
# you are ----
这样****就被替换成了----。
2、自定义过滤信息处理
bad_words = ['Python', 'Java', 'Scala'] # 自定义过滤词汇
profanity.load_censor_words(bad_words)