File size: 2,194 Bytes
bbe2ae8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

from pathlib import Path
from typing import Dict

import pandas as pd

DIFFICULTIES = ("easy", "medium", "hard")
DESCRIPTION_COLUMNS = ("Description", "Ticket Description")

REQUIRED_COLUMNS = {
    "TicketID",
    "expected_category",
    "expected_action",
    "expected_response",
    "requires_escalation",
    "knowledge_base_id",
    "difficulty",
}


def load_dataset(csv_path: str | Path = "dataset.csv") -> pd.DataFrame:
    path = Path(csv_path)
    if not path.exists() and not path.is_absolute():
        candidate = Path(__file__).resolve().parent / path
        if candidate.exists():
            path = candidate
    if not path.exists():
        raise FileNotFoundError(f"Dataset not found at {path}")

    df = pd.read_csv(path)
    desc_col = next((c for c in DESCRIPTION_COLUMNS if c in df.columns), None)
    missing = sorted(REQUIRED_COLUMNS - set(df.columns))
    if desc_col is None:
        missing.append("Description or Ticket Description")
    if missing:
        raise ValueError(f"Missing required dataset columns: {', '.join(missing)}")

    df = df.copy()
    if desc_col != "Description":
        df["Description"] = df[desc_col]
    df["difficulty"] = df["difficulty"].astype(str).str.lower().str.strip()
    df["requires_escalation"] = (
        df["requires_escalation"].astype(str).str.lower().map({"true": True, "false": False}).fillna(False)
    )
    df["TicketID"] = df["TicketID"].astype(int)
    return df


def split_difficulty(df: pd.DataFrame) -> Dict[str, pd.DataFrame]:
    return {d: df[df["difficulty"] == d].reset_index(drop=True).copy() for d in DIFFICULTIES}


def build_expected(row: pd.Series) -> dict:
    return {
        "ticket_id": int(row["TicketID"]),
        "query": str(row["Description"]),
        "expected_category": str(row["expected_category"]),
        "expected_action": str(row["expected_action"]),
        "expected_response": str(row["expected_response"]),
        "requires_escalation": bool(row["requires_escalation"]),
        "kb_id": str(row["knowledge_base_id"]),
        "difficulty": str(row["difficulty"]),
    }