A Python library for detecting and censoring profanity in text
Project description
🧙♂️ 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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file magic_profanity-2.0.1.tar.gz.
File metadata
- Download URL: magic_profanity-2.0.1.tar.gz
- Upload date:
- Size: 43.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
042a0c1dff3a23c9136850239e3eed6ffc28fba7a8894d7eb33e5441be09616b
|
|
| MD5 |
3937a3f11f66a004ef8c71cd1d5b6cb3
|
|
| BLAKE2b-256 |
d04cf6f2fa84cc6089bf5d4bb7f119904b181f09c2f202bad76b7051083039ab
|
File details
Details for the file magic_profanity-2.0.1-py3-none-any.whl.
File metadata
- Download URL: magic_profanity-2.0.1-py3-none-any.whl
- Upload date:
- Size: 40.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9cb2c9a9aa91a499e48fd64d0805959a2b3f80906e9818a3745f0a85b2ca6fb7
|
|
| MD5 |
1b555dd647003d6f971d554806b41d97
|
|
| BLAKE2b-256 |
e3425522e2afb37d7a0e65b0ea38dfc2b7f6cdf600726710f2650d7caf03c21d
|