Spaces:
Sleeping
Sleeping
Create quiz_validator.py
Browse files- quiz_validator.py +204 -0
quiz_validator.py
ADDED
@@ -0,0 +1,204 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
+
from typing import Dict, List, Any
|
3 |
+
|
4 |
+
class QuizValidator:
|
5 |
+
"""Validerer kvaliteten på genererte quiz-spørsmål"""
|
6 |
+
|
7 |
+
def validate_question(self, question: Dict[str, Any]) -> Dict[str, Any]:
|
8 |
+
"""Valider et enkelt quiz-spørsmål"""
|
9 |
+
|
10 |
+
issues = []
|
11 |
+
score = 0
|
12 |
+
max_score = 10
|
13 |
+
|
14 |
+
# Sjekk grunnleggende struktur (4 poeng)
|
15 |
+
if self._has_valid_question(question.get("spørsmål", "")):
|
16 |
+
score += 1
|
17 |
+
else:
|
18 |
+
issues.append("Spørsmål mangler eller er for kort")
|
19 |
+
|
20 |
+
if self._has_valid_alternatives(question.get("alternativer", [])):
|
21 |
+
score += 1
|
22 |
+
else:
|
23 |
+
issues.append("Må ha nøyaktig 4 alternativer")
|
24 |
+
|
25 |
+
if self._has_valid_correct_answer(question.get("korrekt_svar"), question.get("alternativer", [])):
|
26 |
+
score += 1
|
27 |
+
else:
|
28 |
+
issues.append("Ugyldig korrekt svar-indeks")
|
29 |
+
|
30 |
+
if self._has_valid_explanation(question.get("forklaring", "")):
|
31 |
+
score += 1
|
32 |
+
else:
|
33 |
+
issues.append("Forklaring mangler eller er for kort")
|
34 |
+
|
35 |
+
# Sjekk innholdskvalitet (3 poeng)
|
36 |
+
content_score = self._assess_content_quality(question)
|
37 |
+
score += content_score
|
38 |
+
if content_score < 2:
|
39 |
+
issues.append("Lav innholdskvalitet")
|
40 |
+
|
41 |
+
# Sjekk språkkvalitet (2 poeng)
|
42 |
+
language_score = self._assess_language_quality(question)
|
43 |
+
score += language_score
|
44 |
+
if language_score < 1:
|
45 |
+
issues.append("Språklige problemer")
|
46 |
+
|
47 |
+
# Sjekk logikk (1 poeng)
|
48 |
+
if self._has_logical_consistency(question):
|
49 |
+
score += 1
|
50 |
+
else:
|
51 |
+
issues.append("Logiske problemer med spørsmål/svar")
|
52 |
+
|
53 |
+
return {
|
54 |
+
"valid": score >= 7, # Minimum 70% score
|
55 |
+
"score": score,
|
56 |
+
"max_score": max_score,
|
57 |
+
"percentage": round((score / max_score) * 100, 1),
|
58 |
+
"issues": issues
|
59 |
+
}
|
60 |
+
|
61 |
+
def _has_valid_question(self, question: str) -> bool:
|
62 |
+
"""Sjekk om spørsmålet er gyldig"""
|
63 |
+
return (
|
64 |
+
isinstance(question, str) and
|
65 |
+
len(question.strip()) >= 10 and
|
66 |
+
question.strip().endswith("?")
|
67 |
+
)
|
68 |
+
|
69 |
+
def _has_valid_alternatives(self, alternatives: List[str]) -> bool:
|
70 |
+
"""Sjekk om alternativene er gyldige"""
|
71 |
+
return (
|
72 |
+
isinstance(alternatives, list) and
|
73 |
+
len(alternatives) == 4 and
|
74 |
+
all(isinstance(alt, str) and len(alt.strip()) > 0 for alt in alternatives)
|
75 |
+
)
|
76 |
+
|
77 |
+
def _has_valid_correct_answer(self, correct_answer: int, alternatives: List[str]) -> bool:
|
78 |
+
"""Sjekk om korrekt svar er gyldig"""
|
79 |
+
return (
|
80 |
+
isinstance(correct_answer, int) and
|
81 |
+
0 <= correct_answer < len(alternatives)
|
82 |
+
)
|
83 |
+
|
84 |
+
def _has_valid_explanation(self, explanation: str) -> bool:
|
85 |
+
"""Sjekk om forklaringen er gyldig"""
|
86 |
+
return (
|
87 |
+
isinstance(explanation, str) and
|
88 |
+
len(explanation.strip()) >= 10
|
89 |
+
)
|
90 |
+
|
91 |
+
def _assess_content_quality(self, question: Dict[str, Any]) -> int:
|
92 |
+
"""Vurder innholdskvalitet (0-3 poeng)"""
|
93 |
+
score = 0
|
94 |
+
|
95 |
+
# Sjekk om spørsmålet er spesifikt nok
|
96 |
+
question_text = question.get("spørsmål", "").lower()
|
97 |
+
if any(word in question_text for word in ["hvilken", "hvilket", "hvem", "når", "hvor"]):
|
98 |
+
score += 1
|
99 |
+
|
100 |
+
# Sjekk om alternativene er rimelige
|
101 |
+
alternatives = question.get("alternativer", [])
|
102 |
+
if len(set(alt.lower() for alt in alternatives)) == len(alternatives): # Unike alternativer
|
103 |
+
score += 1
|
104 |
+
|
105 |
+
# Sjekk om forklaringen er informativ
|
106 |
+
explanation = question.get("forklaring", "")
|
107 |
+
if len(explanation) > 30 and any(word in explanation.lower() for word in ["fordi", "siden", "på grunn av", "because"]):
|
108 |
+
score += 1
|
109 |
+
|
110 |
+
return score
|
111 |
+
|
112 |
+
def _assess_language_quality(self, question: Dict[str, Any]) -> int:
|
113 |
+
"""Vurder språkkvalitet (0-2 poeng)"""
|
114 |
+
score = 0
|
115 |
+
|
116 |
+
# Sjekk for norske ord (enkel heuristikk)
|
117 |
+
all_text = f"{question.get('spørsmål', '')} {' '.join(question.get('alternativer', []))} {question.get('forklaring', '')}"
|
118 |
+
norwegian_indicators = ["og", "er", "i", "på", "av", "til", "for", "med", "det", "som", "en", "et", "den", "de"]
|
119 |
+
|
120 |
+
if any(word in all_text.lower() for word in norwegian_indicators):
|
121 |
+
score += 1
|
122 |
+
|
123 |
+
# Sjekk for riktig tegnsetting
|
124 |
+
if question.get("spørsmål", "").strip().endswith("?"):
|
125 |
+
score += 1
|
126 |
+
|
127 |
+
return score
|
128 |
+
|
129 |
+
def _has_logical_consistency(self, question: Dict[str, Any]) -> bool:
|
130 |
+
"""Sjekk logisk konsistens"""
|
131 |
+
try:
|
132 |
+
correct_idx = question.get("korrekt_svar", -1)
|
133 |
+
alternatives = question.get("alternativer", [])
|
134 |
+
|
135 |
+
if 0 <= correct_idx < len(alternatives):
|
136 |
+
correct_answer = alternatives[correct_idx]
|
137 |
+
explanation = question.get("forklaring", "").lower()
|
138 |
+
|
139 |
+
# Enkel sjekk: korrekt svar bør nevnes i forklaringen
|
140 |
+
return correct_answer.lower() in explanation
|
141 |
+
|
142 |
+
return False
|
143 |
+
except:
|
144 |
+
return False
|
145 |
+
|
146 |
+
def validate_quiz_batch(self, questions: List[Dict[str, Any]]) -> Dict[str, Any]:
|
147 |
+
"""Valider en hel batch med quiz-spørsmål"""
|
148 |
+
|
149 |
+
if not questions:
|
150 |
+
return {
|
151 |
+
"valid": False,
|
152 |
+
"overall_score": 0,
|
153 |
+
"message": "Ingen spørsmål å validere",
|
154 |
+
"question_results": []
|
155 |
+
}
|
156 |
+
|
157 |
+
results = []
|
158 |
+
total_score = 0
|
159 |
+
|
160 |
+
for i, question in enumerate(questions):
|
161 |
+
validation = self.validate_question(question)
|
162 |
+
validation["question_index"] = i
|
163 |
+
results.append(validation)
|
164 |
+
total_score += validation["score"]
|
165 |
+
|
166 |
+
overall_score = total_score / (len(questions) * 10) * 100 # Prosent av maksimal score
|
167 |
+
|
168 |
+
# Kategoriser kvalitet
|
169 |
+
if overall_score >= 80:
|
170 |
+
quality_level = "Utmerket"
|
171 |
+
elif overall_score >= 70:
|
172 |
+
quality_level = "God"
|
173 |
+
elif overall_score >= 60:
|
174 |
+
quality_level = "Akseptabel"
|
175 |
+
else:
|
176 |
+
quality_level = "Trenger forbedring"
|
177 |
+
|
178 |
+
return {
|
179 |
+
"valid": overall_score >= 70,
|
180 |
+
"overall_score": round(overall_score, 1),
|
181 |
+
"quality_level": quality_level,
|
182 |
+
"total_questions": len(questions),
|
183 |
+
"valid_questions": sum(1 for r in results if r["valid"]),
|
184 |
+
"question_results": results,
|
185 |
+
"summary": {
|
186 |
+
"avg_score": round(total_score / len(questions), 1),
|
187 |
+
"max_possible": 10,
|
188 |
+
"common_issues": self._get_common_issues(results)
|
189 |
+
}
|
190 |
+
}
|
191 |
+
|
192 |
+
def _get_common_issues(self, results: List[Dict[str, Any]]) -> List[str]:
|
193 |
+
"""Finn de vanligste problemene på tvers av spørsmål"""
|
194 |
+
issue_counts = {}
|
195 |
+
|
196 |
+
for result in results:
|
197 |
+
for issue in result.get("issues", []):
|
198 |
+
issue_counts[issue] = issue_counts.get(issue, 0) + 1
|
199 |
+
|
200 |
+
# Returner issues som forekommer i mer enn 25% av spørsmålene
|
201 |
+
threshold = len(results) * 0.25
|
202 |
+
common_issues = [issue for issue, count in issue_counts.items() if count >= threshold]
|
203 |
+
|
204 |
+
return common_issues
|