Delete data/qa_index.py
Browse files- data/qa_index.py +0 -61
data/qa_index.py
DELETED
|
@@ -1,61 +0,0 @@
|
|
| 1 |
-
# data/qa_index.py β QA index from dataset + manual answers
|
| 2 |
-
|
| 3 |
-
from typing import Dict, Optional
|
| 4 |
-
|
| 5 |
-
from .loader import ENTRIES
|
| 6 |
-
from .text_utils import normalize_question
|
| 7 |
-
from .manual_answers import MANUAL_ANSWERS
|
| 8 |
-
|
| 9 |
-
QA_INDEX: Dict[str, str] = {}
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
def _build_qa_index():
|
| 13 |
-
for obj in ENTRIES:
|
| 14 |
-
for pair in obj.get("qa", []):
|
| 15 |
-
q = pair.get("q", "")
|
| 16 |
-
a = pair.get("a", "")
|
| 17 |
-
if q and a:
|
| 18 |
-
norm_q = normalize_question(q)
|
| 19 |
-
QA_INDEX[norm_q] = a.strip()
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
_build_qa_index()
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
def answer_from_qa(question: str) -> Optional[str]:
|
| 26 |
-
"""
|
| 27 |
-
1) Manual teacher answers (MANUAL_ANSWERS) β highest priority.
|
| 28 |
-
2) Exact match from dataset QA_INDEX.
|
| 29 |
-
3) Fuzzy match over dataset QA_INDEX.
|
| 30 |
-
"""
|
| 31 |
-
norm_q = normalize_question(question)
|
| 32 |
-
|
| 33 |
-
# 1) manual perfect answers
|
| 34 |
-
if norm_q in MANUAL_ANSWERS:
|
| 35 |
-
return MANUAL_ANSWERS[norm_q]
|
| 36 |
-
|
| 37 |
-
# 2) exact match from dataset
|
| 38 |
-
if norm_q in QA_INDEX:
|
| 39 |
-
return QA_INDEX[norm_q]
|
| 40 |
-
|
| 41 |
-
# 3) fuzzy match over dataset
|
| 42 |
-
if not QA_INDEX:
|
| 43 |
-
return None
|
| 44 |
-
|
| 45 |
-
best_score = 0
|
| 46 |
-
best_answer: Optional[str] = None
|
| 47 |
-
|
| 48 |
-
for stored_q, a in QA_INDEX.items():
|
| 49 |
-
score = 0
|
| 50 |
-
for ch in norm_q:
|
| 51 |
-
if ch and ch in stored_q:
|
| 52 |
-
score += 1
|
| 53 |
-
|
| 54 |
-
if score > best_score:
|
| 55 |
-
best_score = score
|
| 56 |
-
best_answer = a
|
| 57 |
-
|
| 58 |
-
if best_score > 0:
|
| 59 |
-
return best_answer
|
| 60 |
-
|
| 61 |
-
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|