Spaces:
Sleeping
Sleeping
Update evaluation.py
Browse files- evaluation.py +22 -9
evaluation.py
CHANGED
|
@@ -1,10 +1,3 @@
|
|
| 1 |
-
"""
|
| 2 |
-
evaluation.py
|
| 3 |
-
Evaluates quiz answers and returns detailed results.
|
| 4 |
-
Supports Multiple Choice, True/False, Short Answer, Fill in the Blank.
|
| 5 |
-
"""
|
| 6 |
-
|
| 7 |
-
|
| 8 |
def evaluate_answers(quiz: dict, answers: dict) -> dict:
|
| 9 |
questions = quiz["questions"]
|
| 10 |
total = len(questions)
|
|
@@ -12,35 +5,55 @@ def evaluate_answers(quiz: dict, answers: dict) -> dict:
|
|
| 12 |
details = []
|
| 13 |
|
| 14 |
for i, q in enumerate(questions):
|
|
|
|
| 15 |
user_ans = str(answers.get(i, "")).strip().lower()
|
| 16 |
correct_ans = str(q.get("answer", "")).strip().lower()
|
| 17 |
q_type = q.get("type", "")
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
# Accept if either string contains the other
|
| 21 |
-
is_correct =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
else:
|
| 23 |
is_correct = user_ans == correct_ans
|
| 24 |
|
|
|
|
| 25 |
if is_correct:
|
| 26 |
correct_count += 1
|
| 27 |
|
|
|
|
| 28 |
details.append({
|
| 29 |
"correct": is_correct,
|
| 30 |
"correct_answer": q.get("answer", "N/A"),
|
| 31 |
"explanation": q.get("explanation", ""),
|
| 32 |
})
|
| 33 |
|
|
|
|
| 34 |
score_pct = round((correct_count / total) * 100) if total > 0 else 0
|
| 35 |
|
|
|
|
| 36 |
if score_pct == 100:
|
| 37 |
feedback = "π Perfect score! Outstanding work!"
|
|
|
|
| 38 |
elif score_pct >= 80:
|
| 39 |
feedback = "π Excellent! You have a strong grasp of the material."
|
|
|
|
| 40 |
elif score_pct >= 60:
|
| 41 |
feedback = "π Good effort! Review the questions you missed to reinforce your understanding."
|
|
|
|
| 42 |
elif score_pct >= 40:
|
| 43 |
feedback = "π Keep practising. Revisit the study material for the topics you found tricky."
|
|
|
|
| 44 |
else:
|
| 45 |
feedback = "πͺ Don't give up! Go back to the study notes and try again β you'll improve."
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
def evaluate_answers(quiz: dict, answers: dict) -> dict:
|
| 2 |
questions = quiz["questions"]
|
| 3 |
total = len(questions)
|
|
|
|
| 5 |
details = []
|
| 6 |
|
| 7 |
for i, q in enumerate(questions):
|
| 8 |
+
|
| 9 |
user_ans = str(answers.get(i, "")).strip().lower()
|
| 10 |
correct_ans = str(q.get("answer", "")).strip().lower()
|
| 11 |
q_type = q.get("type", "")
|
| 12 |
|
| 13 |
+
# Check empty answer first
|
| 14 |
+
if user_ans == "":
|
| 15 |
+
is_correct = False
|
| 16 |
+
|
| 17 |
+
# For descriptive questions
|
| 18 |
+
elif q_type in ("Short Answer", "Fill in the Blank"):
|
| 19 |
+
|
| 20 |
# Accept if either string contains the other
|
| 21 |
+
is_correct = (
|
| 22 |
+
correct_ans in user_ans or
|
| 23 |
+
user_ans in correct_ans
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# For objective questions
|
| 27 |
else:
|
| 28 |
is_correct = user_ans == correct_ans
|
| 29 |
|
| 30 |
+
# Count correct answers
|
| 31 |
if is_correct:
|
| 32 |
correct_count += 1
|
| 33 |
|
| 34 |
+
# Store result details
|
| 35 |
details.append({
|
| 36 |
"correct": is_correct,
|
| 37 |
"correct_answer": q.get("answer", "N/A"),
|
| 38 |
"explanation": q.get("explanation", ""),
|
| 39 |
})
|
| 40 |
|
| 41 |
+
# Calculate percentage
|
| 42 |
score_pct = round((correct_count / total) * 100) if total > 0 else 0
|
| 43 |
|
| 44 |
+
# Feedback generation
|
| 45 |
if score_pct == 100:
|
| 46 |
feedback = "π Perfect score! Outstanding work!"
|
| 47 |
+
|
| 48 |
elif score_pct >= 80:
|
| 49 |
feedback = "π Excellent! You have a strong grasp of the material."
|
| 50 |
+
|
| 51 |
elif score_pct >= 60:
|
| 52 |
feedback = "π Good effort! Review the questions you missed to reinforce your understanding."
|
| 53 |
+
|
| 54 |
elif score_pct >= 40:
|
| 55 |
feedback = "π Keep practising. Revisit the study material for the topics you found tricky."
|
| 56 |
+
|
| 57 |
else:
|
| 58 |
feedback = "πͺ Don't give up! Go back to the study notes and try again β you'll improve."
|
| 59 |
|