Decoder2704/python-chatbot-jay
0
1#!/usr/bin/env python32"""3Build AnswersV2.csv from Answers.csv (Preprocessing.ipynb logic).4 5Adds columns: latest_score, alternate, date — unchanged from the notebook.6"""7 8from __future__ import annotations9 10import argparse11import sys12from datetime import date13from pathlib import Path14 15import pandas as pd16 17_BACKEND_DIR = Path(__file__).resolve().parent.parent18if str(_BACKEND_DIR) not in sys.path:19 sys.path.insert(0, str(_BACKEND_DIR))20 21from app.paths import DATA_DIR, PYBOT_DATA_DIR, ensure_data_and_artifacts_dirs22 23DEFAULT_INPUT = PYBOT_DATA_DIR / "Answers.csv"24DEFAULT_OUTPUT = DATA_DIR / "AnswersV2.csv"25CSV_ENCODING = "latin-1"26 27 28def parse_args() -> argparse.Namespace:29 p = argparse.ArgumentParser(description="Build AnswersV2.csv from Answers.csv.")30 p.add_argument(31 "--input",32 type=Path,33 default=DEFAULT_INPUT,34 help=f"Path to Answers.csv (default: {DEFAULT_INPUT})",35 )36 p.add_argument(37 "--output",38 type=Path,39 default=DEFAULT_OUTPUT,40 help=f"Output path (default: {DEFAULT_OUTPUT})",41 )42 return p.parse_args()43 44 45def build_answers_v2(answers: pd.DataFrame) -> pd.DataFrame:46 """Mirror Preprocessing.ipynb: add latest_score, alternate, date; preserve all else."""47 out = answers.copy()48 out["latest_score"] = out["Score"]49 out["alternate"] = out["Score"]50 out["date"] = date.today()51 return out52 53 54def main() -> int:55 ensure_data_and_artifacts_dirs()56 args = parse_args()57 input_path = args.input.resolve()58 output_path = args.output.resolve()59 60 if not input_path.is_file():61 print(f"ERROR: input file not found: {input_path}", file=sys.stderr)62 return 163 64 try:65 answers = pd.read_csv(input_path, encoding=CSV_ENCODING)66 except UnicodeDecodeError as exc:67 print(68 f"ERROR: failed to decode {input_path} with encoding={CSV_ENCODING!r}: {exc}",69 file=sys.stderr,70 )71 return 172 except OSError as exc:73 print(f"ERROR: cannot read {input_path}: {exc}", file=sys.stderr)74 return 175 76 if "Score" not in answers.columns:77 print("ERROR: Answers.csv must contain a 'Score' column.", file=sys.stderr)78 return 179 80 if answers.empty:81 print("WARNING: Answers.csv is empty; writing empty AnswersV2.csv.", file=sys.stderr)82 83 built = build_answers_v2(answers)84 85 try:86 output_path.parent.mkdir(parents=True, exist_ok=True)87 built.to_csv(output_path, index=False)88 except OSError as exc:89 print(f"ERROR: cannot write {output_path}: {exc}", file=sys.stderr)90 return 191 92 print(f"input: {input_path}")93 print(f"output: {output_path}")94 print(f"rows: {len(built):,}")95 return 096 97 98if __name__ == "__main__":99 raise SystemExit(main())100 