Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I didn't found any Python profanity library, so I made one myself.</p> <h2>Parameters</h2> <hr> <h3><code>filterlist</code></h3> <p>A list of regular expressions that match a forbidden word. Please do not use <code>\b</code>, it will be inserted depending on <code>inside_words</code>.</p> <p>Example: <code>['bad', 'un\w+']</code></p> <h3><code>ignore_case</code></h3> <p>Default: <code>True</code></p> <p>Self-explanatory.</p> <h3><code>replacements</code></h3> <p>Default: <code>"$@%-?!"</code></p> <p>A string with characters from which the replacements strings will be randomly generated.</p> <p>Examples: <code>"%&amp;$?!"</code> or <code>"-"</code> etc.</p> <h3><code>complete</code></h3> <p>Default: <code>True</code></p> <p>Controls if the entire string will be replaced or if the first and last chars will be kept.</p> <h3><code>inside_words</code></h3> <p>Default: <code>False</code></p> <p>Controls if words are searched inside other words too. Disabling this </p> <h2>Module source</h2> <hr> <p>(examples at the end)</p> <pre><code>""" Module that provides a class that filters profanities """ __author__ = "leoluk" __version__ = '0.0.1' import random import re class 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', 'un\w+'], 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." </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload