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
54 changes: 54 additions & 0 deletions robot527/0011/word_filter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#! /usr/bin/python
# -*- coding: utf-8 -*-
# word_filter.py
# author: robot527
# created at 2016-4-22

'''
���д��ı��ļ� filtered_words.txt�����������Ϊÿһ��һ�����дʡ�
���û��������д���ʱ�����ӡ�� Freedom�������ӡ�� Human Rights��
'''

def get_words(words_file):
'''Get words from words_file'''
words = []
text = open(words_file)
for each_line in text:
words.append(each_line.strip().decode('gb18030'))
#for w in words:
# print w, type(w)
text.close()
return words


def str_to_unicode(str1):
'''Convert str1 to unicode'''
if isinstance(str1, unicode):
return str1
try:
string_uni = str1.decode("utf-8")
return string_uni
except UnicodeDecodeError as err:
print err


if __name__ == '__main__':
sensitive_words = get_words("filtered_words.txt")
running = True
while running:
print 'Please input a word, or press q for exit.'
try:
para = raw_input("-> ")
except EOFError:
print "If you want to quit, press Q key.\n"
continue
#uw = str_to_unicode(para)
#print uw, type(uw)
if str_to_unicode(para) in sensitive_words:
print "Freedom\n"
elif para is 'q':
para = raw_input("Do you really want to exit ([y]/n)?")
if para is not 'n':
running = False
else:
print "Human Rights\n"
11 changes: 11 additions & 0 deletions robot527/0012/filtered_words.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
北京
程序员
公务员
领导
牛比
牛逼
你娘
你妈
love
sex
jiangge
61 changes: 61 additions & 0 deletions robot527/0012/shield_sensitive_words.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#! /usr/bin/python
# -*- coding: utf-8 -*-
# shield_sensitive_words.py
# author: robot527
# created at 2016-4-26

'''
敏感词文本文件 filtered_words.txt,里面的内容为每一行一个敏感词。
北京
程序员
公务员
领导
牛比
牛逼
你娘
你妈
love
sex
jiangge
当用户输入敏感词语时,则用星号 * 替换,
例如当用户输入「北京是个好城市」,则变成「**是个好城市」。
'''

def get_replace_str(word):
'''Generate word replace string for word'''
if word.isalnum():
return '*' * len(word)
else:
return '*' * (len(word) / 3)


def get_words_dict(words_file):
'''Generate word replace dictionary from words_file'''
words = {}
text = open(words_file)
for each_line in text:
word = each_line.strip()
words[word] = get_replace_str(word)
text.close()
return words


if __name__ == '__main__':
sensitive_words = get_words_dict("filtered_words.txt")
running = True
while running:
print 'Please input a sentence, or press q for exit.'
try:
para = raw_input("-> ")
except EOFError:
print "If you want to quit, press Q key.\n"
continue
if para is 'q':
para = raw_input("Do you really want to exit ([y]/n)?")
if para is not 'n':
running = False
else:
for each in sensitive_words.keys():
para = para.replace(each, sensitive_words[each])
print para, '\n'