DefaultTextChunker
struct DefaultTextChunker: Sendable, Equatable, Hashable, TextChunkerBuilt-in text chunker supporting character sliding windows and layout-preserving word windows.
-
Creates a text chunker after validating window bounds for
mode.When
mode == .wordandwordThresholdisnil,chunkSizeis used aswordThresholdandchunkSize - chunkOverlapbecomes the minimum sentence-split word count.Declaration
Swift
init(chunkSize: Int = 500, chunkOverlap: Int = 100, wordThreshold: Int? = nil, mode: ChunkingMode = .character) throws -
Window overlap in characters (
.character) or word delta whenwordThresholdis omitted.Declaration
Swift
let chunkOverlap: Int -
Target window size in characters (
.character) or minimum split boundary in words (.word).Declaration
Swift
let chunkSize: Int -
The hash value.
Hash values are not guaranteed to be equal across different executions of your program. Do not save hash values to use during a future execution.
- Important:
hashValueis deprecated as aHashablerequirement. To conform toHashable, implement thehash(into:)requirement instead. The compiler provides an implementation forhashValuefor you.Declaration
Swift
var hashValue: Int { get } -
Partitioning granularity.
Declaration
Swift
let mode: ChunkingMode -
Maximum word count per chunk in
.wordmode (0in.charactermode).Declaration
Swift
let wordThreshold: Int -
Partitions
textinto ordered segments.Declaration
Swift
func chunk(text: String) throws -> [String]Parameters
textSource document text to partition.
Return Value
Ordered text chunks, or an empty array when
textcontains no non-whitespace text. -
Hashes the essential components of this value by feeding them into the given hasher.
Implement this method to conform to the
Hashableprotocol. The components used for hashing must be the same as the components compared in your type's==operator implementation. Callhasher.combine(_:)with each of these components.- Important: In your implementation of
hash(into:), don't callfinalize()on thehasherinstance provided, or replace it with a different instance. Doing so may become a compile-time error in the future.Declaration
Swift
func hash(into hasher: inout Hasher)Parameters
hasherThe hasher to use when combining the components of this instance.