splitMarkdownSections
Description
splits an input Markdown document chunkTable = splitMarkdownSections(str)str into sections, for example
according to the ATX section tags "# ", "## ", …,
"###### ".
splits a table of Markdown documents chunkTable = splitMarkdownSections(t)t into sections.
Examples
To split a Markdown-formatted document into sections, use splitMarkdownSections and specify the Markdown code as a string.
str = "# Title" + newline + ... "## Chapter 1" + newline + "Introductory paragraph of chapter 1." + newline + ... "### Section 1" + newline + "Content of section 1." + newline + ... "### Section 2" + newline + "Content of section 2."; chunkTable = splitMarkdownSections(str)
chunkTable=3×4 table
Text H1 H2 H3
______________________________________ _______ ___________ ___________
"Introductory paragraph of chapter 1." "Title" "Chapter 1" <missing>
"Content of section 1." "Title" "Chapter 1" "Section 1"
"Content of section 2." "Title" "Chapter 1" "Section 2"
To split multiple Markdown documents into chunks in a single chunk table, then first create a table of documents and then use splitMarkdownSections with the table of documents as the input. To retain metadata about the source documents, such as their filenames, add the metadata to the table as additional variables.
Create a table from:
A variable
Textthat contains Markdown documents.A variable
DocumentNamethat contains the names of the documents.
str1 = "# Title" + newline + ... "## Chapter 1" + newline + "Introductory paragraph of chapter 1." + newline + ... "### Section 1" + newline + "This is the first chapter of the first document."; str2 = "# Title" + newline + ... "## Chapter 1" + newline + "Introductory paragraph of chapter 1." + newline + ... "### Section 1" + newline + "This is the first chapter of the second document."; str3 = "# Title" + newline + ... "## Chapter 1" + newline + "Introductory paragraph of chapter 1." + newline + ... "### Section 1" + newline + "This is the first chapter of the third document."; Text = [str1;str2;str3]; DocumentName = ["Document 1";"Document 2";"Document 3"]; t = table(Text,DocumentName)
t=3×2 table
Text DocumentName
___________________________________________________________________________________________________________________________ ____________
"# Title↵## Chapter 1↵Introductory paragraph of chapter 1.↵### Section 1↵This is the first chapter of the first document." "Document 1"
"# Title↵## Chapter 1↵Introductory paragraph of chapter 1.↵### Section 1↵This is the first chapter of the second document." "Document 2"
"# Title↵## Chapter 1↵Introductory paragraph of chapter 1.↵### Section 1↵This is the first chapter of the third document." "Document 3"
Split the table of Markdown documents into sections using the splitMarkdownSections function.
chunkTable = splitMarkdownSections(t)
chunkTable=6×5 table
Text DocumentName H1 H2 H3
___________________________________________________ ____________ _______ ___________ ___________
"Introductory paragraph of chapter 1." "Document 1" "Title" "Chapter 1" <missing>
"This is the first chapter of the first document." "Document 1" "Title" "Chapter 1" "Section 1"
"Introductory paragraph of chapter 1." "Document 2" "Title" "Chapter 1" <missing>
"This is the first chapter of the second document." "Document 2" "Title" "Chapter 1" "Section 1"
"Introductory paragraph of chapter 1." "Document 3" "Title" "Chapter 1" <missing>
"This is the first chapter of the third document." "Document 3" "Title" "Chapter 1" "Section 1"
Input Arguments
Input document, specified as a string array, character vector, or cell array of character vectors.
Data Types: string | char | cell
Input table of documents. t must have a column named
Text that contains the documents. The documents must be specified
as a string scalar.
Output Arguments
Table of text chunks. chunkTable has these variables:
Text— Text chunk, returned as a string scalar.H1,H2, ...,H6— Title of nth-level Markdown section that contains the chunk, delineated in the Markdown code by#,##, ...,######. If the chunk is not part of an nth-level Markdown section, then the corresponding variableHncontains<missing>. If there are no nth-level Markdown sections in the input document, then no variableHnexists.
If you specify the input documents as a table, then chunkTable
also contains all the variables in the table. For each chunk, the values of the
variables are the same as for the document from which the chunk originates.
More About
Many analysis tools, including large language models (LLMs), perform better on small chunks of text than on large documents. Text Analytics Toolbox™ includes a range of functions that allow you to split large documents into semantically meaningful chunks.
The splitTextChunks function splits a document recursively into text chunks
of a given target length. The function first splits a document into paragraphs. If any
of the paragraphs are longer than the target length, then the function splits those
paragraphs into sentences, and so on.
chunks = splitTextChunks(str);
Split your document into sections and preserve the section metadata using one of these functions:
splitHTMLSectionsSplit an HTML-formatted document into HTML sections according to the section tags
<h1>...</h1>,<h2>...</h2>, …,<h6>...</h6>.splitMarkdownSectionsSplit a Markdown-formatted document into Markdown sections, for example according to ATX section tags #,##, …,######.splitCustomSectionsSplit a document into custom sections according to custom section delimiters. Split your documents or your chunks recursively into paragraphs, sentences, and tokens using the
splitTextChunksfunction.To avoid redundancy, join similar adjacent chunks using the
joinSimilarTextChunksfunction.Add overlap between adjacent text chunks using the
addTextChunkOverlapfunction. Adding text chunk overlap avoids changing the meaning of sentences by splitting at inopportune points, for example, splitting the sentence "I would never say I love cats" into "I would never say" and "I love cats." Adding overlap in this example results in the two chunks "I would never say I love" and "never say I love cats." You can also add surrounding text to individual chunks as context by using thefindTextChunkContextfunction.
For an example showing the advanced workflow, see Split Document Into Semantically Meaningful Text Chunks.
RAG combines the text generation capabilities of large language models (LLMs) with reliable information contained in a set of source documents. First, retrieve documents relevant to the user prompt from the set of source documents. Then, append the relevant document to the prompt and use the LLM to generate a response.
To improve the quality of the generated output, split large documents into smaller, semantically meaningful chunks.
Use information retrieval to identify the text chunks that are relevant to the query. For more information, see Information Retrieval with Document Embeddings.
Create a prompt based on the most relevant chunks. To provide the LLM with additional context, you can add text from adjacent prompts within the same section by using the
findTextChunkContextfunction, or you can you can add overlap between text chunks before information retrieval by using theaddTextChunkOverlapfunction. Create a Markdown-formatted string from the text chunks using theformatTextChunksfunction. For an example, see Create Large Language Model (LLM) Prompt from Text Chunk.Generate an answer using an LLM. To connect to large language model APIs using MATLAB, use the Large Language Models (LLMs) with MATLAB add-on.
References
Version History
Introduced in R2026a
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)