|
越来越觉得Python可爱了,最近写一个过滤敏感词的类,因为好多地方需要用。于是查了一些资料。发现实现代码挺简单的,在这里记录一下。- import re
- from Common.CommonSingleton import CSingleton
- class CSensitiveFilter(CSingleton):
- #初始化类
- def __init__(self):
- self.filename='word.dat'
- self.strPatton='(';
- self.readFlag = False;
- self.pattern='';
- self.readFilterPatten();
-
- def init(self):
- if self.readFlag == False:
- self.readFilterPatten();
-
- #读取敏感词文件,构造过滤pattern
- def readFilterPatten(self):
-
- file = open(self.filename)
- for eachline in file:
- self.strPatton=self.strPatton+eachline.replace('\n','')+'|';
- self.strPatton=self.strPatton[:-1]+")"
- file.close();
- self.readFlag=True;
- self.pattern = re.compile(self.strPatton)
-
- #敏感词过滤
- def filter(self,message):
-
- return self.pattern.sub('*',message)
-
- #判断是否有敏感字
- def judgeSpecialSign(self,message):
-
- match = self.pattern.match(message.strip());
- if match == None:
- return True;
- return False;
复制代码 |
|