File size: 1,582 Bytes
ea540f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Prepare the headline pairwise CTR dataset for modeling."""

import sys
from pathlib import Path

import pandas as pd


REQUIRED_COLUMNS = ["headline_A", "headline_B", "label_pairwise"]
TEXT_COLUMNS = ["headline_A", "headline_B"]


def load_and_standardize(path: Path) -> pd.DataFrame:
    df = pd.read_csv(
        path,
        dtype={col: "string" for col in TEXT_COLUMNS},
    )

    missing = [col for col in REQUIRED_COLUMNS if col not in df.columns]
    if missing:
        raise ValueError(f"Missing required columns in {path.name}: {missing}")

    out = df[TEXT_COLUMNS + ["label_pairwise"]].copy()
    out = out.rename(columns={"label_pairwise": "__label__"})
    return out


def save_split(df: pd.DataFrame, out_path: Path) -> None:
    pd.to_pickle(df, out_path)
    print(f"{out_path.name}: shape={df.shape}, columns={list(df.columns)}")


def main() -> None:
    if len(sys.argv) != 3:
        raise SystemExit("Usage: python prepare_data.py <RAW_DIR> <OUT_DIR>")

    raw_dir = Path(sys.argv[1])
    out_dir = Path(sys.argv[2])
    out_dir.mkdir(parents=True, exist_ok=True)

    train_df = load_and_standardize(raw_dir / "train.csv")
    save_split(train_df, out_dir / "train.pkl")

    val_path = raw_dir / "val.csv"
    if val_path.exists():
        val_df = load_and_standardize(val_path)
        save_split(val_df, out_dir / "val.pkl")

    test_df = load_and_standardize(raw_dir / "test.csv")
    save_split(test_df, out_dir / "test.pkl")


if __name__ == "__main__":
    main()