SEAAITUTOR / utils.py
AptlyDigital's picture
Create utils.py
818bbf9 verified
"""
Utility functions for SEA Prep Tutor
"""
import re
import json
from typing import List, Dict, Optional
def clean_text(text: str) -> str:
"""Clean and normalize text"""
# Remove extra whitespace
text = re.sub(r'\s+', ' ', text)
# Remove special characters but keep basic punctuation
text = re.sub(r'[^\w\s\.\?\!,;:\-\(\)]', '', text)
return text.strip()
def parse_mcq_options(text: str) -> Optional[Dict]:
"""Parse MCQ options from text"""
options = {}
# Pattern for A), B), etc.
pattern = r'([A-D])[\)\.]\s*([^A-D]+?)(?=\s+[A-D][\)\.]|\s*$)'
matches = re.findall(pattern, text, re.IGNORECASE | re.DOTALL)
for letter, content in matches:
content = content.strip()
if content and len(content) > 1:
options[letter.upper()] = content
return options if options else None
def calculate_difficulty(text: str, subject: str) -> int:
"""Calculate question difficulty (1-5)"""
word_count = len(text.split())
sentence_count = len(re.split(r'[.!?]', text))
base_score = min(5, word_count // 20 + sentence_count // 2)
if subject == "Math":
# Additional factors for math
numbers = len(re.findall(r'\d+', text))
operations = len(re.findall(r'[+\-×÷=]', text))
base_score += min(2, (numbers + operations) // 3)
return min(5, max(1, base_score))
def format_test_for_display(test: Dict) -> str:
"""Format test as HTML for display"""
html = f"""
<div style="padding: 20px; background: #f8f9fa; border-radius: 10px;">
<h3>{test.get('title', 'Practice Test')}</h3>
<p><strong>Date:</strong> {test.get('date', '')}</p>
<p><strong>Questions:</strong> {len(test.get('questions', []))}</p>
<p><strong>Time:</strong> {test.get('total_time', '60 minutes')}</p>
<hr>
"""
for i, q in enumerate(test.get('questions', []), 1):
html += f"""
<div style="margin: 15px 0; padding: 15px; background: white; border-radius: 5px;">
<h4>Question {i}</h4>
<p>{q.get('text', '')}</p>
<p><small>Type: {q.get('type', '')} | Difficulty: {q.get('difficulty_label', 'Medium')}</small></p>
</div>
"""
html += "</div>"
return html