[python每日一练]--0011:敏感词过滤 type1

题目链接:https://github.com/Show-Me-the-Code/show-me-the-code
代码github链接:https://github.com/wjsaya/python_spider_learn/tree/master/python_daily
个人博客地址:https://wjsaya.github.io
第 0011 题: 敏感词文本文件 filtered_words.txt,当用户输入敏感词语时,则打印出 Freedom,否则打印出 Human Rights。

1
2
3
4
北京
程序员
公务员
...

思路:

  1. 从文件解析敏感词。
  2. 根据敏感词决定输出。

敏感词列表(filtered_words.txt)

1
2
3
4
5
6
7
8
9
10
11
北京
程序员
公务员
领导
牛比
牛逼
你娘
你妈
love
sex
jiangge

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Author: wjsaya(http://www.wjsaya.top)
# @Date: 2018-08-10 10:33:32
# @Last Modified by: wjsaya(http://www.wjsaya.top)
# @Last Modified time: 2018-08-10 12:33:32
def get_dirty(fileName=''):
'''解析文件获取敏感词
'''
with open (fileName, 'r', encoding='utf-8') as f:
re = f.readlines()
for i in range(len(re)):
re[i] = re[i].strip('\n')
return(re)
def fliter(dirty_dict):
'''过滤敏感词
'''
instr = input("不要输入敏感词哦:")
for i in dirty_dict:
if (instr == i):
print('Freedom')
return 1
print('Human Rights')
return 0
if __name__ == '__main__':
file = 'filtered_words.txt'
dirty_dict = get_dirty(fileName=file)
while(1):
fliter(dirty_dict)

效果图:

0011

0%