| import re |
| from sklearn.metrics.pairwise import cosine_similarity |
| from quiz.semantic import semantic_similarity |
| from core.llm import embeddings |
|
|
|
|
| |
| |
| |
|
|
| def clean_text(text: str): |
|
|
| if not text: |
| return "" |
|
|
| text = text.lower() |
|
|
| |
| text = re.sub(r"correct answer[:\-]*", "", text) |
|
|
| |
| text = re.sub(r"[^\w\s]", "", text) |
|
|
| return " ".join(text.split()) |
|
|
|
|
| |
| |
| |
|
|
| def semantic_similarity(a: str, b: str): |
|
|
| try: |
| emb_a = embeddings.embed_query(a) |
| emb_b = embeddings.embed_query(b) |
|
|
| similarity = cosine_similarity( |
| [emb_a], |
| [emb_b] |
| )[0][0] |
|
|
| return similarity |
|
|
| except Exception as e: |
| print("Embedding error:", e) |
| return 0 |
|
|
|
|
| |
| |
| |
|
|
| def validate_answer(user, correct, qtype): |
|
|
| if not user: |
| return False |
|
|
| |
| |
| |
| if qtype in ["MCQ", "True/False"]: |
| return bool( |
| user.strip().lower() |
| == correct.strip().lower() |
| ) |
|
|
| |
| |
| |
| similarity = semantic_similarity(user, correct) |
|
|
| print("Similarity:", similarity) |
|
|
| |
| return bool(similarity >= 0.65) |