Provides a high-level interface for text chunking, employing a configurable splitting strategy (defaults to recursive splitting). Manages options and coordinates the process, tracking chunk metadata.
Key Features
- Customizable Splitting: Allows the splitting strategy to be customized via the
:strategyoption. - Size and Overlap Control: Provides options for
:chunk_sizeand:chunk_overlap. - Metadata Tracking: Generates
Chunkstructs containing byte range information.
Supported Options
:chunk_size(positive integer, default: 2000) - Maximum size of each chunk, measured by:get_chunk_size(graphemes by default).:get_chunk_size(function, default: &String.length/1) - A function used to measure chunk size, defaulting to graphemes.:chunk_overlap(non-negative integer, default: 200) - Maximum overlap between consecutive chunks, measured the same way as:chunk_size. Overlap is not guaranteed. Must not be greater than:chunk_size.:strategy(module, default:RecursiveChunk) - A module declaring@behaviour TextChunker.Strategy.:format(atom, default::plaintext) - The format of the input text. Used to determine where to split the text in some strategies.
Invalid options raise TextChunker.Error.
Summary
Functions
Splits the provided text into a list of %Chunk{} structs.
Returns the list of supported :format values.
Functions
@spec split(binary(), keyword()) :: [TextChunker.Chunk.t()]
Splits the provided text into a list of %Chunk{} structs.
Raises TextChunker.Error if the options are invalid.
Examples
iex> long_text = "This is a very long text that needs to be split into smaller pieces for easier handling."
iex> TextChunker.split(long_text)
# => [%Chunk{}, %Chunk{}, ...]
iex> TextChunker.split(long_text, chunk_size: 10, chunk_overlap: 3)
# => Generates many smaller chunks with significant overlap
@spec supported_formats() :: [atom()]
Returns the list of supported :format values.