ComponentRegistry#

class scikitplot.corpus.ComponentRegistry[source]#

Central look-up table for corpus pipeline components.

Stores class references (not instances) for four component types: chunkers, filters, readers, and normalizers. Callers retrieve a class and instantiate it with their own parameters.

Notes

The module-level registry singleton is pre-populated with all built-in components via register_builtins. Third-party packages can register additional components after import.

Examples

>>> from scikitplot.corpus._registry import registry
>>> registry.register_builtins()
>>> chunker = registry.build_chunker("paragraph", min_length=20)
>>> type(chunker).__name__, chunker.config.min_length
('ParagraphChunker', 20)
build_chunker(name, **kwargs)[source]#

Instantiate the chunker registered under name.

Parameters:
namestr

Registry key.

**kwargs

For a chunker registered with a config class: the config’s fields, plus any other keyword option the chunker’s constructor takes (gensim_dictionary for word, adapter for semantic); a name that is both goes to the config. Or config= (a ready config, None for the default) with only constructor options beside it. Without a config class: the constructor’s keyword arguments.

Returns:
object

The chunker instance.

Raises:
KeyError

If name is not registered.

TypeError

If a keyword is neither a config field nor a constructor option (the message lists both), a field is given beside config=, or config= is not that config type.

Parameters:
Return type:

Any

Notes

Developer. Raw keyword arguments used to reach chunkers whose constructor takes only config=, so every named chunker raised TypeError for any option (R3-1).

Examples

>>> chunker = registry.build_chunker(
...     "fixed_window", window_size=50, step_size=25
... )
>>> chunker.config.window_size, chunker.config.step_size
(50, 25)
build_filter(name, **kwargs)[source]#

Instantiate the filter registered under name.

Parameters:
namestr
**kwargs
Returns:
FilterBase instance
Parameters:
Return type:

Any

build_normalizer(name, **kwargs)[source]#

Instantiate the normalizer registered under name.

Parameters:
namestr
**kwargs
Returns:
NormalizerBase instance
Parameters:
Return type:

Any

get_chunker(name)[source]#

Return the chunker class registered under name.

Parameters:
namestr

Registry key.

Returns:
type

The registered chunker class.

Raises:
KeyError

If name is not registered.

Parameters:

name (str)

Return type:

type

get_chunker_config(name)[source]#

Return the configuration dataclass registered for chunker name.

Parameters:
namestr

Registry key.

Returns:
type or None

The config class, or None when the chunker takes keyword arguments directly.

Raises:
KeyError

If no chunker is registered under name.

Parameters:

name (str)

Return type:

type | None

get_filter(name)[source]#

Return the filter class registered under name.

Parameters:
namestr
Returns:
type
Raises:
KeyError

If name is not registered.

Parameters:

name (str)

Return type:

type

get_normalizer(name)[source]#

Return the normalizer class registered under name.

Parameters:
namestr
Returns:
type
Raises:
KeyError

If name is not registered.

Parameters:

name (str)

Return type:

type

get_reader(name)[source]#

Return the reader class registered under name.

Parameters:
namestr
Returns:
type
Raises:
KeyError

If name is not registered.

Parameters:

name (str)

Return type:

type

list_chunkers()[source]#

Return sorted list of registered chunker names.

Return type:

list[str]

list_filters()[source]#

Return sorted list of registered filter names.

Return type:

list[str]

list_normalizers()[source]#

Return sorted list of registered normalizer names.

Return type:

list[str]

list_readers()[source]#

Return sorted list of registered reader names / extensions.

Return type:

list[str]

classmethod load_from_snapshot(snapshot, *, allowed_module_prefixes='scikitplot.')[source]#

Reconstruct a registry from a snapshot.

Parameters:
snapshotdict

Snapshot created by snapshot().

allowed_module_prefixesstr | list[str] | None, default=”scikitplot.”

If provided, only classes whose module starts with one of these prefixes are allowed. Recommended for security.

Caution

  • ⚠: Loading arbitrary FQCN from untrusted JSON is remote code execution risk.

Returns:
ComponentRegistry

New registry populated from snapshot.

Raises:
ValueError

If snapshot structure is invalid.

TypeError

If resolved class does not match expected base type.

Parameters:
Return type:

ComponentRegistry

register_builtins()[source]#

Register all built-in corpus pipeline components.

Safe to call multiple times — subsequent calls are no-ops. Triggers the necessary imports to populate the DocumentReader registry as well.

Notes

Importing scikitplot.corpus._readers as a side effect here is intentional: it populates the DocumentReader._registry extension map used by create.

A name already registered is kept: a component you registered before this call is never replaced by the built-in of the same name (the builder calls this on the shared registry when a build starts).

Return type:

None

register_chunker(name, cls, *, config=None, replace=False)[source]#

Register a chunker class under name.

Parameters:
namestr

Registry key (lowercase, underscore-separated). Must be non-empty.

clstype

A class satisfying ChunkerProtocol (chunk and chunk_batch).

configtype or None, optional

The chunker’s configuration dataclass. When given (or declared by the class as config_class, as every built-in does), build_chunker (and BuilderConfig.chunker_kwargs) build this config from keyword arguments and pass it as cls(config=...); unknown keys raise. When neither, keyword arguments go to cls directly. A class declaring config_class accepts only that class or a subclass.

replacebool, optional

Replace an existing registration deliberately. Default False.

Raises:
ValueError

If name is empty, or already registered and replace is False.

TypeError

If cls is not a conforming class, its chunk cannot take extra_metadata, or config is not a dataclass type (or not the class’s declared config_class).

Parameters:
Return type:

None

register_filter(name, cls, *, replace=False)[source]#

Register a filter class under name.

Parameters:
namestr
clstype

Concrete class inheriting from FilterBase.

Parameters:
Return type:

None

register_normalizer(name, cls, *, replace=False)[source]#

Register a normalizer class under name.

Parameters:
namestr

a normalizer class.

clstype

Concrete class inheriting from NormalizerBase.

replacebool

False

Parameters:
Return type:

None

Notes

Developer. Checked against NormalizerBase, the type NormalizationPipeline accepts. NormalizerProtocol (normalize(text)) describes a text-level interface no built-in normalizer has, so the old check refused every built-in, and with it every snapshot round trip (R3-1).

register_reader(name, cls, *, replace=False)[source]#

Register a reader class under name (typically a file extension).

Parameters:
namestr

File extension (e.g. \".txt\") or URL scheme key (e.g. \":url\").

clstype

Concrete class inheriting from DocumentReader.

replacebool

False

Parameters:
Return type:

None

snapshot()[source]#

Return a JSON-safe snapshot of all registered components.

Returns:
dict[str, dict[str, str]]

Keys: "chunkers", "chunker_configs", "filters", "readers", "normalizers". Values: dicts mapping name to the fully qualified class name.

Return type:

dict[str, dict[str, str]]