forked from zilliztech/akcio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_parser.py
58 lines (45 loc) · 2.01 KB
/
data_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import os
import sys
from typing import List, Optional
from langchain.docstore.document import Document
from langchain.text_splitter import TextSplitter, RecursiveCharacterTextSplitter
sys.path.append(os.path.join(os.path.dirname(__file__), '../..'))
from config import DATAPARSER_CONFIG # pylint: disable=C0413
CHUNK_SIZE = DATAPARSER_CONFIG.get('chunk_size', 300)
class DataParser:
'''Load data from urls or files (paths or file-like objects) as a list of doc chunks'''
def __init__(self,
splitter: TextSplitter = RecursiveCharacterTextSplitter(
chunk_size=CHUNK_SIZE)
):
self.splitter = splitter
def __call__(self, data_src, source_type: str = 'file') -> List[str]:
if not isinstance(data_src, list):
data_src = [data_src]
if source_type == 'file':
docs = self.from_files(data_src)
elif source_type == 'url':
docs = self.from_urls(data_src)
else:
raise AttributeError(
'Invalid source type. Only support "file" or "url".')
docs = self.splitter.split_documents(docs)
return [str(doc.page_content) for doc in docs]
def from_files(self, files: list, encoding: Optional[str] = None) -> List[Document]:
'''Load documents from path or file-like object, return a list of unsplit LangChain Documents'''
docs = []
for file in files:
if hasattr(file, 'name'):
file_path = file.name
else:
file_path = file
with open(file_path, encoding=encoding) as f:
text = f.read()
metadata = {'source': file_path}
docs.append(Document(page_content=text, metadata=metadata))
return docs
def from_urls(self, urls: List[str]) -> List[Document]:
from langchain.document_loaders import UnstructuredURLLoader # pylint: disable=C0415
loader = UnstructuredURLLoader(urls=urls)
docs = loader.load()
return docs