Skip to content

Commit 6052e0a

Browse files
committed
0012 done
1 parent 3b085e5 commit 6052e0a

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

natezhang93/0012/censor.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
__author__ = 'natezhang93'
4+
5+
'''
6+
第 0012 题: 敏感词文本文件 filtered_words.txt,里面的内容 和 0011题一样,
7+
当用户输入敏感词语,则用 星号 * 替换,例如当用户输入「北京是个好城市」,则变成「**是个好城市」。
8+
'''
9+
import string
10+
11+
class Filter:
12+
def __init__(self):
13+
self.data = []
14+
try:
15+
with open("filtered_words.txt", 'r') as fin:
16+
self.data = (fin.read().splitlines())
17+
fin.close()
18+
except:
19+
raise IOError
20+
21+
def check(self, checkword):
22+
result = ""
23+
for word in self.data:
24+
temp = string.replace(checkword, word, "**")
25+
if temp != checkword:
26+
result = temp
27+
if not result:
28+
result = checkword
29+
return result
30+
31+
def main():
32+
try:
33+
fil = Filter()
34+
except:
35+
print "ERROR OPENING FILE"
36+
return
37+
38+
while True:
39+
word = raw_input("> ")
40+
word = fil.check(word)
41+
print word
42+
43+
if __name__ == "__main__":
44+
main()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
北京
2+
程序员
3+
公务员
4+
领导
5+
牛比
6+
牛逼
7+
你娘
8+
你妈
9+
love
10+
sex
11+
jiangge

0 commit comments

Comments
 (0)