RecursiveChunker#
- class scikitplot.corpus.RecursiveChunker(config=None)[source]#
Split text by a separator hierarchy until each piece fits a budget.
- Parameters:
- configRecursiveChunkerConfig or None, default=None
NoneisRecursiveChunkerConfig().
- Parameters:
config (RecursiveChunkerConfig | None)
See also
ParentChildChunkerThe same rule at two sizes, linked.
Notes
User. A paragraph that fits stays whole; a longer one is cut at line ends, then sentence ends, then spaces, then characters, only as far as needed. Each chunk’s
start_char/end_charis the piece it stands for; withchunk_overlapits text also holds the end of the previous chunk (context_span).Examples
>>> text = "Alpha one. Beta two.\n\nGamma three." >>> chunks = ( ... RecursiveChunker(RecursiveChunkerConfig(chunk_size=12)).chunk(text).chunks ... ) >>> [c.text for c in chunks] ['Alpha one.', 'Beta two.', 'Gamma three.'] >>> all(c.text == text[slice(*c.context_span)] for c in chunks) True
- chunk(text, doc_id=None, extra_metadata=None)[source]#
Split text into chunks of at most
chunk_sizecharacters.- 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
The chunks;
metadata["span_basis"]is"input".
- 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:
- 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:
- property config: RecursiveChunkerConfig#
The configuration (read-only).
- config_class[source]#
alias of
RecursiveChunkerConfig
- strategy: ClassVar[ChunkingStrategy] = 'recursive'[source]#
- text_is_source_slice = True#