afri_app / utils /helpers.py
SmokeyBandit's picture
Update utils/helpers.py
2023c3c verified
import json
import os
import random
DATA_DIR = "data"
def load_content():
try:
with open(os.path.join(DATA_DIR, "afrikaans_content.json"), "r") as f:
return json.load(f)
except FileNotFoundError:
return {}
def check_spelling(user_answer, correct_word):
return user_answer.lower().strip() == correct_word.lower()
def check_translation(user_answer, correct_translation):
return user_answer.lower().strip() == correct_translation.lower()
def get_random_exercise(category, level, content):
if category in content and level in content[category]:
exercises = content[category][level]
return random.choice(exercises) if exercises else None
return None
# Add the missing check_reading_answer function
def check_reading_answer(user_answer, question_index, reading):
"""
Check if the user's answer matches the expected answer for a reading comprehension question
Args:
user_answer (str): The user's submitted answer
question_index (int): The index of the question being answered
reading (dict): The reading exercise data containing text and questions
Returns:
bool: True if the answer is correct, False otherwise
"""
# Special handling for specific questions
if question_index == 0 and "johan" in reading["title"].lower():
if "tien" in user_answer.lower() or "10" in user_answer:
return True
elif question_index == 1 and "johan" in reading["title"].lower():
if "anke" in user_answer.lower():
return True
elif question_index == 2 and "johan" in reading["title"].lower():
if "kaapstad" in user_answer.lower() or "by die strand" in user_answer.lower():
return True
# Generic fallback - check if answer contains words from the text
return len(user_answer.strip()) > 0 # Accept any non-empty answer for demo purposes