mirror of
https://github.com/hwchase17/langchain
synced 2024-11-06 03:20:49 +00:00
feat: Add UnstructuredXMLLoader
for .xml
files (#5955)
# Unstructured XML Loader Adds an `UnstructuredXMLLoader` class for .xml files. Works with unstructured>=0.6.7. A plain text representation of the text with the XML tags will be available under the `page_content` attribute in the doc. ### Testing ```python from langchain.document_loaders import UnstructuredXMLLoader loader = UnstructuredXMLLoader( "example_data/factbook.xml", ) docs = loader.load() ``` ## Who can review? @hwchase17 @eyurtsev
This commit is contained in:
parent
21bd16bb59
commit
e4224a396b
@ -0,0 +1,27 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<factbook>
|
||||||
|
<country>
|
||||||
|
<name>United States</name>
|
||||||
|
<capital>Washington, DC</capital>
|
||||||
|
<leader>Joe Biden</leader>
|
||||||
|
<sport>Baseball</sport>
|
||||||
|
</country>
|
||||||
|
<country>
|
||||||
|
<name>Canada</name>
|
||||||
|
<capital>Ottawa</capital>
|
||||||
|
<leader>Justin Trudeau</leader>
|
||||||
|
<sport>Hockey</sport>
|
||||||
|
</country>
|
||||||
|
<country>
|
||||||
|
<name>France</name>
|
||||||
|
<capital>Paris</capital>
|
||||||
|
<leader>Emmanuel Macron</leader>
|
||||||
|
<sport>Soccer</sport>
|
||||||
|
</country>
|
||||||
|
<country>
|
||||||
|
<name>Trinidad & Tobado</name>
|
||||||
|
<capital>Port of Spain</capital>
|
||||||
|
<leader>Keith Rowley</leader>
|
||||||
|
<sport>Track & Field</sport>
|
||||||
|
</country>
|
||||||
|
</factbook>
|
78
docs/modules/indexes/document_loaders/examples/xml.ipynb
Normal file
78
docs/modules/indexes/document_loaders/examples/xml.ipynb
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "22a849cc",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# XML\n",
|
||||||
|
"\n",
|
||||||
|
"The `UnstructuredXMLLoader` is used to load `XML` files. The loader works with `.xml` files. The page content will be the text extracted from the XML tags."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"id": "e6616e3a",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from langchain.document_loaders import UnstructuredXMLLoader"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"id": "a654e4d9",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"Document(page_content='United States\\n\\nWashington, DC\\n\\nJoe Biden\\n\\nBaseball\\n\\nCanada\\n\\nOttawa\\n\\nJustin Trudeau\\n\\nHockey\\n\\nFrance\\n\\nParis\\n\\nEmmanuel Macron\\n\\nSoccer\\n\\nTrinidad & Tobado\\n\\nPort of Spain\\n\\nKeith Rowley\\n\\nTrack & Field', metadata={'source': 'example_data/factbook.xml'})"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"loader = UnstructuredXMLLoader(\n",
|
||||||
|
" \"example_data/factbook.xml\",\n",
|
||||||
|
")\n",
|
||||||
|
"docs = loader.load()\n",
|
||||||
|
"docs[0]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "a54342bb",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3 (ipykernel)",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python3"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.8.15"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 5
|
||||||
|
}
|
@ -121,6 +121,7 @@ from langchain.document_loaders.word_document import (
|
|||||||
Docx2txtLoader,
|
Docx2txtLoader,
|
||||||
UnstructuredWordDocumentLoader,
|
UnstructuredWordDocumentLoader,
|
||||||
)
|
)
|
||||||
|
from langchain.document_loaders.xml import UnstructuredXMLLoader
|
||||||
from langchain.document_loaders.youtube import (
|
from langchain.document_loaders.youtube import (
|
||||||
GoogleApiClient,
|
GoogleApiClient,
|
||||||
GoogleApiYoutubeLoader,
|
GoogleApiYoutubeLoader,
|
||||||
@ -242,6 +243,7 @@ __all__ = [
|
|||||||
"UnstructuredRTFLoader",
|
"UnstructuredRTFLoader",
|
||||||
"UnstructuredURLLoader",
|
"UnstructuredURLLoader",
|
||||||
"UnstructuredWordDocumentLoader",
|
"UnstructuredWordDocumentLoader",
|
||||||
|
"UnstructuredXMLLoader",
|
||||||
"WeatherDataLoader",
|
"WeatherDataLoader",
|
||||||
"WebBaseLoader",
|
"WebBaseLoader",
|
||||||
"WhatsAppChatLoader",
|
"WhatsAppChatLoader",
|
||||||
|
22
langchain/document_loaders/xml.py
Normal file
22
langchain/document_loaders/xml.py
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
"""Loader that loads Microsoft Excel files."""
|
||||||
|
from typing import Any, List
|
||||||
|
|
||||||
|
from langchain.document_loaders.unstructured import (
|
||||||
|
UnstructuredFileLoader,
|
||||||
|
validate_unstructured_version,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class UnstructuredXMLLoader(UnstructuredFileLoader):
|
||||||
|
"""Loader that uses unstructured to load XML files."""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self, file_path: str, mode: str = "single", **unstructured_kwargs: Any
|
||||||
|
):
|
||||||
|
validate_unstructured_version(min_unstructured_version="0.6.7")
|
||||||
|
super().__init__(file_path=file_path, mode=mode, **unstructured_kwargs)
|
||||||
|
|
||||||
|
def _get_elements(self) -> List:
|
||||||
|
from unstructured.partition.xml import partition_xml
|
||||||
|
|
||||||
|
return partition_xml(filename=self.file_path, **self.unstructured_kwargs)
|
15
tests/integration_tests/document_loaders/test_xml.py
Normal file
15
tests/integration_tests/document_loaders/test_xml.py
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from langchain.document_loaders import UnstructuredXMLLoader
|
||||||
|
|
||||||
|
EXAMPLE_DIRECTORY = file_path = Path(__file__).parent.parent / "examples"
|
||||||
|
|
||||||
|
|
||||||
|
def test_unstructured_xml_loader() -> None:
|
||||||
|
"""Test unstructured loader."""
|
||||||
|
file_path = os.path.join(EXAMPLE_DIRECTORY, "factbook.xml")
|
||||||
|
loader = UnstructuredXMLLoader(str(file_path))
|
||||||
|
docs = loader.load()
|
||||||
|
|
||||||
|
assert len(docs) == 1
|
27
tests/integration_tests/examples/factbook.xml
Normal file
27
tests/integration_tests/examples/factbook.xml
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<factbook>
|
||||||
|
<country>
|
||||||
|
<name>United States</name>
|
||||||
|
<capital>Washington, DC</capital>
|
||||||
|
<leader>Joe Biden</leader>
|
||||||
|
<sport>Baseball</sport>
|
||||||
|
</country>
|
||||||
|
<country>
|
||||||
|
<name>Canada</name>
|
||||||
|
<capital>Ottawa</capital>
|
||||||
|
<leader>Justin Trudeau</leader>
|
||||||
|
<sport>Hockey</sport>
|
||||||
|
</country>
|
||||||
|
<country>
|
||||||
|
<name>France</name>
|
||||||
|
<capital>Paris</capital>
|
||||||
|
<leader>Emmanuel Macron</leader>
|
||||||
|
<sport>Soccer</sport>
|
||||||
|
</country>
|
||||||
|
<country>
|
||||||
|
<name>Trinidad & Tobado</name>
|
||||||
|
<capital>Port of Spain</capital>
|
||||||
|
<leader>Keith Rowley</leader>
|
||||||
|
<sport>Track & Field</sport>
|
||||||
|
</country>
|
||||||
|
</factbook>
|
Loading…
Reference in New Issue
Block a user