Download src/data_loader.py from Talayhan/task4.2: direct link, hf CLI and curl.
- Browser
- Download file 3.15 kB
-
https://huggingface.co/datasets/Talayhan/task4.2/resolve/main/src/data_loader.py
- Command line
-
hf download hf://datasets/Talayhan/task4.2/src/data_loader.py
-
curl -L -o data_loader.py https://huggingface.co/datasets/Talayhan/task4.2/resolve/main/src/data_loader.py
3.15 kB
| """ | |
| Veri Yükleme Katmanı | |
| ------------------------ | |
| load_articles(): umutertugrul/turkish-hospital-medical-articles veri setinden | |
| rastgele SAMPLE_SIZE kadar makale çeker. Bu ortamda Hugging Face Hub'a ağ | |
| erişimi olmadığı için, gerçek indirme başarısız olursa (ImportError veya | |
| bağlantı hatası) otomatik olarak data/synthetic_corpus.py'deki küçük örnek | |
| korpusa düşer — bu SADECE pipeline'ın uçtan uca çalıştığını göstermek içindir, | |
| nihai teslimde gerçek veriyle çalıştırılmalıdır (bkz. README "Nasıl Çalıştırılır"). | |
| """ | |
| import random | |
| import sys | |
| import os | |
| sys.path.append(os.path.join(os.path.dirname(__file__), "..")) | |
| from src import config | |
| def load_real_dataset(): | |
| """Gerçek veri setini Hugging Face Hub'dan çeker (internet + `datasets` gerekir).""" | |
| from datasets import load_dataset, Features, Value | |
| features = Features({ | |
| "url": Value("string"), | |
| "title": Value("string"), | |
| "headings": Value("string"), | |
| "text": Value("string"), | |
| "publish_date": Value("string"), | |
| "update_date": Value("string"), | |
| "scrape_date": Value("string"), | |
| "__source": Value("string"), | |
| }) | |
| files = {name.split(".")[0]: name for name in config.HF_DATASET_FILES} | |
| ds = load_dataset(config.HF_DATASET_ID, data_files=files, features=features) | |
| all_rows = [] | |
| for split_name, split_ds in ds.items(): | |
| for row in split_ds: | |
| all_rows.append(dict(row)) | |
| return all_rows | |
| def load_synthetic_dataset(): | |
| """Offline geliştirme/test için küçük sentetik korpus (bkz. data/synthetic_corpus.py).""" | |
| from data.synthetic_corpus import SYNTHETIC_ARTICLES | |
| return list(SYNTHETIC_ARTICLES) | |
| def load_articles(sample_size: int = None, seed: int = None, force_synthetic: bool = False): | |
| """ | |
| Makaleleri yükler ve `sample_size` kadar rastgele örnekler. | |
| force_synthetic=True ise (veya gerçek veri setine erişilemezse) sentetik | |
| korpusa düşer. | |
| Dönüş: list[dict] — her biri en az {url, title, text, __source} içerir. | |
| """ | |
| sample_size = sample_size or config.SAMPLE_SIZE | |
| seed = seed if seed is not None else config.RANDOM_SEED | |
| rng = random.Random(seed) | |
| used_synthetic = False | |
| if force_synthetic: | |
| rows = load_synthetic_dataset() | |
| used_synthetic = True | |
| else: | |
| try: | |
| rows = load_real_dataset() | |
| except Exception as e: | |
| print(f"[UYARI] Gerçek veri seti çekilemedi ({e}). Sentetik korpusa düşülüyor " | |
| f"(SADECE offline test amaçlı — nihai teslimde bu olmamalı).") | |
| rows = load_synthetic_dataset() | |
| used_synthetic = True | |
| # text alanı boş olan satırları at | |
| rows = [r for r in rows if r.get("text") and r["text"].strip()] | |
| if len(rows) > sample_size: | |
| rows = rng.sample(rows, sample_size) | |
| return rows, used_synthetic | |
| if __name__ == "__main__": | |
| articles, is_synthetic = load_articles() | |
| print(f"{len(articles)} makale yüklendi. (sentetik mi: {is_synthetic})") | |
| for a in articles[:2]: | |
| print("-", a["title"], "|", a["url"]) | |