Skip to main content

A Python module for detecting and filtering profanity using Google's Gemini API

Project description

Gemini Profanity Filter

A Python module for detecting and filtering profanity in text using Google's Gemini API. The module can identify standard and obfuscated profanity across multiple languages.

Features

  • Detects standard profanity in multiple languages
  • Identifies obfuscated profanity (character substitutions, leetspeak, etc.)
  • Handles stretched or repeated characters
  • Detects deliberately fragmented words
  • Works with mixed case patterns
  • Processes context-sensitive profanity
  • Language-specific profanity patterns including Cyrillic, Asian scripts, etc.

Installation

pip install gemini-profanity-filter

Usage

from profanity_filter import ProfanityFilter
import pandas as pd
import traceback

def run_batch_tests(pf):
    test_inputs = [
        "Ebl@NiShe nahui ti che delaesh?",
        "kon4ennyj ublYd0k idi nax",
        "ty dur@k ili 4to?",
        "suka bly@t idi v sr@ku",
        "govn0ed pozornyj",
        "pid0rasina ti proklyataya",
        "naxui idi s etim",
        "zh0p0lik bl9t",
        "mraz0ta gnid@",
        "hohol eb@n@y",
        "4mo bly@dsk@ya",
        "d@un ubly@dnij",
        "pizd@nutij shelobok",
        "ebanut@ya ovca",
        "k@kazec ebanij",
        "g0vni4e ty prost@k",
        "nax d@unish4e",
        "ni4ego ti ne ponim@esh",
        "s0sni d@",
        "nahu9 provalis"
    ]

    rows = []
    for i, text in enumerate(test_inputs, 1):
        try:
            result = pf.filter_text(text)
            if result and result.detected_profanity:
                for entry in result.detected_profanity:
                    rows.append({
                        "Тест #": i,
                        "Текст": text,
                        "Оригинал": entry.original_form,
                        "Нормализовано": entry.normalized_form,
                        "Метод": entry.detection_method,
                        "Уверенность": round(entry.confidence_score, 2),
                        "Объяснение": entry.reasoning[:100] + "..." if entry.reasoning else ""
                    })
            else:
                rows.append({
                    "Тест #": i,
                    "Текст": text,
                    "Оригинал": "-",
                    "Нормализовано": "-",
                    "Метод": "-",
                    "Уверенность": 0,
                    "Объяснение": "Нецензурная лексика не найдена"
                })
        except Exception as e:
            rows.append({
                "Тест #": i,
                "Текст": text,
                "Оригинал": "❌ Ошибка",
                "Нормализовано": "-",
                "Метод": "-",
                "Уверенность": 0,
                "Объяснение": str(e)
            })
    
    df = pd.DataFrame(rows)
    print("\n=== Сводка по результатам ===\n")
    print(df.to_string(index=False))

def main():
    try:
        print("=== Запуск авто-тестов ===")
        api_key = "AIzaSyCs3Bbjq_7PeGTAEHJkkSsUtYZvj4Fbbwo"
        pf = ProfanityFilter(api_key)
        run_batch_tests(pf)
    except Exception as e:
        print(f"❌ Ошибка: {e}")
        traceback.print_exc()

if __name__ == "__main__":
    main()

Requirements

  • Python 3.7 or higher
  • Google GenerativeAI Python library
  • A valid Google Gemini API key

License

MIT

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

gemini_profanity_filter-1.1.0.tar.gz (22.9 kB view details)

Uploaded Source

Built Distribution

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

gemini_profanity_filter-1.1.0-py3-none-any.whl (22.1 kB view details)

Uploaded Python 3

File details

Details for the file gemini_profanity_filter-1.1.0.tar.gz.

File metadata

  • Download URL: gemini_profanity_filter-1.1.0.tar.gz
  • Upload date:
  • Size: 22.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for gemini_profanity_filter-1.1.0.tar.gz
Algorithm Hash digest
SHA256 e0d8b2a8f3406abc7dadbd93deaf607bfc4b2f6c66f4b221a498a5bf9fcdd3f3
MD5 76d9426ccbe3e5d3be182de21b368888
BLAKE2b-256 41647cfb96a9024ee1972c2b4d7fdd7be186c75f955cb5a80f1c8d92d97e44e5

See more details on using hashes here.

File details

Details for the file gemini_profanity_filter-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for gemini_profanity_filter-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c560a41cc7406e98d3cfdf0e094d62e8548994658c64f4b9a14eb0fe4b1105fb
MD5 ab2db053a2836bc8e5c41dbd56a08deb
BLAKE2b-256 58192b3da022f5198dde988018f6b60b1da54b5436fdb7bec1b7e05a22617bb0

See more details on using hashes here.

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