Skip to main content

Python port of the Obscenity profanity filtering library

Project description

pyobscenity

PyPI - Version License

A Python port of the JavaScript library obscenity for detecting and censoring obscene words in text.

Note: Due to the nature of this library, repository contents may be considered NSFW or offensive.

Features

  • Simple API - Censor profanity with a single function call
  • Multiple censoring strategies - Full replacement, keep start/end, grawlix, fixed, random
  • Smart detection - Handles case variations, leet speak, Unicode lookalikes
  • Language support - Built-in English preset with 100+ phrases
  • Whitelist support - Exclude specific terms from censoring
  • Reusable filters - Build efficient, reusable filters for batch processing

Quick Start

Installation

pip install pyobscenity

Basic Usage

from pyobscenity import censor, check, find_matches

# Censor profanity
result = censor("This is fucking great!")
# "This is ****ing great!"

# Check if text contains profanity
if check(user_input):
    print("Please keep it clean!")

# Find all profanity matches
matches = find_matches("fuck this shit")
for match in matches:
    print(f"'{match.matched_text}' at position {match.start_index}")

Note: the default English filters are too aggressive for some use cases. Consider customizing the dataset or transformers for your needs. It is based on the original filter from the JavaScript library, with some minor adjustments.

Common Use Cases

Censor with Different Styles

from pyobscenity import censor

# Default (full replacement with *)
censor("This is shit")  
# "This is ****"

# Custom character
censor("shit", censor_char="#")  
# "####"

# Keep first character as hint
censor("shit", censor_type="keep_start", keep_length=1)  
# "s***"

# Fixed replacement
censor("shit", censor_type="fixed", replacement="[REDACTED]")  
# "[REDACTED]"

# Grawlix (comic book style)
censor("shit", censor_type="grawlix")  
# "@#$%"

Check and Find Matches

from pyobscenity import check, find_matches

# Simple check
has_profanity = check("hello world")  # False
has_profanity = check("hello shit")   # True

# Get match details
matches = find_matches("fuck this shit")
for m in matches:
    print(f"'{m.matched_text}' at [{m.start_index}:{m.end_index}]")

Custom Datasets

from pyobscenity import Dataset, ProfanityFilter

# Define custom profanity
dataset = (
    Dataset()
    .add_phrase(lambda p: p
        .add_pattern("|badword|")
        .add_whitelisted_term("badwords")  # Don't censor this
    )
    .add_phrase(lambda p: p.add_pattern("|naughty|"))
)

filter = ProfanityFilter.custom(dataset)
result = filter.censor("This is a badword")  # Censored
result = filter.censor("These are badwords") # Not censored

What Gets Detected?

The library detects:

  • Case variations: shit, SHIT, Shit
  • Leet speak: sh1t, 5h1t
  • Unicode lookalikes: shiт (with Cyrillic т)

These work automatically with the default English preset through intelligent text transformers.

Advanced features:

See ADVANCED_USAGE.md for:

  • Custom censoring strategies and transformers
  • Building domain-specific datasets
  • Low-level API access
  • Performance tuning
  • Architecture details

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyobscenity-0.1.1.tar.gz (35.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyobscenity-0.1.1-py3-none-any.whl (25.8 kB view details)

Uploaded Python 3

File details

Details for the file pyobscenity-0.1.1.tar.gz.

File metadata

  • Download URL: pyobscenity-0.1.1.tar.gz
  • Upload date:
  • Size: 35.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pyobscenity-0.1.1.tar.gz
Algorithm Hash digest
SHA256 4350a17bcc3cace578e8031f7658de59b1d8cef3bf44b861d65f8b33676a5d45
MD5 ddb35fcd4fa7c2fdef70318bbcc4e024
BLAKE2b-256 3d958a5f4917c43b60f702b2299d2a54ad6550e907a90d615094e6c4d575ba24

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyobscenity-0.1.1.tar.gz:

Publisher: python-publish.yml on gsteinLTU/pyobscenity

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyobscenity-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pyobscenity-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 25.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pyobscenity-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e603530c7ed02da71c39c90654859938f99ce79716e8824bf95d8c5de36ad847
MD5 a17fc1fe8d0be9a5eb20b2f63b7950ba
BLAKE2b-256 5342c7f258c816ae5a3a0f15516657b2636f27d05a1a03eeb8558e52b4fa0280

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyobscenity-0.1.1-py3-none-any.whl:

Publisher: python-publish.yml on gsteinLTU/pyobscenity

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page