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

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

什么是好的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."


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5642984.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存