跪拜 Guibai
← All articles
JavaScript · Artificial Intelligence · Full Stack

The Recursive Splitter That Respects Your Sentences

By dzhd ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Chunking strategy is the silent killer of RAG accuracy. A splitter that ignores sentence boundaries produces fragments that retrieve poorly, while the recursive approach shown here keeps semantic units intact, directly improving the quality of answers a vector database can return.

Summary

Building a RAG pipeline starts with turning messy, heterogeneous data into clean, structured Document objects. LangChain's Loader ecosystem handles this with over 180 community adapters, but the real craft lies in what happens next: chunking. A naive character-count splitter destroys semantic coherence, but the RecursiveCharacterTextSplitter probes text with a hierarchy of separators — periods, question marks, then newlines — to find the most natural breakpoints. When a chunk still exceeds the target size, it recursively falls back to the next separator in the list, ensuring every piece is as close to the size limit as possible while keeping sentences whole.

A CheerioWebBaseLoader demonstrates the loading phase, using CSS selectors to extract only article body text from a raw HTML page, sidestepping the noise of navigation and sidebars. The splitter then takes over, configured with Chinese sentence-ending punctuation as the highest-priority separators and a 25% overlap between chunks. That overlap is the safety net: when a long sentence is inevitably broken, the duplicated text across chunk boundaries preserves the logical thread for retrieval.

The entire preprocessing chain — URL to HTTP response, DOM parsing, text extraction, recursive splitting — is a pipeline where every parameter is a trade-off. Chunk size, separator order, and overlap ratio directly control the recall-precision balance of the eventual vector search, making these seemingly mundane settings a core architectural decision.

Takeaways
LangChain's Document abstraction standardizes all ingested content into a pageContent string and a metadata object, regardless of the source format.
The CheerioWebBaseLoader uses server-side jQuery to apply CSS selectors against an HTML page, extracting only targeted text and discarding layout noise.
RecursiveCharacterTextSplitter works through a ranked separator list, attempting to split on periods first, then falling back to weaker separators only when a chunk still exceeds chunkSize.
A 10–25% chunkOverlap copies the tail of one chunk to the head of the next, preventing meaning from being lost at truncation boundaries during retrieval.
Chinese text chunking requires explicit sentence-ending punctuation (。!?) as the top-priority separators; the splitter's built-in fallback list is tuned for English whitespace and newlines.
Every parameter — chunkSize, separator list, overlap — is a tuning lever that trades off storage cost, retrieval latency, and semantic recall accuracy.
Conclusions

The real engineering in RAG preprocessing is not the loading step but the chunking strategy, where language-specific separator hierarchies determine whether retrieved fragments carry coherent meaning or broken half-thoughts.

Cheerio's CSS-selector approach to web scraping is more resilient than regex-based HTML parsing because it mirrors how the page's own styles target content, making extraction logic stable even when surrounding markup changes.

The article's choice to set only three Chinese punctuation marks as explicit separators relies on the splitter's internal fallback chain, a design that keeps configuration minimal but assumes the developer knows the default English-oriented fallback exists and will activate.

Concepts & terms
RecursiveCharacterTextSplitter
A LangChain text splitter that attempts to break text using a priority-ordered list of separators. It first tries the strongest separator (e.g., a period); if any resulting chunk is still larger than chunkSize, it recursively re-splits that chunk with the next separator in the list, preserving semantic boundaries wherever possible.
chunkOverlap
A parameter that duplicates a specified number of characters from the end of one text chunk into the beginning of the next. This overlap acts as a safety margin so that when a sentence is forcibly split at a chunk boundary, the surrounding context is still present in both chunks, improving retrieval accuracy.
CheerioWebBaseLoader
A LangChain document loader that fetches a URL, parses the HTML with the cheerio library (a server-side jQuery implementation), and uses a CSS selector to extract specific elements as plain-text Document objects.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗