Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1033809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:17:20+00:00 2026-05-16T14:17:20+00:00

Like https://stackoverflow.com/questions/1521646/best-profanity-filter , but for Python — and I’m looking for libraries I can

  • 0

Like https://stackoverflow.com/questions/1521646/best-profanity-filter, but for Python — and I’m looking for libraries I can run and control myself locally, as opposed to web services.

(And whilst it’s always great to hear your fundamental objections of principle to profanity filtering, I’m not specifically looking for them here. I know profanity filtering can’t pick up every hurtful thing being said. I know swearing, in the grand scheme of things, isn’t a particularly big issue. I know you need some human input to deal with issues of content. I’d just like to find a good library, and see what use I can make of it.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T14:17:21+00:00Added an answer on May 16, 2026 at 2:17 pm

    I didn’t found any Python profanity library, so I made one myself.

    Parameters


    filterlist

    A list of regular expressions that match a forbidden word. Please do not use \b, it will be inserted depending on inside_words.

    Example:
    ['bad', 'un\w+']

    ignore_case

    Default: True

    Self-explanatory.

    replacements

    Default: "$@%-?!"

    A string with characters from which the replacements strings will be randomly generated.

    Examples: "%&$?!" or "-" etc.

    complete

    Default: True

    Controls if the entire string will be replaced or if the first and last chars will be kept.

    inside_words

    Default: False

    Controls if words are searched inside other words too. Disabling this

    Module source


    (examples at the end)

    """
    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."
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this a web url https://stackoverflow.com/questions/ 3260091 /databases-and-deep-copy how can
Further to https://stackoverflow.com/questions/8990246/frame-like-in-html/8990407#8990407 I implemented floating divs in my JSP. It works properly in
This is the same question as this: https://stackoverflow.com/questions/2890620/jquery-running-a-function-in-a-context-and-adding-to-a-variable But that question was poorly posed.
I am looking at such previous questions as: https://stackoverflow.com/questions/98334/creating-a-java-servlet-web-application How many actions should a
Duplicate: https://stackoverflow.com/questions/587676/why-do-programs-in-unix-like-environments-have-numbers-after-their-name/ For instance, if I type: man ps ...and then scroll to the
Related to https://stackoverflow.com/questions/139944/where-can-one-find-free-software-icons-images I have a need for free weather-related icons. Specifically, I need
I started to take a look at the following question: https://stackoverflow.com/questions/8616458/sql-query-like-clarification Unfortunately it appeared
I can retrieve the text of a web page, let's say https://stackoverflow.com/questions with some
Before you say, yeah this question can be duplicate; https://stackoverflow.com/questions/2912890/gridview-freeze-pane-solutions How to freeze GridView
How do I match an URL string like this: img src = https://stackoverflow.com/a/b/c/d/someimage.jpg where

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.