> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-docs-zh.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 代码文本分割器集成指南

> 支持的语言存储在 langchaintextsplitters.Language 枚举中。它们包括：

@\[RecursiveCharacterTextSplitter] 包含了适用于 [分割文本](https://langchain-docs-zh.mintlify.site/langchain/oss/integrations/splitters.md) 的特定编程语言的预构建分隔符列表。

:::python
支持的语言存储在 `langchain_text_splitters.Language` 枚举中。它们包括：

```
"cpp",
"go",
"java",
"kotlin",
"js",
"ts",
"php",
"proto",
"python",
"rst",
"ruby",
"rust",
"scala",
"swift",
"markdown",
"latex",
"html",
"sol",
"csharp",
"cobol",
"c",
"lua",
"perl",
"haskell"
```

要查看给定语言的分隔符列表，将此枚举中的一个值传递给

```python theme={null}
RecursiveCharacterTextSplitter.get_separators_for_language
```

要实例化针对特定语言定制的分割器，将此枚举中的一个值传递给

```python theme={null}
RecursiveCharacterTextSplitter.from_language
```

:::

:::js
支持的语言保存在 `SupportedTextSplitterLanguages` 类型中。它们包括：

```
"cpp",
"go",
"java",
"js",
"php",
"proto",
"python",
"rst",
"ruby",
"rust",
"scala",
"swift",
"markdown",
"latex",
"html",
"sol",
```

要查看给定语言的分隔符列表，将此枚举中的一个值传递给

```ts theme={null}
RecursiveCharacterTextSplitter.getSeparatorsForLanguage()
```

要实例化针对特定语言定制的分割器，将此枚举中的一个值传递给

```ts theme={null}
RecursiveCharacterTextSplitter.fromLanguage()
```

:::

下面我们展示了各种语言的示例。

:::python

```python theme={null}
pip install -qU langchain-text-splitters
```

:::
:::js

```bash npm theme={null}
npm install @langchain/textsplitters
```

```bash pnpm theme={null}
pnpm install @langchain/textsplitters
```

```bash yarn theme={null}
yarn add @langchain/textsplitters
```

```bash bun theme={null}
bun add @langchain/textsplitters
```

:::

:::python

```python theme={null}
from langchain_text_splitters import (
    Language,
    RecursiveCharacterTextSplitter,
)
```

要查看支持的语言完整列表：

```python theme={null}
[e.value for e in Language]
```

```python theme={null}
['cpp',
 'go',
 'java',
 'kotlin',
 'js',
 'ts',
 'php',
 'proto',
 'python',
 'rst',
 'ruby',
 'rust',
 'scala',
 'swift',
 'markdown',
 'latex',
 'html',
 'sol',
 'csharp',
 'cobol',
 'c',
 'lua',
 'perl',
 'haskell',
 'elixir',
 'powershell',
 'visualbasic6']
```

您还可以查看给定语言使用的分隔符：

```python theme={null}
RecursiveCharacterTextSplitter.get_separators_for_language(Language.PYTHON)
```

```python theme={null}
['\nclass ', '\ndef ', '\n\tdef ', '\n\n', '\n', ' ', '']
```

:::

## Python

:::python

这里是一个使用 PythonTextSplitter 的示例：

```python theme={null}
PYTHON_CODE = """
def hello_world():
    print("Hello, World!")

# Call the function
hello_world()
"""
python_splitter = RecursiveCharacterTextSplitter.from_language(
    language=Language.PYTHON, chunk_size=50, chunk_overlap=0
)
python_docs = python_splitter.create_documents([PYTHON_CODE])
python_docs
```

```text theme={null}
[Document(metadata={}, page_content='def hello_world():\n    print("Hello, World!")'),
 Document(metadata={}, page_content='# Call the function\nhello_world()')]
```

:::
:::js
这里是一个使用 python 文本分割器的示例：

```ts theme={null}
const pythonSplitter = RecursiveCharacterTextSplitter.fromLanguage(
    "python",
    { chunkSize: 50, chunkOverlap: 0 }
);
const pythonDocs = pythonSplitter.createDocuments([{ pageContent: PYTHON_CODE }]);
console.log(pythonDocs);
```

```javascript theme={null}
[
    Document { metadata: {}, pageContent: 'def hello_world():\n    print("Hello, World!")' },
    Document { metadata: {}, pageContent: '# Call the function\nhello_world()' }
]
```

:::

## JS

:::python
这里是一个使用 JS 文本分割器的示例：

```python theme={null}
JS_CODE = """
function helloWorld() {
  console.log("Hello, World!");
}

// Call the function
helloWorld();
"""

js_splitter = RecursiveCharacterTextSplitter.from_language(
    language=Language.JS, chunk_size=60, chunk_overlap=0
)
js_docs = js_splitter.create_documents([JS_CODE])
js_docs
```

```javascript theme={null}
[Document(metadata={}, page_content='function helloWorld() {\n  console.log("Hello, World!");\n}'),
 Document(metadata={}, page_content='// Call the function\nhelloWorld();')]
```

:::
:::js
这里是一个使用 JS 文本分割器的示例：

```ts theme={null}
const JS_CODE = `
function helloWorld() {
  console.log("Hello, World!");
}

// Call the function
helloWorld();
`;

const jsSplitter = RecursiveCharacterTextSplitter.fromLanguage(
    "js",
    { chunkSize: 60, chunkOverlap: 0 }
);
const jsDocs = jsSplitter.createDocuments([{ pageContent: JS_CODE }]);
console.log(jsDocs);
```

```javascript theme={null}
[
    Document { metadata: {}, pageContent: 'function helloWorld() {\n  console.log("Hello, World!");\n}' },
    Document { metadata: {}, pageContent: '// Call the function\nhelloWorld()' }
]
```

:::

## TS

:::python
这里是一个使用 typescript 文本分割器的示例：

```python theme={null}
TS_CODE = """
function helloWorld(): void {
  console.log("Hello, World!");
}

// Call the function
helloWorld();
"""

ts_splitter = RecursiveCharacterTextSplitter.from_language(
    language=Language.TS, chunk_size=60, chunk_overlap=0
)
ts_docs = ts_splitter.create_documents([TS_CODE])
ts_docs
```

```javascript theme={null}
[Document(metadata={}, page_content='function helloWorld(): void {'),
 Document(metadata={}, page_content='console.log("Hello, World!");\n}'),
 Document(metadata={}, page_content='// Call the function\nhelloWorld();')]
```

:::
:::js
这里是一个使用 typescript 文本分割器的示例：

```ts theme={null}
const TS_CODE = `
function helloWorld(): void {
  console.log("Hello, World!");
}

// Call the function
helloWorld();
`;

const tsSplitter = RecursiveCharacterTextSplitter.fromLanguage(
    "ts",
    { chunkSize: 60, chunkOverlap: 0 }
);
const tsDocs = tsSplitter.createDocuments([{ pageContent: TS_CODE }]);
console.log(tsDocs);
```

```javascript theme={null}
[
    Document { metadata: {}, pageContent: 'function helloWorld(): void {' },
    Document { metadata: {}, pageContent: 'console.log("Hello, World!");\n}' },
    Document { metadata: {}, pageContent: '// Call the function\nhelloWorld()' }
]
```

:::

## Markdown

:::python
这里是一个使用 Markdown 文本分割器的示例：

```python theme={null}
markdown_text = """
# 🦜️🔗 LangChain

⚡ Building applications with LLMs through composability ⚡

## What is LangChain?

# Hopefully this code block isn't split
LangChain is a framework for...

As an open-source project in a rapidly developing field, we are extremely open to contributions.
"""
```

```python theme={null}
md_splitter = RecursiveCharacterTextSplitter.from_language(
    language=Language.MARKDOWN, chunk_size=60, chunk_overlap=0
)
md_docs = md_splitter.create_documents([markdown_text])
md_docs
```

```text theme={null}
[Document(metadata={}, page_content='# 🦜️🔗 LangChain'),
 Document(metadata={}, page_content='⚡ Building applications with LLMs through composability ⚡'),
 Document(metadata={}, page_content='## What is LangChain?'),
 Document(metadata={}, page_content="# Hopefully this code block isn't split"),
 Document(metadata={}, page_content='LangChain is a framework for...'),
 Document(metadata={}, page_content='As an open-source project in a rapidly developing field, we'),
 Document(metadata={}, page_content='are extremely open to contributions.')]
```

:::
:::js
这里是一个使用 Markdown 文本分割器的示例：

```ts theme={null}
const markdownText = `
# 🦜️🔗 LangChain

⚡ Building applications with LLMs through composability ⚡

## What is LangChain?

# Hopefully this code block isn't split
LangChain is a framework for...

As an open-source project in a rapidly developing field, we are extremely open to contributions.
`;

const mdSplitter = RecursiveCharacterTextSplitter.fromLanguage(
    "markdown",
    { chunkSize: 60, chunkOverlap: 0 }
);
const mdDocs = mdSplitter.createDocuments([ markdownText ]);
console.log(mdDocs);
```

```javascript theme={null}
[
    Document { metadata: {}, pageContent: '# 🦜️🔗 LangChain' },
    Document { metadata: {}, pageContent: '⚡ Building applications with LLMs through composability ⚡' },
    Document { metadata: {}, pageContent: '## What is LangChain?' },
    Document { metadata: {}, pageContent: "# Hopefully this code block isn't split" },
    Document { metadata: {}, pageContent: 'LangChain is a framework for...' },
    Document { metadata: {}, pageContent: 'As an open-source project in a rapidly developing field, we' },
    Document { metadata: {}, pageContent: 'are extremely open to contributions.' }
]
```

:::

## Latex

:::python
这里是一个关于 Latex 文本的示例：

```python theme={null}
latex_text = """
\documentclass{article}

\begin{document}

\maketitle

\section{Introduction}
Large language models (LLMs) are a type of machine learning model that can be trained on vast amounts of text data to generate human-like language. In recent years, LLMs have made significant advances in a variety of natural language processing tasks, including language translation, text generation, and sentiment analysis.

\subsection{History of LLMs}
The earliest LLMs were developed in the 1980s and 1990s, but they were limited by the amount of data that could be processed and the computational power available at the time. In the past decade, however, advances in hardware and software have made it possible to train LLMs on massive datasets, leading to significant improvements in performance.

\subsection{Applications of LLMs}
LLMs have many applications in industry, including chatbots, content creation, and virtual assistants. They can also be used in academia for research in linguistics, psychology, and computational linguistics.

\end{document}
"""
```

```python theme={null}
latex_splitter = RecursiveCharacterTextSplitter.from_language(
    language=Language.MARKDOWN, chunk_size=60, chunk_overlap=0
)
latex_docs = latex_splitter.create_documents([latex_text])
latex_docs
```

```text theme={null}
[Document(metadata={}, page_content='\\documentclass{article}\n\n\x08egin{document}\n\n\\maketitle'),
 Document(metadata={}, page_content='\\section{Introduction}'),
 Document(metadata={}, page_content='Large language models (LLMs) are a type of machine learning'),
 Document(metadata={}, page_content='model that can be trained on vast amounts of text data to'),
 Document(metadata={}, page_content='generate human-like language. In recent years, LLMs have'),
 Document(metadata={}, page_content='made significant advances in a variety of natural language'),
 Document(metadata={}, page_content='processing tasks, including language translation, text'),
 Document(metadata={}, page_content='generation, and sentiment analysis.'),
 Document(metadata={}, page_content='\\subsection{History of LLMs}'),
 Document(metadata={}, page_content='The earliest LLMs were developed in the 1980s and 1990s,'),
 Document(metadata={}, page_content='but they were limited by the amount of data that could be'),
 Document(metadata={}, page_content='processed and the computational power available at the'),
 Document(metadata={}, page_content='time. In the past decade, however, advances in hardware and'),
 Document(metadata={}, page_content='software have made it possible to train LLMs on massive'),
 Document(metadata={}, page_content='datasets, leading to significant improvements in'),
 Document(metadata={}, page_content='performance.'),
 Document(metadata={}, page_content='\\subsection{Applications of LLMs}'),
 Document(metadata={}, page_content='LLMs have many applications in industry, including'),
 Document(metadata={}, page_content='chatbots, content creation, and virtual assistants. They'),
 Document(metadata={}, page_content='can also be used in academia for research in linguistics,'),
 Document(metadata={}, page_content='psychology, and computational linguistics.'),
 Document(metadata={}, page_content='\\end{document}')]
```

:::
:::js
这里是一个关于 Latex 文本的示例：

```ts theme={null}
const latexText = `
\\documentclass{article}

\\begin{document}

\\maketitle

\\section{Introduction}
Large language models (LLMs) are a type of machine learning model that can be trained on vast amounts of text data to generate human-like language. In recent years, LLMs have made significant advances in a variety of natural language processing tasks, including language translation, text generation, and sentiment analysis.

\\subsection{History of LLMs}
The earliest LLMs were developed in the 1980s and 1990s, but they were limited by the amount of data that could be processed and the computational power available at the time. In the past decade, however, advances in hardware and software have made it possible to train LLMs on massive datasets, leading to significant improvements in performance.

\\subsection{Applications of LLMs}
LLMs have many applications in industry, including chatbots, content creation, and virtual assistants. They can also be used in academia for research in linguistics, psychology, and computational linguistics.

\\end{document}
`;

const latexSplitter = RecursiveCharacterTextSplitter.fromLanguage(
    "latex",
    { chunkSize: 60, chunkOverlap: 0 }
);
const latexDocs = latexSplitter.createDocuments([{ pageContent: latexText }]);
console.log(latexDocs);
```

```javascript theme={null}
[
    Document { metadata: {}, pageContent: '\\documentclass{article}\n\n\\begin{document}\n\n\\maketitle' },
    Document { metadata: {}, pageContent: '\\section{Introduction}' },
    Document { metadata: {}, pageContent: 'Large language models (LLMs) are a type of machine learning' },
    Document { metadata: {}, pageContent: 'model that can be trained on vast amounts of text data to' },
    Document { metadata: {}, pageContent: 'generate human-like language. In recent years, LLMs have' },
    Document { metadata: {}, pageContent: 'made significant advances in a variety of natural language' },
    Document { metadata: {}, pageContent: 'processing tasks, including language translation, text' },
    Document { metadata: {}, pageContent: 'generation, and sentiment analysis.' },
    Document { metadata: {}, pageContent: '\\subsection{History of LLMs}' },
    Document { metadata: {}, pageContent: 'The earliest LLMs were developed in the 1980s and 1990s,' },
    Document { metadata: {}, pageContent: 'but they were limited by the amount of data that could be' },
    Document { metadata: {}, pageContent: 'processed and the computational power available at the' },
    Document { metadata: {}, pageContent: 'time. In the past decade, however, advances in hardware and' },
    Document { metadata: {}, pageContent: 'software have made it possible to train LLMs on massive' },
    Document { metadata: {}, pageContent: 'datasets, leading to significant improvements in' },
    Document { metadata: {}, pageContent: 'performance.' },
    Document { metadata: {}, pageContent: '\\subsection{Applications of LLMs}' },
    Document { metadata: {}, pageContent: 'LLMs have many applications in industry, including' },
    Document { metadata: {}, pageContent: 'chatbots, content creation, and virtual assistants. They' },
    Document { metadata: {}, pageContent: 'can also be used in academia for research in linguistics,' },
    Document { metadata: {}, pageContent: 'psychology, and computational linguistics.' },
    Document { metadata: {}, pageContent: '\\end{document}' }
]
```

:::

## HTML

:::python
这里是一个使用 HTML 文本分割器的示例：

```python theme={null}
html_text = """
<!DOCTYPE html>
<html>
    <head>
        <title>🦜️🔗 LangChain</title>
        <style>
            body {
                font-family: Arial, sans-serif;
            }
            h1 {
                color: darkblue;
            }
        </style>
    </head>
    <body>
        <div>
            <h1>🦜️🔗 LangChain</h1>
            <p>⚡ Building applications with LLMs through composability ⚡</p>
        </div>
        <div>
            As an open-source project in a rapidly developing field, we are extremely open to contributions.
        </div>
    </body>
</html>
"""
```

```python theme={null}
html_splitter = RecursiveCharacterTextSplitter.from_language(
    language=Language.HTML, chunk_size=60, chunk_overlap=0
)
html_docs = html_splitter.create_documents([html_text])
html_docs
```

```text theme={null}
[Document(metadata={}, page_content='<!DOCTYPE html>\n<html>'),
 Document(metadata={}, page_content='<head>\n        <title>🦜️🔗 LangChain</title>'),
 Document(metadata={}, page_content='<style>\n            body {\n                font-family: Aria'),
 Document(metadata={}, page_content='l, sans-serif;\n            }\n            h1 {'),
 Document(metadata={}, page_content='color: darkblue;\n            }\n        </style>\n    </head'),
 Document(metadata={}, page_content='>'),
 Document(metadata={}, page_content='<body>'),
 Document(metadata={}, page_content='<div>\n            <h1>🦜️🔗 LangChain</h1>'),
 Document(metadata={}, page_content='<p>⚡ Building applications with LLMs through composability ⚡'),
 Document(metadata={}, page_content='</p>\n        </div>'),
 Document(metadata={}, page_content='<div>\n            As an open-source project in a rapidly dev'),
 Document(metadata={}, page_content='eloping field, we are extremely open to contributions.'),
 Document(metadata={}, page_content='</div>\n    </body>\n</html>')]
```

:::
:::js
这里是一个使用 HTML 文本分割器的示例：

```ts theme={null}
const htmlText = `
<!DOCTYPE html>
<html>
    <head>
        <title>🦜️🔗 LangChain</title>
        <style>
            body {
                font-family: Arial, sans-serif;
            }
            h1 {
                color: darkblue;
            }
        </style>
    </head>
    <body>
        <div>
            <h1>🦜️🔗 LangChain</h1>
            <p>⚡ Building applications with LLMs through composability ⚡</p>
        </div>
        <div>
            As an open-source project in a rapidly developing field, we are extremely open to contributions.
        </div>
    </body>
</html>
`;

const htmlSplitter = RecursiveCharacterTextSplitter.fromLanguage(
    "html",
    { chunkSize: 60, chunkOverlap: 0 }
);
const htmlDocs = htmlSplitter.createDocuments([{ pageContent: htmlText }]);
console.log(htmlDocs);
```

```javascript theme={null}
[
    Document { metadata: {}, pageContent: '<!DOCTYPE html>\n<html>' },
    Document { metadata: {}, pageContent: '<head>\n        <title>🦜️🔗 LangChain</title>' },
    Document { metadata: {}, pageContent: '<style>\n            body {\n                font-family: Aria' },
    Document { metadata: {}, pageContent: 'l, sans-serif;\n            }\n            h1 {' },
    Document { metadata: {}, pageContent: 'color: darkblue;\n            }\n        </style>\n    </head' },
    Document { metadata: {}, pageContent: '>' },
    Document { metadata: {}, pageContent: '<body>' },
    Document { metadata: {}, pageContent: '<div>\n            <h1>🦜️🔗 LangChain</h1>' },
    Document { metadata: {}, pageContent: '<p>⚡ Building applications with LLMs through composability ⚡' },
    Document { metadata: {}, pageContent: '</p>\n        </div>' },
    Document { metadata: {}, pageContent: '<div>\n            As an open-source project in a rapidly dev' },
    Document { metadata: {}, pageContent: 'eloping field, we are extremely open to contributions.' },
    Document { metadata: {}, pageContent: '</div>\n    </body>\n</html>' }
]
```

:::

## Solidity

:::python
这里是一个使用 Solidity 文本分割器的示例：

```python theme={null}
SOL_CODE = """
pragma solidity ^0.8.20;
contract HelloWorld {
   function add(uint a, uint b) pure public returns(uint) {
       return a + b;
   }
}
"""

sol_splitter = RecursiveCharacterTextSplitter.from_language(
    language=Language.SOL, chunk_size=128, chunk_overlap=0
)
sol_docs = sol_splitter.create_documents([SOL_CODE])
sol_docs
```

```text theme={null}
[Document(metadata={}, page_content='pragma solidity ^0.8.20;'),
 Document(metadata={}, page_content='contract HelloWorld {\n   function add(uint a, uint b) pure public returns(uint) {\n       return a + b;\n   }\n}')]
```

:::
:::js
这里是一个使用 Solidity 文本分割器的示例：

```ts theme={null}
const SOL_CODE = `
pragma solidity ^0.8.20;
contract HelloWorld {
   function add(uint a, uint b) pure public returns(uint) {
       return a + b;
   }
}
`;

const solSplitter = RecursiveCharacterTextSplitter.fromLanguage(
    "sol",
    { chunkSize: 128, chunkOverlap: 0 }
);
const solDocs = solSplitter.createDocuments([{ pageContent: SOL_CODE }]);
console.log(solDocs);
```

```javascript theme={null}
[
    Document { metadata: {}, pageContent: 'pragma solidity ^0.8.20;' },
    Document { metadata: {}, pageContent: 'contract HelloWorld {\n   function add(uint a, uint b) pure public returns(uint) {\n       return a + b;\n   }\n}' }
]
```

:::

## C\#

:::python
这里是一个使用 C# 文本分割器的示例：

```python theme={null}
C_CODE = """
using System;
class Program
{
    static void Main()
    {
        int age = 30; // Change the age value as needed

        // Categorize the age without any console output
        if (age < 18)
        {
            // Age is under 18
        }
        else if (age >= 18 && age < 65)
        {
            // Age is an adult
        }
        else
        {
            // Age is a senior citizen
        }
    }
}
"""
c_splitter = RecursiveCharacterTextSplitter.from_language(
    language=Language.CSHARP, chunk_size=128, chunk_overlap=0
)
c_docs = c_splitter.create_documents([C_CODE])
c_docs
```

```text theme={null}
[Document(metadata={}, page_content='using System;'),
 Document(metadata={}, page_content='class Program\n{\n    static void Main()\n    {\n        int age = 30; // Change the age value as needed'),
 Document(metadata={}, page_content='// Categorize the age without any console output\n        if (age < 18)\n        {\n            // Age is under 18'),
 Document(metadata={}, page_content='}\n        else if (age >= 18 && age < 65)\n        {\n            // Age is an adult\n        }\n        else\n        {'),
 Document(metadata={}, page_content='// Age is a senior citizen\n        }\n    }\n}')]
```

:::
:::js
这里是一个使用 C# 文本分割器的示例：

```ts theme={null}
const C_CODE = `
using System;
class Program
{
    static void Main()
    {
        int age = 30; // Change the age value as needed

        // Categorize the age without any console output
        if (age < 18)
        {
            // Age is under 18
        }
        else if (age >= 18 && age < 65)
        {
            // Age is an adult
        }
        else
        {
            // Age is a senior citizen
        }
    }
}
`;

const csharpSplitter = RecursiveCharacterTextSplitter.fromLanguage(
    "csharp",
    { chunkSize: 128, chunkOverlap: 0 }
);
const csharpDocs = csharpSplitter.createDocuments([{ pageContent: C_CODE }]);
console.log(csharpDocs);
```

```javascript theme={null}
[
    Document { metadata: {}, pageContent: 'using System;' },
    Document { metadata: {}, pageContent: 'class Program\n{\n    static void Main()\n    {\n        int age = 30; // Change the age value as needed' },
    Document { metadata: {}, pageContent: '// Categorize the age without any console output\n        if (age < 18)\n        {\n            // Age is under 18' },
    Document { metadata: {}, pageContent: '}\n        else if (age >= 18 && age < 65)\n        {\n            // Age is an adult\n        }\n        else\n        {' },
    Document { metadata: {}, pageContent: '// Age is a senior citizen\n        }\n    }\n}' }
]
```

:::

## Haskell

:::python
这里是一个使用 Haskell 文本分割器的示例：

```python theme={null}
HASKELL_CODE = """
main :: IO ()
main = do
    putStrLn "Hello, World!"
-- Some sample functions
add :: Int -> Int -> Int
add x y = x + y
"""
haskell_splitter = RecursiveCharacterTextSplitter.from_language(
    language=Language.HASKELL, chunk_size=50, chunk_overlap=0
)
haskell_docs = haskell_splitter.create_documents([HASKELL_CODE])
haskell_docs
```

```text theme={null}
[Document(metadata={}, page_content='main :: IO ()'),
 Document(metadata={}, page_content='main = do\n    putStrLn "Hello, World!"\n-- Some'),
 Document(metadata={}, page_content='sample functions\nadd :: Int -> Int -> Int\nadd x y'),
 Document(metadata={}, page_content='= x + y')]
```

:::
:::js
这里是一个使用 Haskell 文本分割器的示例：

```ts theme={null}
const HASKELL_CODE = `
main :: IO ()
main = do
    putStrLn "Hello, World!"
-- Some sample functions
add :: Int -> Int -> Int
add x y = x + y
`;

const haskellSplitter = RecursiveCharacterTextSplitter.fromLanguage(
    "haskell",
    { chunkSize: 50, chunkOverlap: 0 }
);
const haskellDocs = haskellSplitter.createDocuments([{ pageContent: HASKELL_CODE }]);
console.log(haskellDocs);
```

```javascript theme={null}
[
    Document { metadata: {}, pageContent: 'main :: IO ()' },
    Document { metadata: {}, pageContent: 'main = do\n    putStrLn "Hello, World!"\n-- Some' },
    Document { metadata: {}, pageContent: 'sample functions\nadd :: Int -> Int -> Int\nadd x y' },
    Document { metadata: {}, pageContent: '= x + y' }
]
```

:::

## PHP

:::python
这里是一个使用 PHP 文本分割器的示例：

```python theme={null}
PHP_CODE = """<?php
namespace foo;
class Hello {
    public function __construct() { }
}
function hello() {
    echo "Hello World!";
}
interface Human {
    public function breath();
}
trait Foo { }
enum Color
{
    case Red;
    case Blue;
}"""
php_splitter = RecursiveCharacterTextSplitter.from_language(
    language=Language.PHP, chunk_size=50, chunk_overlap=0
)
php_docs = php_splitter.create_documents([PHP_CODE])
php_docs
```

```text theme={null}
[Document(metadata={}, page_content='<?php\nnamespace foo;'),
 Document(metadata={}, page_content='class Hello {'),
 Document(metadata={}, page_content='public function __construct() { }\n}'),
 Document(metadata={}, page_content='function hello() {\n    echo "Hello World!";\n}'),
 Document(metadata={}, page_content='interface Human {\n    public function breath();\n}'),
 Document(metadata={}, page_content='trait Foo { }\nenum Color\n{\n    case Red;'),
 Document(metadata={}, page_content='case Blue;\n}')]
```

:::
:::js
这里是一个使用 PHP 文本分割器的示例：

```ts theme={null}
const PHP_CODE = `<?php
namespace foo;
class Hello {
    public function __construct() { }
}
function hello() {
    echo "Hello World!";
}
interface Human {
    public function breath();
}
trait Foo { }
enum Color
{
    case Red;
    case Blue;
}`;

const phpSplitter = RecursiveCharacterTextSplitter.fromLanguage(
    "php",
    { chunkSize: 50, chunkOverlap: 0 }
);
const phpDocs = phpSplitter.createDocuments([{ pageContent: PHP_CODE }]);
console.log(phpDocs);
```

```javascript theme={null}
[
    Document { metadata: {}, pageContent: '<?php\nnamespace foo;' },
    Document { metadata: {}, pageContent: 'class Hello {' },
    Document { metadata: {}, pageContent: 'public function __construct() { }\n}' },
    Document { metadata: {}, pageContent: 'function hello() {\n    echo "Hello World!";\n}' },
    Document { metadata: {}, pageContent: 'interface Human {\n    public function breath();\n}' },
    Document { metadata: {}, pageContent: 'trait Foo { }\nenum Color\n{\n    case Red;' },
    Document { metadata: {}, pageContent: 'case Blue;\n}' }
]
```

:::

## PowerShell

:::python
这里是一个使用 PowerShell 文本分割器的示例：

```python theme={null}
POWERSHELL_CODE = """
$directoryPath = Get-Location

$items = Get-ChildItem -Path $directoryPath

$files = $items | Where-Object { -not $_.PSIsContainer }

$sortedFiles = $files | Sort-Object LastWriteTime

foreach ($file in $sortedFiles) {
    Write-Output ("Name: " + $file.Name + " | Last Write Time: " + $file.LastWriteTime)
}
"""
powershell_splitter = RecursiveCharacterTextSplitter.from_language(
    language=Language.POWERSHELL, chunk_size=100, chunk_overlap=0
)
powershell_docs = powershell_splitter.create_documents([POWERSHELL_CODE])
powershell_docs
```

```text theme={null}
[Document(metadata={}, page_content='$directoryPath = Get-Location\n\n$items = Get-ChildItem -Path $directoryPath'),
 Document(metadata={}, page_content='$files = $items | Where-Object { -not $_.PSIsContainer }'),
 Document(metadata={}, page_content='$sortedFiles = $files | Sort-Object LastWriteTime'),
 Document(metadata={}, page_content='foreach ($file in $sortedFiles) {'),
 Document(metadata={}, page_content='Write-Output ("Name: " + $file.Name + " | Last Write Time: " + $file.LastWriteTime)\n}')]
```

:::
:::js
这里是一个使用 PowerShell 文本分割器的示例：

```ts theme={null}
const POWERSHELL_CODE = `
$directoryPath = Get-Location

$items = Get-ChildItem -Path $directoryPath

$files = $items | Where-Object { -not $_.PSIsContainer }

$sortedFiles = $files | Sort-Object LastWriteTime

foreach ($file in $sortedFiles) {
    Write-Output ("Name: " + $file.Name + " | Last Write Time: " + $file.LastWriteTime)
}
`;

const powershellSplitter = RecursiveCharacterTextSplitter.fromLanguage(
    "powershell",
    { chunkSize: 100, chunkOverlap: 0 }
);
const powershellDocs = powershellSplitter.createDocuments([{ pageContent: POWERSHELL_CODE }]);
console.log(powershellDocs);
```

```javascript theme={null}
[
    Document { metadata: {}, pageContent: '$directoryPath = Get-Location\n\n$items = Get-ChildItem -Path $directoryPath' },
    Document { metadata: {}, pageContent: '$files = $items | Where-Object { -not $_.PSIsContainer }' },
    Document { metadata: {}, pageContent: '$sortedFiles = $files | Sort-Object LastWriteTime' },
    Document { metadata: {}, pageContent: 'foreach ($file in $sortedFiles) {' },
    Document { metadata: {}, pageContent: 'Write-Output ("Name: " + $file.Name + " | Last Write Time: " + $file.LastWriteTime)\n}' }
]
```

:::

## Visual basic 6

:::python

```python theme={null}
VISUALBASIC6_CODE = """Option Explicit

Public Sub HelloWorld()
    MsgBox "Hello, World!"
End Sub

Private Function Add(a As Integer, b As Integer) As Integer
    Add = a + b
End Function
"""
visualbasic6_splitter = RecursiveCharacterTextSplitter.from_language(
    Language.VISUALBASIC6,
    chunk_size=128,
    chunk_overlap=0,
)
visualbasic6_docs = visualbasic6_splitter.create_documents([VISUALBASIC6_CODE])
visualbasic6_docs
```

```text theme={null}
[Document(metadata={}, page_content='Option Explicit'),
 Document(metadata={}, page_content='Public Sub HelloWorld()\n    MsgBox "Hello, World!"\nEnd Sub'),
 Document(metadata={}, page_content='Private Function Add(a As Integer, b As Integer) As Integer\n    Add = a + b\nEnd Function')]
```

:::
:::js

```ts theme={null}
const VISUALBASIC6_CODE = `Option Explicit

Public Sub HelloWorld()
    MsgBox "Hello, World!"
End Sub

Private Function Add(a As Integer, b As Integer) As Integer
    Add = a + b
End Function
`;

const visualbasic6Splitter = RecursiveCharacterTextSplitter.fromLanguage(
    "visualbasic6",
    { chunkSize: 128, chunkOverlap: 0 }
);
const visualbasic6Docs = visualbasic6Splitter.createDocuments([{ pageContent: VISUALBASIC6_CODE }]);
console.log(visualbasic6Docs);
```

```javascript theme={null}
[
    Document { metadata: {}, pageContent: 'Option Explicit' },
    Document { metadata: {}, pageContent: 'Public Sub HelloWorld()\n    MsgBox "Hello, World!"\nEnd Sub' },
    Document { metadata: {}, pageContent: 'Private Function Add(a As Integer, b As Integer) As Integer\n    Add = a + b\nEnd Function' }
]
```

:::

***

> 官方原文：[src/oss/integrations/splitters/code\_splitter.mdx](https://github.com/langchain-ai/docs/blob/main/src/oss/integrations/splitters/code_splitter.mdx) · 非官方翻译，译文由 AI 生成，以英文原文为准。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.