Spaces:
Sleeping
Sleeping
File size: 1,239 Bytes
89c8b6a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | from typing import List
from langchain_text_splitters import RecursiveCharacterTextSplitter
from core.config import CHUNK_SIZE, CHUNK_OVERLAP
def smart_chunking(docs: List) -> List:
print("Đang áp dụng Smart Chunking (Regex Lookahead)...")
# Cấu hình Regex bắt cấu trúc phân cấp hành chính (Chương -> Điều -> Khoản)
legal_splitter = RecursiveCharacterTextSplitter(
chunk_size=CHUNK_SIZE,
chunk_overlap=CHUNK_OVERLAP,
separators=[
"\nChương ",
"\nĐiều ",
"\nKhoản ",
"\n\n",
r"\n(?=\d+\.)",
r"\n(?=[a-z]\.)",
r"\n(?=-|\+)",
"\n", " ", ""
],
length_function=len,
is_separator_regex=True
)
chunks = []
for doc in docs:
doc_chunks = legal_splitter.split_text(doc.page_content)
for chunk_text in doc_chunks:
new_doc = type(doc)(
page_content=chunk_text,
metadata=doc.metadata.copy()
)
chunks.append(new_doc)
print(f"Đã tạo {len(chunks)} chunks thông minh.")
return chunks |