Censore is a Python library that provides profanity filtering capabilities with support for multiple languages and custom patterns. The library is designed to be flexible, extensible, and easy to use while maintaining high performance.
├── benchmarks/ # Performance testing directory
│ └── benchmark.py # Benchmark test implementations
|
├── censore/ # Main package directory
│ ├── data/ # Resource files directory
│ │ ├── exclude_patterns/ # Folder with patterns to be excluded from filtering
│ │ │ ├── en.txt # English exclusion patterns
│ │ │ ├── uk.txt # Ukrainian exclusion patterns
│ │ │ └── ... # Other language exclusion patterns
| | |
│ │ └── patterns/ # Folder with profanity patterns
│ │ ├── en.txt # English profanity patterns
│ │ ├── uk.txt # Ukrainian profanity patterns
│ │ └── ... # Other language patterns
| |
│ ├── __init__.py # Package initialization and exports
│ └── profanity_filter.py # Core filtering implementation
|
└── tests/ # Test suite directory
├── __init__.py # Test package initialization
└── test.py # Test implementations
__init__.py: Exports the main classes and handles backward compatibility with the deprecatedCensorclassprofanity_filter.py: Contains the mainProfanityFilterclass implementation with all core functionality
data/*patterns/*.txt: Language-specific pattern files containing:- One pattern per line
- Lowercase entries
- No duplicates
- UTF-8 encoding
test.py: Unit tests for filtering functionality, censoring options, and language support
The main class that handles all profanity filtering functionality.
- Multi-language support
- Custom pattern support
- Text normalization
- Configurable censoring options
- Pattern exclusion support
censor(): Main method for censoring textcontains_profanity(): Check for presence of profanityadd_custom_language(): Add new language patternsadd_custom_profanity_patterns(): Add custom patterns
- Location:
/data/patterns/and/data/exclude_patterns/ - Format: Text files (.txt) for each language
- Structure: One pattern per line
- Naming:
{language_code}.txt
-
Regular Patterns
- Standard profanity words
- Common variations
- Language-specific terms
-
Exclude Patterns
- False positive prevention
- Context-aware exclusions
- Language-specific exceptions
- Input Processing
Raw Text → Word Tokenization → Strip Punctuation
- Word Analysis
Word → Normalization → Pattern Matching → Profanity Detection
- Censoring Pipeline
Detected Word → Censoring Rules Application → Text Reconstruction
- Dynamic language loading
- Language-specific pattern sets
- Support for "all" languages option
- Additional language injection
- Custom language definition
- Case-insensitive matching
- Character substitution handling (e.g., "0" → "o")
- Pattern exclusion system
- Custom pattern support
- Full word censoring
- Partial censoring (preserving first/last characters)
- Custom censoring character
- Pattern-based replacement
Input Text
↓
Language Selection
↓
Pattern Loading
↓
Text Tokenization
↓
Word Processing
│
├→ Normalization
│ - Character substitution
│ - Case normalization
│
├→ Pattern Matching
│ - Profanity detection
│ - Exclusion checking
│
└→ Censoring
- Pattern application
- Text reconstruction
↓
Output Text
- Uses sets for O(1) lookup
- Cached language patterns
- Optimized pattern loading
- Efficient string manipulation
- Minimal regex usage
- Optimized word splitting
- Lazy loading of language patterns
- Pattern set reuse
- Efficient data structures
profanity_filter.add_custom_language(
language="custom",
custom_patterns=["word1", "word2"],
exclude_patterns=["good_word"]
)profanity_filter.add_custom_profanity_patterns(
custom_patterns=[],
exclude_patterns=[],
language="custom"
)profanity_filter.censor(
text="input text",
partial_censor=True,
censor_symbol="*"
)- Load only required languages
- Use language-specific exclude patterns
- Maintain separate custom patterns
- Use lowercase patterns
- Include common variations
- Define specific exclusions
- Test patterns thoroughly
- Reuse filter instances
- Batch process similar texts
- Cache results when possible
-
Pattern Management
- Pattern scoring system
- Context-aware filtering
- Machine learning integration
-
Performance Optimization
- Parallel processing
- Pattern compilation
- Cached results
-
Feature Additions
- Regular expression support
- Contextual analysis
- Profanity severity levels
- API integration options
-
Unit Tests
- Individual method testing
- Pattern matching validation
- Language loading verification
-
Integration Tests
- Multi-language scenarios
- Custom pattern integration
- Full text processing
-
Performance Tests
- Large text processing
- Multiple language loading
- Pattern matching speed
- Python 3.6+
- Standard library only:
osstringtyping
-
Pattern File Security
- Validate file contents
- Protect pattern files
- Sanitize custom patterns
-
Input Validation
- Text length limits
- Character encoding
- Pattern validation
-
Output Sanitization
- Consistent censoring
- Safe character handling
- Unicode support