문자 단위 텍스트 분할
문자 단위 텍스트 분할 (CharacterTextSplitter)
텍스트 분할에서 가장 단순한 접근 방식이 문자 단위 분할이에요. 지정한 문자 시퀀스(기본값 "\n\n")를 기준으로 텍스트를 나누고, 청크 길이는 문자 수로 측정해요. 개념이 직관적이라 처음 분할기를 접할 때 이해하기 쉬워요. 다만 재귀 분할기만큼 문맥을 지키려고 하지는 않으니, 문단 단위처럼 경계가 명확한 텍스트를 다룰 때 주로 어울려요.
출처: 공식문서
분할 방식
핵심 포인트 두 가지를 기억하면 돼요.
- 텍스트를 어떻게 나누나: 주어진 문자 구분자 기준.
- 청크 크기를 어떻게 재나: 문자 수 기준.
설치
Python
pip install -qU langchain-text-splitters
JavaScript
npm install @langchain/textsplitters
pnpm install @langchain/textsplitters
yarn add @langchain/textsplitters
bun add @langchain/textsplitters
사용 방법
결과 형태에 따라 두 메서드를 고를 수 있어요.
.split_text: 단순한 문자열 청크를 반환해요..create_documents:Document객체를 반환해요. 이후 다운스트림 작업에 메타데이터를 보존해야 할 때 유용하죠.
from langchain_text_splitters import CharacterTextSplitter
# Load an example document
with open("state_of_the_union.txt") as f:
state_of_the_union = f.read()
text_splitter = CharacterTextSplitter(
separator="\n\n",
chunk_size=1000,
chunk_overlap=200,
length_function=len,
is_separator_regex=False,
)
texts = text_splitter.create_documents([state_of_the_union])
print(texts[0])
page_content='Madam Speaker, Madam Vice President, our First Lady and Second Gentleman. Members of Congress and the Cabinet. Justices of the Supreme Court. My fellow Americans. \n\nLast year COVID-19 kept us apart. This year we are finally together again. \n\nTonight, we meet as Democrats Republicans and Independents. But most importantly as Americans. \n\nWith a duty to one another to the American people to the Constitution. \n\nAnd with an unwavering resolve that freedom will always triumph over tyranny. \n\nSix days ago, Russia’s Vladimir Putin sought to shake the foundations of the free world thinking he could make it bend to his menacing ways. But he badly miscalculated. \n\nHe thought he could roll into Ukraine and the world would roll over. Instead he met a wall of strength he never imagined. \n\nHe met the Ukrainian people. \n\nFrom President Zelenskyy to every Ukrainian, their fearlessness, their courage, their determination, inspires the world.'
JavaScript 예시
import { CharacterTextSplitter } from "@langchain/textsplitters";
import { readFileSync } from "fs";
// Example: read a long document
const stateOfTheUnion = readFileSync("state_of_the_union.txt", "utf8");
const splitter = new CharacterTextSplitter({
separator: "\n\n",
chunkSize: 1000,
chunkOverlap: 200,
});
const texts = splitter.createDocuments([{ pageContent: stateOfTheUnion }]);
console.log(texts[0]);
Document {
pageContent: 'Madam Speaker, Madam Vice President, our First Lady and Second Gentleman. Members of Congress and the Cabinet. Justices of the Supreme Court. My fellow Americans. \n\nLast year COVID-19 kept us apart. This year we are finally together again. \n\nTonight, we meet as Democrats Republicans and Independents. But most importantly as Americans. \n\nWith a duty to one another to the American people to the Constitution. \n\nAnd with an unwavering resolve that freedom will always triumph over tyranny. \n\nSix days ago, Russia’s Vladimir Putin sought to shake the foundations of the free world thinking he could make it bend to his menacing ways. But he badly miscalculated. \n\nHe thought he could roll into Ukraine and the world would roll over. Instead he met a wall of strength he never imagined. \n\nHe met the Ukrainian people. \n\nFrom President Zelenskyy to every Ukrainian, their fearlessness, their courage, their determination, inspires the world.'
}
메타데이터 전파하기
.create_documents를 쓰면 각 문서에 달린 메타데이터를 출력 청크로 함께 전달해요. 검색했을 때 출처 정보 같은 걸 청크 단위로 유지하고 싶을 때 유용해요.
metadatas = [{"document": 1}, {"document": 2}]
documents = text_splitter.create_documents(
[state_of_the_union, state_of_the_union], metadatas=metadatas
)
print(documents[0])
page_content='Madam Speaker, Madam Vice President, our First Lady and Second Gentleman. Members of Congress and the Cabinet. Justices of the Supreme Court. My fellow Americans. \n\nLast year COVID-19 kept us apart. This year we are finally together again. \n\nTonight, we meet as Democrats Republicans and Independents. But most importantly as Americans. \n\nWith a duty to one another to the American people to the Constitution. \n\nAnd with an unwavering resolve that freedom will always triumph over tyranny. \n\nSix days ago, Russia’s Vladimir Putin sought to shake the foundations of the free world thinking he could make it bend to his menacing ways. But he badly miscalculated. \n\nHe thought he could roll into Ukraine and the world would roll over. Instead he met a wall of strength he never imagined. \n\nHe met the Ukrainian people. \n\nFrom President Zelenskyy to every Ukrainian, their fearlessness, their courage, their determination, inspires the world.' metadata={'document': 1}
JavaScript 예시
const metadatas = [{"document": 1}, {"document": 2}]
const documents = splitter.createDocuments(
[{ pageContent: stateOfTheUnion }, { pageContent: stateOfTheUnion }],
{ metadatas: metadatas }
);
console.log(documents[0]);
Document {
pageContent: 'Madam Speaker, Madam Vice President, our First Lady and Second Gentleman. Members of Congress and the Cabinet. Justices of the Supreme Court. My fellow Americans. \n\nLast year COVID-19 kept us apart. This year we are finally together again. \n\nTonight, we meet as Democrats Republicans and Independents. But most importantly as Americans. \n\nWith a duty to one another to the American people to the Constitution. \n\nAnd with an unwavering resolve that freedom will always triumph over tyranny. \n\nSix days ago, Russia’s Vladimir Putin sought to shake the foundations of the free world thinking he could make it bend to his menacing ways. But he badly miscalculated. \n\nHe thought he could roll into Ukraine and the world would roll over. Instead he met a wall of strength he never imagined. \n\nHe met the Ukrainian people. \n\nFrom President Zelenskyy to every Ukrainian, their fearlessness, their courage, their determination, inspires the world.',
metadata: {'document': 1}
}
문자열만 얻기
.split_text를 쓰면 문자열 내용을 직접 얻을 수 있어요.
text_splitter.split_text(state_of_the_union)[0]
'Madam Speaker, Madam Vice President, our First Lady and Second Gentleman. Members of Congress and the Cabinet. Justices of the Supreme Court. My fellow Americans. \n\nLast year COVID-19 kept us apart. This year we are finally together again. \n\nTonight, we meet as Democrats Republicans and Independents. But most importantly as Americans. \n\nWith a duty to one another to the American people to the Constitution. \n\nAnd with an unwavering resolve that freedom will always triumph over tyranny. \n\nSix days ago, Russia’s Vladimir Putin sought to shake the foundations of the free world thinking he could make it bend to his menacing ways. But he badly miscalculated. \n\nHe thought he could roll into Ukraine and the world would roll over. Instead he met a wall of strength he never imagined. \n\nHe met the Ukrainian people. \n\nFrom President Zelenskyy to every Ukrainian, their fearlessness, their courage, their determination, inspires the world.'
JavaScript 예시
splitter.splitText(stateOfTheUnion)[0]
'Madam Speaker, Madam Vice President, our First Lady and Second Gentleman. Members of Congress and the Cabinet. Justices of the Supreme Court. My fellow Americans. \n\nLast year COVID-19 kept us apart. This year we are finally together again. \n\nTonight, we meet as Democrats Republicans and Independents. But most importantly as Americans. \n\nWith a duty to one another to the American people to the Constitution. \n\nAnd with an unwavering resolve that freedom will always triumph over tyranny. \n\nSix days ago, Russia’s Vladimir Putin sought to shake the foundations of the free world thinking he could make it bend to his menacing ways. But he badly miscalculated. \n\nHe thought he could roll into Ukraine and the world would roll over. Instead he met a wall of strength he never imagined. \n\nHe met the Ukrainian people. \n\nFrom President Zelenskyy to every Ukrainian, their fearlessness, their courage, their determination, inspires the world.'
더 알아보기 (Learn more)
- Text splitters 개요 — 분할기의 종류와 선택 기준
- 재귀 문자 텍스트 분할 — 일반 텍스트 권장 분할기
- 토큰 단위 텍스트 분할 — 언어 모델 토큰 제한에 맞춘 분할