File size: 3,147 Bytes
ab773bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""
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"])