電子郵件 (Email)
本筆記展示如何載入電子郵件(.eml
)或 Microsoft Outlook
(.msg
)檔案。
請參閱本指南,了解更多關於在本地設定 Unstructured 的說明,包括設定所需的系統依賴性。
使用 Unstructured
%pip install --upgrade --quiet unstructured
from langchain_community.document_loaders import UnstructuredEmailLoader
loader = UnstructuredEmailLoader("./example_data/fake-email.eml")
data = loader.load()
data
API 參考:UnstructuredEmailLoader
[Document(page_content='This is a test email to use for unit tests.\n\nImportant points:\n\nRoses are red\n\nViolets are blue', metadata={'source': './example_data/fake-email.eml'})]
保留元素
在底層,Unstructured 會為不同的文字區塊建立不同的「元素」。 預設情況下,我們會將它們組合在一起,但您可以透過指定 mode="elements"
輕鬆保持這種分離。
loader = UnstructuredEmailLoader("example_data/fake-email.eml", mode="elements")
data = loader.load()
data[0]
Document(page_content='This is a test email to use for unit tests.', metadata={'source': 'example_data/fake-email.eml', 'file_directory': 'example_data', 'filename': 'fake-email.eml', 'last_modified': '2022-12-16T17:04:16-05:00', 'sent_from': ['Matthew Robinson <mrobinson@unstructured.io>'], 'sent_to': ['Matthew Robinson <mrobinson@unstructured.io>'], 'subject': 'Test Email', 'languages': ['eng'], 'filetype': 'message/rfc822', 'category': 'NarrativeText'})
處理附件
您可以透過在建構函式中設定 process_attachments=True
,使用 UnstructuredEmailLoader
處理附件。 預設情況下,附件將使用 unstructured
中的 partition
函數進行分割。 您可以透過將函數傳遞給 attachment_partitioner
kwarg 來使用不同的分割函數。
loader = UnstructuredEmailLoader(
"example_data/fake-email.eml",
mode="elements",
process_attachments=True,
)
data = loader.load()
data[0]
Document(page_content='This is a test email to use for unit tests.', metadata={'source': 'example_data/fake-email.eml', 'file_directory': 'example_data', 'filename': 'fake-email.eml', 'last_modified': '2022-12-16T17:04:16-05:00', 'sent_from': ['Matthew Robinson <mrobinson@unstructured.io>'], 'sent_to': ['Matthew Robinson <mrobinson@unstructured.io>'], 'subject': 'Test Email', 'languages': ['eng'], 'filetype': 'message/rfc822', 'category': 'NarrativeText'})
使用 OutlookMessageLoader
%pip install --upgrade --quiet extract_msg
from langchain_community.document_loaders import OutlookMessageLoader
loader = OutlookMessageLoader("example_data/fake-email.msg")
data = loader.load()
data[0]
API 參考:OutlookMessageLoader
Document(page_content='This is a test email to experiment with the MS Outlook MSG Extractor\r\n\r\n\r\n-- \r\n\r\n\r\nKind regards\r\n\r\n\r\n\r\n\r\nBrian Zhou\r\n\r\n', metadata={'source': 'example_data/fake-email.msg', 'subject': 'Test for TIF files', 'sender': 'Brian Zhou <brizhou@gmail.com>', 'date': datetime.datetime(2013, 11, 18, 0, 26, 24, tzinfo=zoneinfo.ZoneInfo(key='America/Los_Angeles'))})