ParentChildChunker#

class scikitplot.corpus.ParentChildChunker(config=None)[source]#

Large parents for context, small children for retrieval, linked.

Parameters:
configParentChildChunkerConfig or None, default=None

None is ParentChildChunkerConfig().

Parameters:

config (ParentChildChunkerConfig | None)

See also

RecursiveChunker

The splitting rule used at both levels.

scikitplot.corpus.children

Traverse the documents it produces.

Notes

User. Each parent is followed by its children. Every chunk’s metadata holds chunk_role ("parent" or "child") and parent_span; a child holds parent_chunk, the position of its parent in the result. A document built from a child gets parent_doc_id (its parent’s doc_id): retrieve children, then give the model the parent. Documents keep chunk_role and parent_span in their metadata, so with emit_parents=False a child still locates its parent’s region. Both levels report the RECURSIVE strategy; chunk_role tells them apart. A parent no longer than child_size has one child with the same text: index children only.

Examples

>>> text = "Alpha one. Beta two.\n\nGamma three. Delta four."
>>> config = ParentChildChunkerConfig(parent_size=25, child_size=12)
>>> result = ParentChildChunker(config).chunk(text)
>>> [
...     (c.metadata["chunk_role"], c.text) for c in result.chunks
... ]
[('parent', 'Alpha one. Beta two.'), ('child', 'Alpha one.'), ('child', 'Beta two.'),
 ('parent', 'Gamma three. Delta four.'), ('child', 'Gamma three.'), ('child', 'Delta four.')]
chunk(text, doc_id=None, extra_metadata=None)[source]#

Split text into parents and the children inside each.

Parameters:
textstr

Raw document text.

doc_idstr or None, default=None

Stored in each chunk’s metadata.

extra_metadatadict or None, default=None

Merged into the result metadata.

Returns:
ChunkResult

Parents and children in source order (each parent first).

Raises:
TypeError

If text is not a str.

ValueError

If text is empty or whitespace-only, or holds a NUL character or a lone surrogate.

Parameters:
Return type:

ChunkResult

chunk_batch(texts, doc_ids=None, extra_metadata=None)[source]#

Chunk a list of documents.

Parameters:
textslist of str

Input documents.

doc_idslist of str or None, default=None

Parallel document identifiers.

extra_metadatadict or None, default=None

Shared metadata for every result.

Returns:
list of ChunkResult

One result per document.

Raises:
TypeError

If texts is not a list.

ValueError

If doc_ids length mismatches texts.

Parameters:
Return type:

list[ChunkResult]

property config: ParentChildChunkerConfig#

The configuration (read-only).

config_class[source]#

alias of ParentChildChunkerConfig

strategy: ClassVar[ChunkingStrategy] = 'recursive'[source]#
text_is_source_slice = True#