-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader.py
More file actions
22 lines (19 loc) · 725 Bytes
/
Copy pathloader.py
File metadata and controls
22 lines (19 loc) · 725 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import os
from cleaner import clean
def load_docs(folder="data"):
docs = {}
if not os.path.exists(folder):
os.makedirs(folder)
print(f"[loader] created '{folder}/'; place .txt files there.")
return docs
for root, dirs, files in os.walk(folder):
for fname in sorted(files):
if fname.endswith(".txt"):
path = os.path.join(root, fname)
# e.g. sub/file.txt
rel_key = os.path.relpath(path, folder)
with open(path, "r", encoding="utf-8", errors="ignore") as f:
docs[rel_key] = clean(f.read())
if not docs:
print(f"[loader] no .txt files found in '{folder}/'.")
return docs