How semantic splitting works
mainThe splitter aims to maximize chunk size while respecting semantic boundaries. It follows these steps:
- Identify Levels: It splits text by increasing semantic levels (e.g., from characters up to headings or syntax tree depth).
- Select Highest Level: It checks the first item of each level and selects the highest level whose first item still fits within the chunk size.
- Merge: It merges neighboring sections of that level (or higher) into a chunk to maximize length without crossing semantic boundaries.
Semantic Levels by Splitter Type
TextSplitter (Ascending order)
- Characters
- Unicode Grapheme Cluster Boundaries
- Unicode Word Boundaries
- Unicode Sentence Boundaries
- Newline sequences (e.g.,
\n\nis a higher level than\n)
MarkdownSplitter (Ascending order)
- Characters
- Unicode Grapheme Cluster Boundaries
- Unicode Word Boundaries
- Unicode Sentence Boundaries
- Soft line breaks (single newline)
- Inline elements (text, emphasis, links, etc.)
- Block elements (paragraphs, code blocks, metadata, etc.)
- Thematic breaks (horizontal rules)
- Headings by level
CodeSplitter (Ascending order)
- Characters
- Unicode Grapheme Cluster Boundaries
- Unicode Word Boundaries
- Unicode Sentence Boundaries
- Syntax tree depth (e.g., a function is a higher level than a statement inside it)
Note: Splitting never occurs below the character level to ensure valid Unicode strings.