File size: 8,799 Bytes
91391c2 3d94eac 91391c2 3d94eac 91391c2 0262cf9 91391c2 0262cf9 91391c2 0262cf9 91391c2 3d94eac 91391c2 3d94eac 91391c2 3d94eac 91391c2 | 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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | # 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
}
}
|