File tree Expand file tree Collapse file tree 3 files changed +53
-1
lines changed
Expand file tree Collapse file tree 3 files changed +53
-1
lines changed Original file line number Diff line number Diff line change 1+ #!/bin/env python
2+ # -*- coding: utf-8 -*-
3+
4+ #之前没写注释是因为是在Linux下做的,切不出想要的输入法(压根就没倒腾),我要Wine个Bing输入法
5+
6+ #导入模块
7+ import re
8+
9+ #从文件读取信息,以列表形式返回
10+ def read_file (filename ):
11+ l = []
12+ with open (filename ,'r' ) as fp :
13+ for line in fp .readlines ():
14+ l .append (line .strip ())
15+ return l
16+
17+ #生成对应的正则匹配规则
18+ def gen_pattern (l ):
19+ #看到好多用join方法的,因为我最初接触的是+,所以比较习惯,外加感觉很爽。如果join比较好的话,我慢慢改吧
20+ pattern = ''
21+ for string in l :
22+ pattern += string + '|'
23+ #复习了下切片,我刚刚还想试试直接减可不可以,那样python就太酷了,虽然已经非常酷了
24+ return pattern [:- 1 ]
25+
26+ #输入检测后输出
27+ def input_replace (pattern ):
28+ sentence = raw_input ('Please enter a sentence:' )
29+ #直接替换了,懒得先检测,然后判断分支了,没有就不替换
30+ print re .sub (pattern ,'**' ,sentence )
31+
32+ #主函数
33+ def main ():
34+ filename = 'filtered_words.txt'
35+ l = read_file (filename )
36+ pattern = gen_pattern (l )
37+ input_replace (pattern )
38+
39+ if __name__ == '__main__' :
40+ main ()
41+
42+
Original file line number Diff line number Diff line change 1+ 北京
2+ 程序员
3+ 公务员
4+ 领导
5+ 牛比
6+ 牛逼
7+ 你娘
8+ 你妈
9+ love
10+ sex
11+ jiangge
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments