Download document_chunker.py from janggi3/ragDumm: direct link, hf CLI and curl.
- Browser
- Download file 4.75 kB
-
https://huggingface.co/spaces/janggi3/ragDumm/resolve/main/document_chunker.py
- Command line
-
hf download hf://spaces/janggi3/ragDumm/document_chunker.py
-
curl -L -o document_chunker.py https://huggingface.co/spaces/janggi3/ragDumm/resolve/main/document_chunker.py
4.75 kB
| import os | |
| import glob | |
| import config | |
| from pathlib import Path | |
| from langchain_text_splitters import MarkdownHeaderTextSplitter, RecursiveCharacterTextSplitter | |
| class DocumentChuncker: | |
| def __init__(self): | |
| self.__parent_splitter = MarkdownHeaderTextSplitter( | |
| headers_to_split_on=config.HEADERS_TO_SPLIT_ON, | |
| strip_headers=False | |
| ) | |
| self.__child_splitter = RecursiveCharacterTextSplitter( | |
| chunk_size=config.CHILD_CHUNK_SIZE, | |
| chunk_overlap=config.CHILD_CHUNK_OVERLAP | |
| ) | |
| self.__min_parent_size = config.MIN_PARENT_SIZE | |
| self.__max_parent_size = config.MAX_PARENT_SIZE | |
| def __merge_metadata(target, source, prepend=False): | |
| for key, value in source.items(): | |
| if key not in target: | |
| target[key] = value | |
| elif prepend: | |
| target[key] = f"{value} -> {target[key]}" | |
| else: | |
| target[key] = f"{target[key]} -> {value}" | |
| def create_chunks(self, path_dir=config.MARKDOWN_DIR): | |
| all_parent_chunks, all_child_chunks = [], [] | |
| for doc_path_str in sorted(glob.glob(os.path.join(path_dir, "*.md"))): | |
| doc_path = Path(doc_path_str) | |
| parent_chunks, child_chunks = self.create_chunks_single(doc_path) | |
| all_parent_chunks.extend(parent_chunks) | |
| all_child_chunks.extend(child_chunks) | |
| return all_parent_chunks, all_child_chunks | |
| def create_chunks_single(self, md_path): | |
| doc_path = Path(md_path) | |
| with open(doc_path, "r", encoding="utf-8") as f: | |
| parent_chunks = self.__parent_splitter.split_text(f.read()) | |
| merged_parents = self.__merge_small_parents(parent_chunks) | |
| split_parents = self.__split_large_parents(merged_parents) | |
| cleaned_parents = self.__clean_small_chunks(split_parents) | |
| all_parent_chunks, all_child_chunks = [], [] | |
| self.__create_child_chunks(all_parent_chunks, all_child_chunks, cleaned_parents, doc_path) | |
| return all_parent_chunks, all_child_chunks | |
| def __merge_small_parents(self, chunks): | |
| if not chunks: | |
| return [] | |
| merged, current = [], None | |
| for chunk in chunks: | |
| if current is None: | |
| current = chunk | |
| else: | |
| current.page_content += "\n\n" + chunk.page_content | |
| self.__merge_metadata(current.metadata, chunk.metadata) | |
| if len(current.page_content) >= self.__min_parent_size: | |
| merged.append(current) | |
| current = None | |
| if current: | |
| if merged: | |
| merged[-1].page_content += "\n\n" + current.page_content | |
| self.__merge_metadata(merged[-1].metadata, current.metadata) | |
| else: | |
| merged.append(current) | |
| return merged | |
| def __split_large_parents(self, chunks): | |
| split_chunks = [] | |
| for chunk in chunks: | |
| if len(chunk.page_content) <= self.__max_parent_size: | |
| split_chunks.append(chunk) | |
| else: | |
| splitter = RecursiveCharacterTextSplitter( | |
| chunk_size=self.__max_parent_size, | |
| chunk_overlap=config.CHILD_CHUNK_OVERLAP | |
| ) | |
| sub_chunks = splitter.split_documents([chunk]) | |
| split_chunks.extend(sub_chunks) | |
| return split_chunks | |
| def __clean_small_chunks(self, chunks): | |
| cleaned = [] | |
| for i, chunk in enumerate(chunks): | |
| if len(chunk.page_content) < self.__min_parent_size: | |
| if cleaned: | |
| cleaned[-1].page_content += "\n\n" + chunk.page_content | |
| self.__merge_metadata(cleaned[-1].metadata, chunk.metadata) | |
| elif i < len(chunks) - 1: | |
| chunks[i + 1].page_content = chunk.page_content + "\n\n" + chunks[i + 1].page_content | |
| self.__merge_metadata(chunks[i + 1].metadata, chunk.metadata, prepend=True) | |
| else: | |
| cleaned.append(chunk) | |
| else: | |
| cleaned.append(chunk) | |
| return cleaned | |
| def __create_child_chunks(self, all_parent_pairs, all_child_chunks, parent_chunks, doc_path): | |
| for i, p_chunk in enumerate(parent_chunks): | |
| parent_id = f"{doc_path.stem}_parent_{i}" | |
| p_chunk.metadata.update({"source": str(doc_path.stem)+".pdf", "parent_id": parent_id}) | |
| all_parent_pairs.append((parent_id, p_chunk)) | |
| all_child_chunks.extend(self.__child_splitter.split_documents([p_chunk])) |