mirror of
https://github.com/kennethreitz/langchain.git
synced 2026-06-05 23:00:18 +00:00
1feac83323
updated docstring for the `document_loaders` Maintainer responsibilities: - DataLoaders / VectorStores / Retrievers: @rlancemartin, @eyurtsev
29 lines
969 B
Python
29 lines
969 B
Python
"""Loads .txt web files."""
|
|
from typing import List
|
|
|
|
from langchain.docstore.document import Document
|
|
from langchain.document_loaders.base import BaseLoader
|
|
|
|
|
|
class GutenbergLoader(BaseLoader):
|
|
"""Loader that uses urllib to load .txt web files."""
|
|
|
|
def __init__(self, file_path: str):
|
|
"""Initialize with a file path."""
|
|
if not file_path.startswith("https://www.gutenberg.org"):
|
|
raise ValueError("file path must start with 'https://www.gutenberg.org'")
|
|
|
|
if not file_path.endswith(".txt"):
|
|
raise ValueError("file path must end with '.txt'")
|
|
|
|
self.file_path = file_path
|
|
|
|
def load(self) -> List[Document]:
|
|
"""Load file."""
|
|
from urllib.request import urlopen
|
|
|
|
elements = urlopen(self.file_path)
|
|
text = "\n\n".join([str(el.decode("utf-8-sig")) for el in elements])
|
|
metadata = {"source": self.file_path}
|
|
return [Document(page_content=text, metadata=metadata)]
|