API reference¶
The supported public API is exported from profanex. Construct a Filter once and reuse it; configuration and compiled filters are immutable after construction.
For configuration recipes and tradeoffs, see the configuration guide. For normalization and matching semantics, see How Profanex works.
Filter¶
profanex.api.Filter
¶
Compiled, immutable profanity filter.
Construct a filter once and reuse it. Construction loads and validates the effective lexicon and compiles the Rust matching engine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
FilterConfig | None
|
Immutable policy configuration. |
None
|
Raises:
| Type | Description |
|---|---|
ConfigError
|
If a configured lexicon cannot be loaded or validated. |
ImportError
|
If the native |
from_defaults
classmethod
¶
Construct a filter with the packaged lexicon and default policy.
contains
¶
Return whether text contains at least one accepted match.
This is the lowest-overhead single-text detection API and may stop after the first accepted match.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Source text to inspect. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
find
¶
Return ordered, non-overlapping matches in text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Source text to inspect. |
required |
Returns:
| Type | Description |
|---|---|
list[Match]
|
Matches with half-open indices into the original Python string. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
clean
¶
Return text with every accepted match masked.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Source text to filter. |
required |
Returns:
| Type | Description |
|---|---|
str
|
A new string transformed by the configured built-in or custom mask. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
analyze
¶
Return (contains_profanity, cleaned_text) from one scan.
Prefer this method over separate contains and clean calls
when both results are needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Source text to inspect and filter. |
required |
Returns:
| Type | Description |
|---|---|
tuple[bool, str]
|
A detection flag and the corresponding cleaned text. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
contains_many
¶
Detect profanity in a sequence while preserving input order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
texts
|
Sequence[str]
|
Finite sequence of strings. A bare string is rejected. |
required |
parallel
|
bool
|
Use the Rust worker pool when |
False
|
workers
|
int | None
|
Explicit worker count from 1 through 256. Requires |
None
|
Returns:
| Type | Description |
|---|---|
list[bool]
|
One detection flag per input string. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ConfigError
|
If parallel or worker options are invalid. |
find_many
¶
Find matches in a sequence while preserving input order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
texts
|
Sequence[str]
|
Finite sequence of strings. A bare string is rejected. |
required |
parallel
|
bool
|
Use the Rust worker pool when |
False
|
workers
|
int | None
|
Explicit worker count from 1 through 256. Requires |
None
|
Returns:
| Type | Description |
|---|---|
list[list[Match]]
|
One ordered match list per input string. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ConfigError
|
If parallel or worker options are invalid. |
clean_many
¶
Mask matches in a sequence while preserving input order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
texts
|
Sequence[str]
|
Finite sequence of strings. A bare string is rejected. |
required |
parallel
|
bool
|
Use the Rust worker pool when |
False
|
workers
|
int | None
|
Explicit worker count from 1 through 256. Requires |
None
|
Returns:
| Type | Description |
|---|---|
list[str]
|
One cleaned string per input string. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If an input or custom-mask result is not a string. |
ConfigError
|
If worker options are invalid or a custom mask is combined with parallel cleaning. |
analyze_many
¶
Return ordered flags and cleaned texts while scanning each input once.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
texts
|
Sequence[str]
|
Finite sequence of strings. A bare string is rejected. |
required |
parallel
|
bool
|
Use the Rust worker pool when |
False
|
workers
|
int | None
|
Explicit worker count from 1 through 256. Requires |
None
|
Returns:
| Type | Description |
|---|---|
tuple[list[bool], list[str]]
|
Parallel lists of detection flags and cleaned strings. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If an input or custom-mask result is not a string. |
ConfigError
|
If parallel or worker options are invalid. |
iter_contains
¶
Yield detection flags from any string iterable in bounded chunks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
texts
|
Iterable[str]
|
Iterable of strings. |
required |
chunk_size
|
int
|
Maximum inputs materialized per batch; must be positive. |
1000
|
parallel
|
bool
|
Process each chunk using the Rust worker pool. |
False
|
workers
|
int | None
|
Explicit worker count from 1 through 256. Requires |
None
|
Yields:
| Type | Description |
|---|---|
bool
|
Detection flags in input order. |
iter_find
¶
Yield match lists from any string iterable in bounded chunks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
texts
|
Iterable[str]
|
Iterable of strings. |
required |
chunk_size
|
int
|
Maximum inputs materialized per batch; must be positive. |
1000
|
parallel
|
bool
|
Process each chunk using the Rust worker pool. |
False
|
workers
|
int | None
|
Explicit worker count from 1 through 256. Requires |
None
|
Yields:
| Type | Description |
|---|---|
list[Match]
|
Ordered match lists in input order. |
iter_clean
¶
Yield cleaned strings from any string iterable in bounded chunks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
texts
|
Iterable[str]
|
Iterable of strings. |
required |
chunk_size
|
int
|
Maximum inputs materialized per batch; must be positive. |
1000
|
parallel
|
bool
|
Process each chunk using the Rust worker pool. |
False
|
workers
|
int | None
|
Explicit worker count from 1 through 256. Requires |
None
|
Yields:
| Type | Description |
|---|---|
str
|
Cleaned strings in input order. |
iter_analyze
¶
Yield detection and cleaning results from one scan per input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
texts
|
Iterable[str]
|
Iterable of strings. |
required |
chunk_size
|
int
|
Maximum inputs materialized per batch; must be positive. |
1000
|
parallel
|
bool
|
Process each chunk using the Rust worker pool. |
False
|
workers
|
int | None
|
Explicit worker count from 1 through 256. Requires |
None
|
Yields:
| Type | Description |
|---|---|
tuple[bool, str]
|
|
FilterConfig¶
profanex.api.FilterConfig
dataclass
¶
Immutable filter policy.
Numeric knobs, mask, categories, mutual exclusion, and in-memory lexicon
container shapes are validated eagerly. YAML path contents are validated when
constructing a Filter.
Attributes:
| Name | Type | Description |
|---|---|---|
categories |
frozenset[Category] | None
|
Enabled categories, or |
banned |
Collection[str] | Mapping[str, Collection[str]] | None
|
In-memory replacement banned lexicon. Flat terms use |
banned_path |
str | None
|
YAML replacement banned lexicon path. |
extra_banned |
Collection[str] | Mapping[str, Collection[str]] | None
|
Terms merged into the selected banned lexicon. Flat terms
use the same category assignment as |
extra_banned_path |
str | None
|
YAML terms merged into the selected banned lexicon. |
allowlist |
Collection[str] | None
|
In-memory replacement allowlist. |
allowlist_path |
str | None
|
YAML replacement allowlist path. |
extra_allowlist |
Collection[str] | None
|
Terms merged into the selected allowlist. |
extra_allowlist_path |
str | None
|
YAML terms merged into the selected allowlist. |
threshold |
int
|
Minimum fuzzy similarity score from 0 through 100. |
enable_fuzzy |
bool
|
Whether to run optional single-token fuzzy matching. |
min_fuzzy_len |
int
|
Minimum normalized token and term length for fuzzy matching. |
fuzzy_length_delta |
int
|
Maximum token/term length difference considered by fuzzy matching, from 0 through 64. |
fuzzy_max_token_len |
int
|
Maximum token length considered by fuzzy matching, capped at 256. |
word_boundaries |
bool
|
Whether accepted matches must satisfy Unicode word boundaries. |
mask |
MaskSpec
|
Built-in mask name or serial Python callback. |
normalize_leet |
bool
|
Whether to normalize the built-in leetspeak substitutions. |
normalize_separated_runs |
bool
|
Whether to join guarded separated character runs. |
normalize_repeated_chars |
bool
|
Whether to collapse character runs of length three or more. |
Raises:
| Type | Description |
|---|---|
ConfigError
|
If an option has the wrong type or range, mutually exclusive sources are combined, or an in-memory lexicon has an invalid shape. |
Match¶
profanex.api.Match
dataclass
¶
One accepted, non-overlapping profanity match.
Attributes:
| Name | Type | Description |
|---|---|---|
start |
int
|
Inclusive start index in the original Python string. |
end |
int
|
Exclusive end index in the original Python string. |
text |
str
|
Exact slice from the original string. Always equals
|
term |
str
|
Canonical term from the effective banned lexicon. |
categories |
frozenset[Category]
|
Enabled policy categories associated with |
score |
float
|
Similarity from 0 through 100. Exact and phrase matches use 100. |
kind |
Literal['exact', 'phrase', 'fuzzy']
|
Matching path: |
Category¶
profanex.lexicon.Category
¶
Bases: str, Enum
Built-in moderation-policy category.
ConfigError¶
profanex.lexicon.ConfigError
¶
Bases: ValueError
Invalid filter configuration or lexicon input.