🧙♂️ Magic Profanity
magic_profanity is a Python library for detecting and censoring profanity in text using customizable word lists and character mappings. It supports English and Hinglish, with enhanced features including:
- Sentiment Analysis
- Text Enhancement Suggestions
📦 Installation
pip install magic_profanity
Requirements
- Python 3+
nltk(for sentiment analysis)
🚀 Usage
🔁 Importing the Library
from magic_profanity import ProfanityFilter
🛠️ Initializing the Profanity Filter
Basic initialization:
profanity_filter = ProfanityFilter()
With sentiment analysis:
profanity_filter = ProfanityFilter(enable_sentiment=True)
With all features enabled:
profanity_filter = ProfanityFilter(
enable_sentiment=True,
sentiment_options={
'custom_threshold': {'positive': 0.1, 'negative': -0.1},
'preprocess_text': True
},
enable_enhancement=True
)
📥 Loading Custom Words
From a list:
profanity_filter.load_words(["badword1", "badword2"])
From a file:
profanity_filter.load_words_from_file("path/to/custom_wordlist.txt")
🔍 Checking for Profanity
text = "This sentence contains a badword1 and a BadWord2."
if profanity_filter.has_profanity(text):
print("Profanity detected!")
else:
print("No profanity found.")
❌ Censoring Text
censored_text = profanity_filter.censor_text(text)
print(censored_text)
➕ Adding Custom Words
profanity_filter.add_custom_words(["newbadword1", "newbadword2"])
🔤 Custom Character Mappings
profanity_filter.char_map = {
"a": ("a", "@", "*", "4"),
"i": ("i", "*", "l", "1"),
"o": ("o", "*", "0", "@"),
# Add more mappings as needed
}
💬 Using Sentiment Analysis
Basic Sentiment Analysis
text = "This product is amazing! I'm really happy with it."
analysis = profanity_filter.analyze_text(text)
print(f"Censored text: {analysis['censored_text']}")
print(f"Contains profanity: {analysis['contains_profanity']}")
print(f"Sentiment: {analysis['sentiment']['classification']}")
print(f"Sentiment scores: {analysis['sentiment']['scores']}")
🔎 Detailed Sentiment Analysis
text = "This product is absolutely amazing! I couldn't be happier with it."
analysis = profanity_filter.analyze_text(text, detailed=True)
print(f"Sentiment: {analysis['sentiment']['classification']}")
print(f"Confidence: {analysis['sentiment']['confidence']}")
print(f"Emotion indicators: {analysis['sentiment']['emotion_indicators']}")
✨ Getting Text Enhancement Suggestions
text = "This damn product is terrible. I hate how it always breaks!"
analysis = profanity_filter.analyze_text(text)
# Print enhancement suggestions
suggestions = analysis['enhancement_suggestions']
for category, items in suggestions.items():
if category != 'overall_recommendations' and items:
print(f"\n{category.replace('_', ' ').title()}:")
for suggestion in items:
print(f"- Replace '{suggestion['original']}' with: {', '.join(suggestion['suggestions'])}")
elif category == 'overall_recommendations' and items:
print("\nOverall recommendations:")
for recommendation in items:
print(f"- {recommendation}")
🧪 Complete Example
# Initialize with all features enabled
# Initialize with all features enabled
profanity_filter = ProfanityFilter(
enable_sentiment=True,
sentiment_options={
'custom_threshold': {'positive': 0.1, 'negative': -0.1},
'preprocess_text': True
},
enable_enhancement=True
)
# Analyze text
text = "This damn product is terrible. I hate how it always breaks!"
analysis = profanity_filter.analyze_text(text, detailed=True)
# Use the analysis results
print(f"Censored: {analysis['censored_text']}")
print(f"Sentiment: {analysis['sentiment']['classification']} ({analysis['sentiment']['confidence']})")
if analysis['enhancement_suggestions']['politeness_improvements']:
print("\nSuggested improvements:")
for suggestion in analysis['enhancement_suggestions']['politeness_improvements']:
print(f"- Replace '{suggestion['original']}' with: {', '.join(suggestion['suggestions'])}")
🤝 Contributing
Contributions are welcome!
Please open an issue or pull request on GitHub with your suggestions, bug reports, or enhancements.
📄 License
This project is licensed under the MIT License – see the LICENSE file for details.
Metadata
Release files for magic-profanity 2.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| magic_profanity-2.0.1.tar.gz | 43.7 kB | Without provenance | Details for magic_profanity-2.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| magic_profanity-2.0.1-py3-none-any.whl | Python 3 | none | any | Details for magic_profanity-2.0.1-py3-none-any.whl |
Total release size: 83.9 kB
Release files / magic_profanity-2.0.1.tar.gz
| Download URL | magic_profanity-2.0.1.tar.gz |
|---|---|
| Size | 43.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
042a0c1dff3a23c9136850239e3eed6ffc28fba7a8894d7eb33e5441be09616b
|
|
BLAKE2b-256 checksum How to use checksums |
d04cf6f2fa84cc6089bf5d4bb7f119904b181f09c2f202bad76b7051083039ab
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Release files / magic_profanity-2.0.1-py3-none-any.whl
| Download URL | magic_profanity-2.0.1-py3-none-any.whl |
|---|---|
| Size | 40.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9cb2c9a9aa91a499e48fd64d0805959a2b3f80906e9818a3745f0a85b2ca6fb7
|
|
BLAKE2b-256 checksum How to use checksums |
e3425522e2afb37d7a0e65b0ea38dfc2b7f6cdf600726710f2650d7caf03c21d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Security for magic-profanity 2.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.