Spaces:
Sleeping
Sleeping
import re | |
from typing import Dict, List, Any | |
class QuizValidator: | |
"""Validerer kvaliteten på genererte quiz-spørsmål""" | |
def validate_question(self, question: Dict[str, Any]) -> Dict[str, Any]: | |
"""Valider et enkelt quiz-spørsmål""" | |
issues = [] | |
score = 0 | |
max_score = 10 | |
# Sjekk grunnleggende struktur (4 poeng) | |
if self._has_valid_question(question.get("spørsmål", "")): | |
score += 1 | |
else: | |
issues.append("Spørsmål mangler eller er for kort") | |
if self._has_valid_alternatives(question.get("alternativer", [])): | |
score += 1 | |
else: | |
issues.append("Må ha nøyaktig 4 alternativer") | |
if self._has_valid_correct_answer(question.get("korrekt_svar"), question.get("alternativer", [])): | |
score += 1 | |
else: | |
issues.append("Ugyldig korrekt svar-indeks") | |
if self._has_valid_explanation(question.get("forklaring", "")): | |
score += 1 | |
else: | |
issues.append("Forklaring mangler eller er for kort") | |
# Sjekk innholdskvalitet (3 poeng) | |
content_score = self._assess_content_quality(question) | |
score += content_score | |
if content_score < 2: | |
issues.append("Lav innholdskvalitet") | |
# Sjekk språkkvalitet (2 poeng) | |
language_score = self._assess_language_quality(question) | |
score += language_score | |
if language_score < 1: | |
issues.append("Språklige problemer") | |
# Sjekk logikk (1 poeng) | |
if self._has_logical_consistency(question): | |
score += 1 | |
else: | |
issues.append("Logiske problemer med spørsmål/svar") | |
return { | |
"valid": score >= 7, # Minimum 70% score | |
"score": score, | |
"max_score": max_score, | |
"percentage": round((score / max_score) * 100, 1), | |
"issues": issues | |
} | |
def _has_valid_question(self, question: str) -> bool: | |
"""Sjekk om spørsmålet er gyldig""" | |
return ( | |
isinstance(question, str) and | |
len(question.strip()) >= 10 and | |
question.strip().endswith("?") | |
) | |
def _has_valid_alternatives(self, alternatives: List[str]) -> bool: | |
"""Sjekk om alternativene er gyldige""" | |
return ( | |
isinstance(alternatives, list) and | |
len(alternatives) == 4 and | |
all(isinstance(alt, str) and len(alt.strip()) > 0 for alt in alternatives) | |
) | |
def _has_valid_correct_answer(self, correct_answer: int, alternatives: List[str]) -> bool: | |
"""Sjekk om korrekt svar er gyldig""" | |
return ( | |
isinstance(correct_answer, int) and | |
0 <= correct_answer < len(alternatives) | |
) | |
def _has_valid_explanation(self, explanation: str) -> bool: | |
"""Sjekk om forklaringen er gyldig""" | |
return ( | |
isinstance(explanation, str) and | |
len(explanation.strip()) >= 10 | |
) | |
def _assess_content_quality(self, question: Dict[str, Any]) -> int: | |
"""Vurder innholdskvalitet (0-3 poeng)""" | |
score = 0 | |
# Sjekk om spørsmålet er spesifikt nok | |
question_text = question.get("spørsmål", "").lower() | |
if any(word in question_text for word in ["hvilken", "hvilket", "hvem", "når", "hvor"]): | |
score += 1 | |
# Sjekk om alternativene er rimelige | |
alternatives = question.get("alternativer", []) | |
if len(set(alt.lower() for alt in alternatives)) == len(alternatives): # Unike alternativer | |
score += 1 | |
# Sjekk om forklaringen er informativ | |
explanation = question.get("forklaring", "") | |
if len(explanation) > 30 and any(word in explanation.lower() for word in ["fordi", "siden", "på grunn av", "because"]): | |
score += 1 | |
return score | |
def _assess_language_quality(self, question: Dict[str, Any]) -> int: | |
"""Vurder språkkvalitet (0-2 poeng)""" | |
score = 0 | |
# Sjekk for norske ord (enkel heuristikk) | |
all_text = f"{question.get('spørsmål', '')} {' '.join(question.get('alternativer', []))} {question.get('forklaring', '')}" | |
norwegian_indicators = ["og", "er", "i", "på", "av", "til", "for", "med", "det", "som", "en", "et", "den", "de"] | |
if any(word in all_text.lower() for word in norwegian_indicators): | |
score += 1 | |
# Sjekk for riktig tegnsetting | |
if question.get("spørsmål", "").strip().endswith("?"): | |
score += 1 | |
return score | |
def _has_logical_consistency(self, question: Dict[str, Any]) -> bool: | |
"""Sjekk logisk konsistens""" | |
try: | |
correct_idx = question.get("korrekt_svar", -1) | |
alternatives = question.get("alternativer", []) | |
if 0 <= correct_idx < len(alternatives): | |
correct_answer = alternatives[correct_idx] | |
explanation = question.get("forklaring", "").lower() | |
# Enkel sjekk: korrekt svar bør nevnes i forklaringen | |
return correct_answer.lower() in explanation | |
return False | |
except: | |
return False | |
def validate_quiz_batch(self, questions: List[Dict[str, Any]]) -> Dict[str, Any]: | |
"""Valider en hel batch med quiz-spørsmål""" | |
if not questions: | |
return { | |
"valid": False, | |
"overall_score": 0, | |
"message": "Ingen spørsmål å validere", | |
"question_results": [] | |
} | |
results = [] | |
total_score = 0 | |
for i, question in enumerate(questions): | |
validation = self.validate_question(question) | |
validation["question_index"] = i | |
results.append(validation) | |
total_score += validation["score"] | |
overall_score = total_score / (len(questions) * 10) * 100 # Prosent av maksimal score | |
# Kategoriser kvalitet | |
if overall_score >= 80: | |
quality_level = "Utmerket" | |
elif overall_score >= 70: | |
quality_level = "God" | |
elif overall_score >= 60: | |
quality_level = "Akseptabel" | |
else: | |
quality_level = "Trenger forbedring" | |
return { | |
"valid": overall_score >= 70, | |
"overall_score": round(overall_score, 1), | |
"quality_level": quality_level, | |
"total_questions": len(questions), | |
"valid_questions": sum(1 for r in results if r["valid"]), | |
"question_results": results, | |
"summary": { | |
"avg_score": round(total_score / len(questions), 1), | |
"max_possible": 10, | |
"common_issues": self._get_common_issues(results) | |
} | |
} | |
def _get_common_issues(self, results: List[Dict[str, Any]]) -> List[str]: | |
"""Finn de vanligste problemene på tvers av spørsmål""" | |
issue_counts = {} | |
for result in results: | |
for issue in result.get("issues", []): | |
issue_counts[issue] = issue_counts.get(issue, 0) + 1 | |
# Returner issues som forekommer i mer enn 25% av spørsmålene | |
threshold = len(results) * 0.25 | |
common_issues = [issue for issue, count in issue_counts.items() if count >= threshold] | |
return common_issues |