| |
| import json |
| import logging |
| from pathlib import Path |
|
|
| |
| |
| |
|
|
| ROOT = Path(__file__).parent.parent.parent |
| SOURCE_DIR = ROOT / "source" / "clubs" / "detail" |
| OUTPUT_FILE = ROOT / "data" / "clubs.jsonl" |
| PREFIX = "club" |
|
|
| logging.basicConfig(level=logging.INFO, format="%(message)s") |
| log = logging.getLogger(__name__) |
|
|
| |
| |
| |
|
|
| def transform_club(data: dict) -> dict: |
| """Flatten club JSON into a ChromaDB-ready format.""" |
| |
| chroma_id = f"{PREFIX}:{int(data['id']):06d}" |
| |
| |
| parts = [] |
| name = data.get('name') |
| location = data.get('location') or f"{data.get('city')}, {data.get('state')}" |
| |
| summary = f"Rocketry Club: {name}" |
| if location: summary += f" located in {location}" |
| summary += "." |
| parts.append(summary) |
| |
| affiliations = [] |
| if data.get("has_nar"): affiliations.append(f"NAR Section {data.get('nar_section') or ''}") |
| if data.get("has_tripoli"): affiliations.append(f"Tripoli Prefecture {data.get('tripoli_prefecture') or ''}") |
| if affiliations: |
| parts.append("Affiliations: " + ", ".join(affiliations).strip() + ".") |
| |
| if data.get("description"): |
| parts.append(data["description"]) |
|
|
| document = " ".join(parts) |
| |
| |
| metadata = { |
| "id": data["id"], |
| "name": name, |
| "city": data.get("city"), |
| "state": data.get("state"), |
| "country": data.get("country"), |
| "has_nar": data.get("has_nar"), |
| "has_tripoli": data.get("has_tripoli"), |
| "url": data.get("url") |
| } |
| |
| metadata = {k: v for k, v in metadata.items() if v is not None} |
| |
| return { |
| "id": chroma_id, |
| "document": document, |
| "metadata": metadata |
| } |
|
|
| |
| |
| |
|
|
| def main(): |
| if not SOURCE_DIR.exists(): |
| log.error(f"Source directory {SOURCE_DIR} not found.") |
| return |
|
|
| OUTPUT_FILE.parent.mkdir(parents=True, exist_ok=True) |
| |
| count = 0 |
| with OUTPUT_FILE.open("w", encoding="utf-8") as out: |
| for shard_dir in sorted(SOURCE_DIR.iterdir()): |
| if not shard_dir.is_dir(): continue |
| for file_path in sorted(shard_dir.glob("*.json")): |
| try: |
| with file_path.open("r", encoding="utf-8") as f: |
| raw_data = json.load(f) |
| processed = transform_club(raw_data) |
| out.write(json.dumps(processed, ensure_ascii=False) + "\n") |
| count += 1 |
| except Exception as e: |
| log.error(f"Error processing {file_path}: {e}") |
|
|
| log.info(f"Successfully built {count} records in {OUTPUT_FILE}") |
|
|
| if __name__ == "__main__": |
| main() |
|
|