Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions natezhang93/0011/censor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'natezhang93'

'''
第 0011 题: 敏感词文本文件 filtered_words.txt,里面的内容为以下内容,
当用户输入敏感词语时,则打印出 Freedom,否则打印出 Human Rights。
'''

class Filter:
def __init__(self):
self.data = []
try:
with open("filtered_words.txt", 'r') as fin:
self.data = (fin.read().splitlines())
fin.close()
except:
raise IOError

def check(self, word):
try:
self.data.index(word)
result = 'Freedom'
except:
result = 'Human Rights'
return result

def main():
try:
fil = Filter()
except:
print "ERROR OPENING FILE"
return

while True:
word = raw_input("> ")
word = fil.check(word)
print word

if __name__ == "__main__":
main()
11 changes: 11 additions & 0 deletions natezhang93/0011/filtered_words.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
北京
程序员
公务员
领导
牛比
牛逼
你娘
你妈
love
sex
jiangge
44 changes: 44 additions & 0 deletions natezhang93/0012/censor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'natezhang93'

'''
第 0012 题: 敏感词文本文件 filtered_words.txt,里面的内容 和 0011题一样,
当用户输入敏感词语,则用 星号 * 替换,例如当用户输入「北京是个好城市」,则变成「**是个好城市」。
'''
import string

class Filter:
def __init__(self):
self.data = []
try:
with open("filtered_words.txt", 'r') as fin:
self.data = (fin.read().splitlines())
fin.close()
except:
raise IOError

def check(self, checkword):
result = ""
for word in self.data:
temp = string.replace(checkword, word, "**")
if temp != checkword:
result = temp
if not result:
result = checkword
return result

def main():
try:
fil = Filter()
except:
print "ERROR OPENING FILE"
return

while True:
word = raw_input("> ")
word = fil.check(word)
print word

if __name__ == "__main__":
main()
11 changes: 11 additions & 0 deletions natezhang93/0012/filtered_words.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
北京
程序员
公务员
领导
牛比
牛逼
你娘
你妈
love
sex
jiangge