您的位置:首页 > 编程语言 > Python开发

Yixiaohan--0011及0012题,敏感词过滤及替换

2016-06-27 16:33 573 查看
Yixiaohan 第0011题及0012题,由于两题类似,并在一起

主要是用中文分词库 jieba(可使用 pip install jieba 直接安装),及os文件操作

代码如下(仅供参考,环境为python3.5X,Mac OS系统):

import os
import jieba

fil = []

f = open('filtered_words.txt','r')
for fword in f.readlines():
fil.append(fword.strip())#把敏感词文件的每一个词加入列表
f.close()

for fword in fil:
jieba.add_word(fword) #把每一个敏感词都确保成为一个词,而不会被分成两个或更多的词

while True:
userInput = input("请检测是否敏感词:")
lwords = jieba.lcut(userInput.strip())
for word in lwords:
if word in fil:
#0011题
filterKey = True
#0012题,循环检测并替换,确保每一个敏感词都会被替换,而不是只替换第一个
userInput = userInput.replace(word,'**')
continue
else:
filterKey = False
# print("Human Rights")
if filterKey:
print("Freedom")
print(userInput)
else:
print("Human Rights")
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息