Spaces:
Sleeping
Sleeping
Upload catalog_loader.py
Browse files- data/catalog_loader.py +69 -0
data/catalog_loader.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import hashlib
|
| 4 |
+
import json
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
from typing import Dict, Tuple
|
| 7 |
+
|
| 8 |
+
import pandas as pd
|
| 9 |
+
|
| 10 |
+
from crawler.utils import canonicalize_url
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def make_assessment_id(url: str) -> str:
|
| 14 |
+
canonical = canonicalize_url(url.lower())
|
| 15 |
+
return hashlib.sha1(canonical.encode("utf-8")).hexdigest()
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def to_solutions_url(url: str) -> str:
|
| 19 |
+
"""Ensure outgoing URLs include the /solutions/ prefix for compatibility with labels/eval."""
|
| 20 |
+
return url.replace("/products/product-catalog", "/solutions/products/product-catalog")
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def load_catalog(path: str) -> Tuple[pd.DataFrame, Dict[str, dict], Dict[str, str]]:
|
| 24 |
+
p = Path(path)
|
| 25 |
+
if not p.exists():
|
| 26 |
+
raise FileNotFoundError(f"Catalog file not found: {path}")
|
| 27 |
+
if p.suffix == ".jsonl":
|
| 28 |
+
df = pd.read_json(p, lines=True)
|
| 29 |
+
elif p.suffix in {".parquet", ".pq"}:
|
| 30 |
+
df = pd.read_parquet(p)
|
| 31 |
+
else:
|
| 32 |
+
raise ValueError(f"Unsupported catalog format: {p}")
|
| 33 |
+
|
| 34 |
+
df["url_canonical"] = df["url"].apply(lambda u: canonicalize_url(str(u).lower()))
|
| 35 |
+
df["assessment_id"] = df["url_canonical"].apply(make_assessment_id)
|
| 36 |
+
df["url_recommend"] = df["url"].apply(to_solutions_url)
|
| 37 |
+
if "duration" in df.columns and "duration_minutes" not in df.columns:
|
| 38 |
+
df["duration_minutes"] = df["duration"]
|
| 39 |
+
for col in ["remote_support", "adaptive_support"]:
|
| 40 |
+
if col in df.columns:
|
| 41 |
+
df[col] = df[col].fillna(False).astype(bool)
|
| 42 |
+
catalog_by_id = {row.assessment_id: row._asdict() if hasattr(row, "_asdict") else row.to_dict() for _, row in df.iterrows()}
|
| 43 |
+
id_by_url = {}
|
| 44 |
+
for canonical, aid in zip(df["url_canonical"], df["assessment_id"]):
|
| 45 |
+
id_by_url[canonical] = aid
|
| 46 |
+
alt_products = canonical.replace("/solutions/products/product-catalog", "/products/product-catalog")
|
| 47 |
+
alt_solutions = canonical.replace("/products/product-catalog", "/solutions/products/product-catalog")
|
| 48 |
+
id_by_url.setdefault(alt_products, aid)
|
| 49 |
+
id_by_url.setdefault(alt_solutions, aid)
|
| 50 |
+
return df, catalog_by_id, id_by_url
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def save_catalog_with_ids(input_path: str, output_path: str) -> None:
|
| 54 |
+
df, _, _ = load_catalog(input_path)
|
| 55 |
+
Path(output_path).parent.mkdir(parents=True, exist_ok=True)
|
| 56 |
+
if output_path.endswith(".jsonl"):
|
| 57 |
+
df.to_json(output_path, orient="records", lines=True)
|
| 58 |
+
else:
|
| 59 |
+
df.to_parquet(output_path, index=False)
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
if __name__ == "__main__":
|
| 63 |
+
import argparse
|
| 64 |
+
|
| 65 |
+
parser = argparse.ArgumentParser()
|
| 66 |
+
parser.add_argument("--input", required=True)
|
| 67 |
+
parser.add_argument("--output", required=True)
|
| 68 |
+
args = parser.parse_args()
|
| 69 |
+
save_catalog_with_ids(args.input, args.output)
|