File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #! /usr/bin/python
2+ # -*- coding: utf-8 -*-
3+ # word_filter.py
4+ # author: robot527
5+ # created at 2016-4-22
6+
7+ '''
8+ 敏感词文本文件 filtered_words.txt,里面的内容为每一行一个敏感词。
9+ 当用户输入敏感词语时,则打印出 Freedom,否则打印出 Human Rights。
10+ '''
11+
12+ def get_words (words_file ):
13+ '''Get words from words_file'''
14+ words = []
15+ text = open (words_file )
16+ for each_line in text :
17+ words .append (each_line .strip ().decode ('gb18030' ))
18+ #for w in words:
19+ # print w, type(w)
20+ text .close ()
21+ return words
22+
23+
24+ def str_to_unicode (str1 ):
25+ '''Convert str1 to unicode'''
26+ if isinstance (str1 , unicode ):
27+ return str1
28+ try :
29+ string_uni = str1 .decode ("utf-8" )
30+ return string_uni
31+ except UnicodeDecodeError as err :
32+ print err
33+
34+
35+ if __name__ == '__main__' :
36+ sensitive_words = get_words ("filtered_words.txt" )
37+ running = True
38+ while running :
39+ print 'Please input a word, or press q for exit.'
40+ try :
41+ para = raw_input ("-> " )
42+ except EOFError :
43+ print "If you want to quit, press Q key.\n "
44+ continue
45+ #uw = str_to_unicode(para)
46+ #print uw, type(uw)
47+ if str_to_unicode (para ) in sensitive_words :
48+ print "Freedom\n "
49+ elif para is 'q' :
50+ para = raw_input ("Do you really want to exit ([y]/n)?" )
51+ if para is not 'n' :
52+ running = False
53+ else :
54+ print "Human Rights\n "
Original file line number Diff line number Diff line change 1+ 北京
2+ 程序员
3+ 公务员
4+ 领导
5+ 牛比
6+ 牛逼
7+ 你娘
8+ 你妈
9+ love
10+ sex
11+ jiangge
Original file line number Diff line number Diff line change 1+ #! /usr/bin/python
2+ # -*- coding: utf-8 -*-
3+ # shield_sensitive_words.py
4+ # author: robot527
5+ # created at 2016-4-26
6+
7+ '''
8+ 敏感词文本文件 filtered_words.txt,里面的内容为每一行一个敏感词。
9+ 北京
10+ 程序员
11+ 公务员
12+ 领导
13+ 牛比
14+ 牛逼
15+ 你娘
16+ 你妈
17+ love
18+ sex
19+ jiangge
20+
21+ 当用户输入敏感词语时,则用星号 * 替换,
22+ 例如当用户输入「北京是个好城市」,则变成「**是个好城市」。
23+ '''
24+
25+ def get_replace_str (word ):
26+ '''Generate word replace string for word'''
27+ if word .isalnum ():
28+ return '*' * len (word )
29+ else :
30+ return '*' * (len (word ) / 3 )
31+
32+
33+ def get_words_dict (words_file ):
34+ '''Generate word replace dictionary from words_file'''
35+ words = {}
36+ text = open (words_file )
37+ for each_line in text :
38+ word = each_line .strip ()
39+ words [word ] = get_replace_str (word )
40+ text .close ()
41+ return words
42+
43+
44+ if __name__ == '__main__' :
45+ sensitive_words = get_words_dict ("filtered_words.txt" )
46+ running = True
47+ while running :
48+ print 'Please input a sentence, or press q for exit.'
49+ try :
50+ para = raw_input ("-> " )
51+ except EOFError :
52+ print "If you want to quit, press Q key.\n "
53+ continue
54+ if para is 'q' :
55+ para = raw_input ("Do you really want to exit ([y]/n)?" )
56+ if para is not 'n' :
57+ running = False
58+ else :
59+ for each in sensitive_words .keys ():
60+ para = para .replace (each , sensitive_words [each ])
61+ print para , '\n '
You can’t perform that action at this time.
0 commit comments