/
githubmirror
/
UFO
Обзор
Документация
Войти
/
githubmirror
/
UFO
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
3.0.7
learner/xml_loader.py
86 строк
2 KB
Ray Huang (from Dev Box)
Replace xml.etree.ElementTree with defusedxml.ElementTree for enhanced security; add defusedxml to requirements.
26 май 2026, 11:47
26 май 2026, 11:47
9d8349c
Код
Авторство
О чём код?
# Copyright (c) Microsoft Corporation. # Licensed under the MIT License. from . import basic import os from langchain_community.document_loaders import UnstructuredXMLLoader from langchain.docstore.document import Document import defusedxml.ElementTree as ET class XMLLoader(basic.BasicDocumentLoader): """ Class to load XML documents. """ def __init__(self, directory: str = None): """ Create a new XMLLoader. """ super().__init__() self.extensions = ".xml" self.directory = directory def get_microsoft_document_metadata(self, file: str): """ Get the metadata for the given file. :param file: The file to get the metadata for. :return: The metadata for the given file. """ if not os.path.exists(file): return {"title": os.path.basename(file), "summary": os.path.basename(file)} tree = ET.parse(file) root = tree.getroot() # Extracting title if root.find("title") is not None: title = root.find("title").text else: title = None # Extracting content summary if root.find("Content-Summary") is not None: summary = root.find("Content-Summary").attrib["value"] else: summary = None return {"title": title, "summary": summary} def get_microsoft_document_text(self, file: str): """ Get the text for the given file. :param file: The file to get the text for. :return: The text for the given file. """ try: doc_text = UnstructuredXMLLoader(file).load()[0].page_content except: doc_text = None return doc_text def construct_document(self): """ Construct a langchain document list. :return: The langchain document list. """ documents = [] for file in self.load_file_name(): text = self.get_microsoft_document_text(file) metadata = self.get_microsoft_document_metadata(file + ".meta") title = metadata["title"] summary = metadata["summary"] page_content = """{title} - {summary}""".format( title=title, summary=summary ) metadata = {"title": title, "summary": summary, "text": text} document = Document(page_content=page_content, metadata=metadata) documents.append(document) return documents