Chunk#

class scikitplot.corpus.Chunk(text, start_char, end_char, metadata=<factory>, context_start=None, context_end=None)[source]#

A single unit of text produced by a chunker.

Parameters:
textstr

The chunk’s text content (possibly including overlap context).

start_charint

Zero-based character offset of the chunk’s anchor in the source: the region the chunk stands for (a sentence, without the sentences repeated before it as overlap). Set to 0 when offsets are disabled.

end_charint

Zero-based exclusive end of the anchor. Set to 0 when offsets are disabled.

metadataMetadataDict

Arbitrary per-chunk key/value pairs (e.g. chunk_index, doc_id, backend, tokens). Always a dict — never None.

context_startint or None, optional

Start of the context span: the source region text is taken from, which covers the anchor (overlap context included). None means the anchor start.

context_endint or None, optional

Exclusive end of the context span; None means the anchor end.

Parameters:

Notes

  • Span invariant (R3-3). For every built-in chunker whose text_is_source_slice is True (all but WordChunker, whose text is a token transformation), text == source[context_start: context_end] when offsets are enabled, and the context span covers the anchor: context_start <= start_char <= end_char <= context_end.

  • text must be a non-empty string. Validation is the chunker’s responsibility; Chunk itself does not re-validate to keep it a pure value object.

  • metadata is stored as-is. Callers must not mutate the dict after construction (frozen dataclass contract).

property anchor_span: tuple[int, int]#

the region this chunk stands for.

Type:

(start_char, end_char)

char_length()[source]#

Return the character span length of this chunk.

Returns:
int

end_char - start_char. Returns 0 when offsets are disabled (both fields are 0).

Return type:

int

content_hash()[source]#

Return a stable SHA-256 hex digest of text.

Useful as a deduplication key in storage backends.

Returns:
str

64-character lowercase hex string.

Return type:

str

context_end: int | None = None#
property context_span: tuple[int, int]#

The source region text is taken from (covers the anchor).

context_start: int | None = None#
end_char: int[source]#
metadata: dict[str, Any][source]#
start_char: int[source]#
text: str[source]#
with_metadata(**kwargs)[source]#

Return a new Chunk with additional metadata keys.

Parameters:
**kwargs

Key/value pairs to merge into a copy of metadata.

Returns:
Chunk

New frozen instance with merged metadata.

Parameters:

kwargs (Any)

Return type:

Chunk

Notes

Uses dataclasses.replace so any new fields added to Chunk are automatically preserved without editing this method (CRITICAL-04 / LOW-02 fix).