赞
踩
Langchain提供了很多文本切割的工具,其中langchain默认使用RecursiveCharacterTextSplitter:
使用例子:
- from langchain.text_splitter import RecursiveCharacterTextSplitter, CharacterTextSplitter
-
- r_splitter = RecursiveCharacterTextSplitter(
- chunk_size=26, #chunk length
- chunk_overlap=4, #overlapping string length
- # separators=["\n\n", "\n", " ", ""] #Default separator characters
- # separators=["\n\n", "\n", "(?<=\. )", " ", ""] # 分割标识有以.结尾
-
- )
-
- #Split string
- text1 = 'abcdefghijklmnopqrstuvwxyz'
- r_splitter.split_text(text1)##
- c_splitter = CharacterTextSplitter(
- chunk_size=26,分割长度
- chunk_overlap=4, # 重复数字个数
- # separator = ' ' #分割字符
- )
-
- text3 = "a b c d e f g h i j k l m n o p q r s t u v w x y z"
- c_splitter.split_text(text3)
- from langchain.text_splitter import CharacterTextSplitter
-
- #创建分割器
- text_splitter = CharacterTextSplitter(
- separator="\n", # 设置单换行符作为分隔符
- chunk_size=1000, # 设置块的大小为1000个字符
- chunk_overlap=150, # 设置重叠字符为150个字符
- length_function=len # 长度函数设置为python的len函数
- )
-
- #分割文档
- docs = text_splitter.split_documents(pages)
- from langchain.text_splitter import TokenTextSplitter
-
- text_splitter = TokenTextSplitter(chunk_size=1, chunk_overlap=0)
-
- text1 = "foo bar bazzyfoo"
- text_splitter.split_text(text1)
-
- # 如果是文档,则下面分割 pages是打开的文档
- text_splitter = TokenTextSplitter(chunk_size=10, chunk_overlap=0)
-
- docs = text_splitter.split_documents(pages)
-
- len(docs)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。