栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

什么是好的Python亵渎过滤器库?[关闭]

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

什么是好的Python亵渎过滤器库?[关闭]

我没有找到任何Python亵渎库,所以我自己做了一个。

参量


filterlist

与禁止字匹配的正则表达式列表。请不要使用

b
,它会根据插入
inside_words

例:

['bad', 'unw+']

ignore_case

默认:

True

不言自明。

replacements

默认:

"$@%-?!"

一个带有字符的字符串,将从中随机生成替换字符串。

例子:

"%&$?!"
"-"

complete

默认:

True

控制是否将替换整个字符串,或者是否保留第一个和最后一个字符。

inside_words

默认:

False

控制是否也在其他单词内搜索单词。禁用此

模块来源


(最后的例子)

"""Module that provides a class that filters profanities"""__author__ = "leoluk"__version__ = '0.0.1'import randomimport reclass ProfanitiesFilter(object):    def __init__(self, filterlist, ignore_case=True, replacements="$@%-?!",       complete=True, inside_words=False):        """        Inits the profanity filter.        filterlist -- a list of regular expressions that        matches words that are forbidden        ignore_case -- ignore capitalization        replacements -- string with characters to replace the forbidden word        complete -- completely remove the word or keep the first and last char?        inside_words -- search inside other words?        """        self.badwords = filterlist        self.ignore_case = ignore_case        self.replacements = replacements        self.complete = complete        self.inside_words = inside_words    def _make_clean_word(self, length):        """        Generates a random replacement string of a given length        using the chars in self.replacements.        """        return ''.join([random.choice(self.replacements) for i in       range(length)])    def __replacer(self, match):        value = match.group()        if self.complete: return self._make_clean_word(len(value))        else: return value[0]+self._make_clean_word(len(value)-2)+value[-1]    def clean(self, text):        """Cleans a string from profanity."""        regexp_insidewords = { True: r'(%s)', False: r'b(%s)b', }        regexp = (regexp_insidewords[self.inside_words] %        '|'.join(self.badwords))        r = re.compile(regexp, re.IGNORECASE if self.ignore_case else 0)        return r.sub(self.__replacer, text)if __name__ == '__main__':    f = ProfanitiesFilter(['bad', 'unw+'], replacements="-")        example = "I am doing bad ungood badlike things."    print f.clean(example)    # Returns "I am doing --- ------ badlike things."    f.inside_words = True        print f.clean(example)    # Returns "I am doing --- ------ ---like things."    f.complete = False        print f.clean(example)    # Returns "I am doing b-d u----d b-dlike things."


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/637153.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号