Source code for langchain_community.document_loaders.rtf
"""加载富文本文件。"""
from pathlib import Path
from typing import Any, List, Union
from langchain_community.document_loaders.unstructured import (
UnstructuredFileLoader,
satisfies_min_unstructured_version,
)
[docs]class UnstructuredRTFLoader(UnstructuredFileLoader):
"""使用`Unstructured`加载`RTF`文件。
您可以在两种模式中的一种中运行加载程序:"single"和"elements"。
如果使用"single"模式,则文档将作为单个`langchain Document`对象返回。
如果使用"elements"模式,`unstructured`库将文档拆分为诸如Title和NarrativeText之类的元素。
您可以在模式之后传递额外的`unstructured kwargs`以应用不同的`unstructured settings`。
示例
--------
```python
from langchain_community.document_loaders import UnstructuredRTFLoader
loader = UnstructuredRTFLoader(
"example.rtf", mode="elements", strategy="fast",
)
docs = loader.load()
```
参考
----------
https://unstructured-io.github.io/unstructured/bricks.html#partition-rtf"""
[docs] def __init__(
self,
file_path: Union[str, Path],
mode: str = "single",
**unstructured_kwargs: Any,
):
"""使用文件路径进行初始化。
参数:
file_path:要加载的文件路径。
mode:用于分区的模式。有关详细信息,请参见非结构化。
默认为"single"。
**unstructured_kwargs:要传递给非结构化的其他关键字参数。
"""
min_unstructured_version = "0.5.12"
if not satisfies_min_unstructured_version(min_unstructured_version):
raise ValueError(
"Partitioning rtf files is only supported in "
f"unstructured>={min_unstructured_version}."
)
super().__init__(file_path=file_path, mode=mode, **unstructured_kwargs)
def _get_elements(self) -> List:
from unstructured.partition.rtf import partition_rtf
return partition_rtf(filename=self.file_path, **self.unstructured_kwargs)