Spaces:
Sleeping
Sleeping
Download src/data_loader.py from Sujith2121/Semantic-search-cache: direct link, hf CLI and curl.
- Browser
- Download file 865 Bytes
-
https://huggingface.co/spaces/Sujith2121/Semantic-search-cache/resolve/main/src/data_loader.py
- Command line
-
hf download hf://spaces/Sujith2121/Semantic-search-cache/src/data_loader.py
-
curl -L -o data_loader.py https://huggingface.co/spaces/Sujith2121/Semantic-search-cache/resolve/main/src/data_loader.py
865 Bytes
| import os | |
| def load_documents(dataset_path): | |
| documents = [] | |
| labels = [] | |
| for category in os.listdir(dataset_path): | |
| category_path = os.path.join(dataset_path, category) | |
| if os.path.isdir(category_path): | |
| for file in os.listdir(category_path): | |
| file_path = os.path.join(category_path, file) | |
| try: | |
| with open(file_path, "r", encoding="latin-1") as f: | |
| text = f.read() | |
| documents.append(text) | |
| labels.append(category) | |
| except: | |
| continue | |
| return documents, labels | |
| if __name__ == "__main__": | |
| dataset_path = "data/20_newsgroups" | |
| docs, labels = load_documents(dataset_path) | |
| print("Total documents:", len(docs)) | |
| print("Example category:", labels[0]) |