codekingpro/portable-devtools
114k
1"""Loads rich text files."""2 3from pathlib import Path4from typing import Any, List, Union5 6from langchain_community.document_loaders.unstructured import (7 UnstructuredFileLoader,8 validate_unstructured_version,9)10 11 12class UnstructuredRTFLoader(UnstructuredFileLoader):13 """Load `RTF` files using `Unstructured`.14 15 You can run the loader in one of two modes: "single" and "elements".16 If you use "single" mode, the document will be returned as a single17 langchain Document object. If you use "elements" mode, the unstructured18 library will split the document into elements such as Title and NarrativeText.19 You can pass in additional unstructured kwargs after mode to apply20 different unstructured settings.21 22 Examples23 --------24 from langchain_community.document_loaders import UnstructuredRTFLoader25 26 loader = UnstructuredRTFLoader(27 "example.rtf", mode="elements", strategy="fast",28 )29 docs = loader.load()30 31 References32 ----------33 https://unstructured-io.github.io/unstructured/bricks.html#partition-rtf34 """35 36 def __init__(37 self,38 file_path: Union[str, Path],39 mode: str = "single",40 **unstructured_kwargs: Any,41 ):42 """43 Initialize with a file path.44 45 Args:46 file_path: The path to the file to load.47 mode: The mode to use for partitioning. See unstructured for details.48 Defaults to "single".49 **unstructured_kwargs: Additional keyword arguments to pass50 to unstructured.51 """52 file_path = str(file_path)53 validate_unstructured_version("0.5.12")54 super().__init__(file_path=file_path, mode=mode, **unstructured_kwargs)55 56 def _get_elements(self) -> List:57 from unstructured.partition.rtf import partition_rtf58 59 return partition_rtf(filename=self.file_path, **self.unstructured_kwargs)60 