Create question_support_loader
Browse files- question_support_loader +58 -0
question_support_loader
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import json
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
from typing import Any, Dict, Optional
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
BASE_DIR = Path(__file__).resolve().parent
|
| 9 |
+
DATA_DIR = BASE_DIR / "data"
|
| 10 |
+
QUESTION_SUPPORT_PATH = DATA_DIR / "question_support_bank.jsonl"
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class QuestionSupportBank:
|
| 14 |
+
def __init__(self, path: Path | None = None):
|
| 15 |
+
self.path = path or QUESTION_SUPPORT_PATH
|
| 16 |
+
self._by_id: Dict[str, Dict[str, Any]] = {}
|
| 17 |
+
self._loaded = False
|
| 18 |
+
|
| 19 |
+
def load(self) -> None:
|
| 20 |
+
self._by_id = {}
|
| 21 |
+
|
| 22 |
+
if not self.path.exists():
|
| 23 |
+
self._loaded = True
|
| 24 |
+
return
|
| 25 |
+
|
| 26 |
+
with self.path.open("r", encoding="utf-8") as f:
|
| 27 |
+
for line_number, line in enumerate(f, start=1):
|
| 28 |
+
line = line.strip()
|
| 29 |
+
if not line:
|
| 30 |
+
continue
|
| 31 |
+
|
| 32 |
+
try:
|
| 33 |
+
row = json.loads(line)
|
| 34 |
+
except json.JSONDecodeError as e:
|
| 35 |
+
print(f"[QuestionSupportBank] Bad JSON on line {line_number}: {e}")
|
| 36 |
+
continue
|
| 37 |
+
|
| 38 |
+
question_id = str(row.get("question_id", "")).strip()
|
| 39 |
+
if not question_id:
|
| 40 |
+
print(f"[QuestionSupportBank] Missing question_id on line {line_number}")
|
| 41 |
+
continue
|
| 42 |
+
|
| 43 |
+
self._by_id[question_id] = row
|
| 44 |
+
|
| 45 |
+
self._loaded = True
|
| 46 |
+
print(f"[QuestionSupportBank] Loaded {len(self._by_id)} support entries.")
|
| 47 |
+
|
| 48 |
+
def get(self, question_id: Optional[str]) -> Optional[Dict[str, Any]]:
|
| 49 |
+
if not self._loaded:
|
| 50 |
+
self.load()
|
| 51 |
+
|
| 52 |
+
if not question_id:
|
| 53 |
+
return None
|
| 54 |
+
|
| 55 |
+
return self._by_id.get(str(question_id).strip())
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
question_support_bank = QuestionSupportBank()
|