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

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")


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值