# utils_quiz_embedding.py — Smart AI (MiniLM) version from __future__ import annotations import os, re, random, math from typing import List, Dict, Tuple, Optional from dataclasses import dataclass import numpy as np from sentence_transformers import SentenceTransformer from sklearn.metrics.pairwise import cosine_similarity # --------------------------- # Utils # --------------------------- _ws = re.compile(r"\s+") _tok = re.compile(r"[A-Za-z0-9\-]+") def norm(s: str) -> str: return _ws.sub(" ", s.strip().lower()) def dedup_keep_order(items: List[str]) -> List[str]: seen, out = set(), [] for x in items: k = norm(x) if k and k not in seen: seen.add(k) out.append(x) return out def too_long(s: str, max_chars: int) -> bool: return len(s) > max_chars # --------------------------- # Build embedding index # --------------------------- from typing import Any @dataclass class EmbIndex: model: SentenceTransformer emb_slides: Any candidates: List[str] emb_candidates: Any def extract_terms_en(txt: str, min_token_len=3, max_words=4) -> List[str]: words = [w for w in _tok.findall(txt) if len(w) >= min_token_len] if not words: return [] cands = set() for n in (1, 2): for i in range(len(words) - n + 1): phrase = " ".join(words[i:i+n]) cands.add(phrase) stop = {"and","or","is","are","be","of","in","the","a","an","to","for","on","by", "with","as","at","from","that","this","it","its"} out = [p for p in cands if norm(p) not in stop and len(p) >= 3] return out[:1000] def build_embedding_index(slides: List[str]) -> EmbIndex: model = SentenceTransformer("all-MiniLM-L6-v2") corpus = [norm(s) for s in slides] emb_slides = model.encode(corpus, normalize_embeddings=True) all_cands = [] for s in slides: all_cands.extend(extract_terms_en(s)) candidates = dedup_keep_order(all_cands)[:3000] emb_candidates = model.encode([norm(c) for c in candidates], normalize_embeddings=True) return EmbIndex(model, emb_slides, candidates, emb_candidates) # --------------------------- # Difficulty config # --------------------------- DIFF = { "easy": {"n_options": 4, "sim_low": 0.05, "sim_high": 0.45}, "medium": {"n_options": 4, "sim_low": 0.10, "sim_high": 0.60}, "hard": {"n_options": 5, "sim_low": 0.25, "sim_high": 0.85}, } def clamp(level: str) -> str: return level if level in DIFF else "medium" # --------------------------- # Core logic # --------------------------- def pick_answer(slide: str, emb: EmbIndex) -> Optional[str]: slide_vec = emb.model.encode([norm(slide)], normalize_embeddings=True) sims = cosine_similarity(slide_vec, emb.emb_candidates)[0] if sims.size == 0: return None order = np.argsort(-sims) for i in order: term = emb.candidates[i] if " " in term: return term return emb.candidates[order[0]] def make_distractors(ans: str, emb: EmbIndex, k: int, lo: float, hi: float) -> List[str]: ans_vec = emb.model.encode([norm(ans)], normalize_embeddings=True) sims = cosine_similarity(ans_vec, emb.emb_candidates)[0] idx = np.argsort(-sims) out = [] ansn = norm(ans) for j in idx: t = emb.candidates[j] if norm(t) == ansn: continue if lo <= sims[j] <= hi: out.append(t) if len(out) >= k: break if len(out) < k: pool = [c for c in emb.candidates if norm(c) != ansn] random.shuffle(pool) out.extend(pool[:k - len(out)]) return dedup_keep_order(out)[:k] # --------------------------- # Question generator # --------------------------- def gen_question(slide: str, emb: EmbIndex, level="medium") -> Dict: level = clamp(level) ans = pick_answer(slide, emb) if not ans: return {} spec = DIFF[level] need = spec["n_options"] - 1 distracts = make_distractors(ans, emb, k=max(need+2, 4), lo=spec["sim_low"], hi=spec["sim_high"]) options = [ans] + distracts[:need] random.shuffle(options) stem_templates = [ f"In this slide, what does “{ans}” refer to?", f"Which phrase best describes “{ans}”?", f"Which option most accurately explains “{ans}” in this slide?" ] stem = random.choice(stem_templates) return {"question": stem, "options": options, "answer": ans, "meta": {"level": level}} # --------------------------- # Apply question types (MCQ / CLOZE / T/F) # --------------------------- def _apply_qtypes( qq: List[Dict], q_types: Optional[List[str]], seed: int, ) -> List[Dict]: if not qq: return qq if not q_types: q_types = ["MCQ"] norm_types = [t.upper() for t in q_types if t] if not norm_types: norm_types = ["MCQ"] rng = random.Random(seed) out: List[Dict] = [] for i, q in enumerate(qq): stem = str(q.get("question", "")) ans = str(q.get("answer", "")) opts = [ str(o) for o in q.get("options", []) if isinstance(o, (str, int, float)) and str(o).strip() ] if ans and all(norm(ans) != norm(o) for o in opts): opts.insert(0, ans) if not opts and ans: opts = [ans] while len(opts) < 4: opts.append(f"option-{len(opts)+1}") opts = dedup_keep_order(opts)[:4] target_type = norm_types[i % len(norm_types)] # CLOZE if target_type == "CLOZE": if ans: pattern = re.compile(re.escape(ans), re.IGNORECASE) new_stem, nsub = pattern.subn("____", stem, count=1) if nsub > 0: stem_cloze = new_stem else: stem_cloze = f"____ {stem}" else: stem_cloze = stem out.append({ "type": "CLOZE", "question": stem_cloze, "options": opts, "answer": ans, "explanation": "", "meta": q.get("meta", {}), }) # T/F elif target_type in ("T/F", "TF", "TRUE/FALSE"): wrong = next( (o for o in opts if ans and norm(o) != norm(ans)), None, ) make_true = (i % 2 == 0 or not wrong) if make_true: statement = f"\"{ans}\" ปรากฏเป็นคำสำคัญในสไลด์นี้" tf_ans = "True" expl = "" else: statement = f"\"{wrong}\" ปรากฏเป็นคำสำคัญในสไลด์นี้" tf_ans = "False" expl = f"คำตอบที่ถูกคือ {ans}" out.append({ "type": "T/F", "question": statement, "options": ["True", "False"], "answer": tf_ans, "explanation": expl, "meta": q.get("meta", {}), }) # MCQ else: out.append({ "type": "MCQ", "question": stem, "options": opts, "answer": ans, "explanation": "", "meta": q.get("meta", {}), }) return out # --------------------------- # Public API # --------------------------- def generate_quiz( slides: List[str], n_questions=10, level="medium", seed=42, q_types: Optional[List[str]] = None, ) -> Dict: random.seed(seed) np.random.seed(seed) slides = [s for s in slides if s.strip()] if not slides: slides = ["(empty)"] emb = build_embedding_index(slides) all_q = [] n_slides = max(1, len(slides)) for s in slides: # ให้จำนวน attempt ต่อสไลด์ขึ้นกับจำนวนข้อที่ขอ # เช่น ขอ 10 ข้อ มี 1 slide -> พยายาม ~10 ครั้ง attempts_per_slide = max(3, n_questions // n_slides) for _ in range(attempts_per_slide): q = gen_question(s, emb, level) if q: all_q.append(q) # ตัดไม่ให้เกินจำนวนที่ขอ if len(all_q) > n_questions: all_q = all_q[:n_questions] typed_questions = _apply_qtypes(all_q, q_types, seed) return { "questions": typed_questions, "qc_report": { "slides": len(slides), "generated": len(typed_questions), "level": clamp(level), "seed": seed } }