| 22 | |
| 23 | |
| 24 | class DocumentLoader: |
| 25 | @staticmethod |
| 26 | def get_files(path: str, filetype: str = '.pdf') -> Iterator[str]: |
| 27 | try: |
| 28 | yield from [ |
| 29 | file_name for file_name in os.listdir(f'{path}') |
| 30 | if file_name.endswith(filetype) |
| 31 | ] |
| 32 | except FileNotFoundError as e: |
| 33 | print(f'\033[31m{e}') |
| 34 | |
| 35 | @staticmethod |
| 36 | def load_documents( |
| 37 | file: str, |
| 38 | filetype: str = '.pdf' |
| 39 | ) -> list[Document] | list[Any]: |
| 40 | #) -> Union[CSVLoader, Docx2txtLoader, PyMuPDFLoader, TextLoader]: |
| 41 | """Loading PDF, Docx, CSV""" |
| 42 | try: |
| 43 | if filetype == '.pdf': |
| 44 | loader = PyMuPDFLoader(file) |
| 45 | elif filetype == '.docx': |
| 46 | loader = Docx2txtLoader(file) |
| 47 | elif filetype == '.csv': |
| 48 | loader = CSVLoader(file, encoding='utf-8') |
| 49 | elif filetype == '.txt': |
| 50 | loader = TextLoader(file, encoding='utf-8') |
| 51 | |
| 52 | return loader.load() |
| 53 | |
| 54 | except Exception as e: |
| 55 | print(f'\033[31m{e}') |
| 56 | return [] |
| 57 | |
| 58 | @staticmethod |
| 59 | def split_documents( |
| 60 | document: list[Document], |
| 61 | chunk_size: int=300, |
| 62 | chunk_overlap: int=0 |
| 63 | ) -> list: |
| 64 | splitter = RecursiveCharacterTextSplitter( |
| 65 | chunk_size=chunk_size, |
| 66 | chunk_overlap=chunk_overlap |
| 67 | ) |
| 68 | |
| 69 | return splitter.split_documents(document) |
| 70 | |
| 71 | @staticmethod |
| 72 | def split_documents_2texts( |
| 73 | documents: list[Document], |
| 74 | chunk_size: int = 300, |
| 75 | chunk_overlap: int = 0 |
| 76 | ) -> list: |
| 77 | splitter = RecursiveCharacterTextSplitter( |
| 78 | chunk_size=chunk_size, |
| 79 | chunk_overlap=chunk_overlap |
| 80 | ) |
| 81 | content = "" |
nothing calls this directly
no outgoing calls
no test coverage detected