File size: 8,430 Bytes
d0b8e8f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 | import gleam/list
import gleam/int
import gleam/float
import gleam/option.{type Option, Some, None}
pub type ExamQuestion {
MultipleChoice(
question_text: String,
options: List(String),
correct_answer: Int,
explanation: String,
difficulty: String,
)
ShortAnswer(
question_text: String,
expected_keywords: List(String),
explanation: String,
difficulty: String,
)
Essay(question_text: String, rubric: GradingRubric, difficulty: String)
}
pub type GradingRubric {
GradingRubric(
criteria: List(#(String, Int)),
total_points: Int,
)
}
pub type MockExam {
MockExam(
exam_id: String,
title: String,
questions: List(ExamQuestion),
duration_minutes: Int,
total_points: Int,
difficulty_distribution: DifficultyDistribution,
generated_at: String,
)
}
pub type DifficultyDistribution {
DifficultyDistribution(
easy_percentage: Float,
medium_percentage: Float,
hard_percentage: Float,
)
}
pub type ExamResult {
ExamResult(
exam_id: String,
score: Float,
max_score: Int,
percentage: Float,
time_taken: Int,
answers: List(#(Int, String)),
feedback: List(String),
)
}
pub fn generate_mock_exam(
topic: String,
cards: List(#(String, String, String)),
num_questions: Int,
difficulty_level: String,
) -> MockExam {
let questions = list.take(
list.map(cards, fn(card) {
let #(_id, q, a) = card
create_question_from_card(q, a, difficulty_level)
}),
num_questions,
)
MockExam(
exam_id: "EXAM_" <> topic <> "_2026",
title: "Mock Exam: " <> topic,
questions: questions,
duration_minutes: num_questions * 2,
total_points: num_questions * 10,
difficulty_distribution: analyze_difficulty(questions),
generated_at: "2026-03-19T00:00:00Z",
)
}
fn create_question_from_card(
question: String,
answer: String,
difficulty: String,
) -> ExamQuestion {
case difficulty {
"easy" ->
MultipleChoice(
question_text: question,
options: [
answer,
"Incorrect option 1",
"Incorrect option 2",
"Incorrect option 3",
],
correct_answer: 0,
explanation: "The correct answer is: " <> answer,
difficulty: "Easy",
)
"medium" ->
ShortAnswer(
question_text: question,
expected_keywords: extract_keywords(answer),
explanation: answer,
difficulty: "Medium",
)
_ ->
Essay(
question_text: question,
rubric: GradingRubric(
criteria: [
#("Understanding", 4),
#("Organization", 3),
#("Examples", 3),
],
total_points: 10,
),
difficulty: "Hard",
)
}
}
fn extract_keywords(text: String) -> List(String) {
// In production, would use NLP
let _ = text
[]
}
fn analyze_difficulty(questions: List(ExamQuestion)) -> DifficultyDistribution {
let total = list.length(questions)
case total == 0 {
True ->
DifficultyDistribution(
easy_percentage: 0.0,
medium_percentage: 0.0,
hard_percentage: 0.0,
)
False ->
DifficultyDistribution(
easy_percentage: int.to_float(count_by_difficulty(questions, "Easy")) /. int.to_float(total) *. 100.0,
medium_percentage: int.to_float(count_by_difficulty(questions, "Medium")) /. int.to_float(total) *. 100.0,
hard_percentage: int.to_float(count_by_difficulty(questions, "Hard")) /. int.to_float(total) *. 100.0,
)
}
}
fn get_question_at(questions: List(ExamQuestion), index: Int) -> Option(ExamQuestion) {
case questions, index {
[q, .._], 0 -> Some(q)
[_, ..rest], i if i > 0 -> get_question_at(rest, i - 1)
_, _ -> None
}
}
fn count_by_difficulty(
questions: List(ExamQuestion),
level: String,
) -> Int {
list.fold(questions, 0, fn(acc, q) {
case q {
MultipleChoice(_, _, _, _, diff) if diff == level -> acc + 1
ShortAnswer(_, _, _, diff) if diff == level -> acc + 1
Essay(_, _, diff) if diff == level -> acc + 1
_ -> acc
}
})
}
pub fn generate_pdf_exam(exam: MockExam) -> String {
// In production, would create actual PDF
let header =
"MOCK EXAMINATION\n" <>
exam.title <>
"\n" <>
"Duration: " <>
int.to_string(exam.duration_minutes) <>
" minutes\n" <>
"Total Points: " <>
int.to_string(exam.total_points) <>
"\n\n"
let questions_text =
list.index_map(exam.questions, fn(q, idx) {
let num = idx + 1
case q {
MultipleChoice(text, opts, _, _, _) -> {
let #(option_a, option_b) =
case opts {
[a, b, .._] -> #(a, b)
[a] -> #(a, "")
_ -> #("", "")
}
int.to_string(num) <>
". " <>
text <>
"\n A) " <>
option_a <>
"\n B) " <>
option_b <>
"\n\n"
}
ShortAnswer(text, _, _, _) ->
int.to_string(num) <>
". " <>
text <>
"\n[Answer space]\n\n"
Essay(text, _, _) ->
int.to_string(num) <>
". [Essay] " <>
text <>
"\n[Essay writing space]\n\n"
}
})
|> list.fold("", fn(acc, q) { acc <> q })
header <> questions_text <> "\n\nEND OF EXAMINATION"
}
pub fn calculate_exam_score(
exam: MockExam,
answers: List(#(Int, String)),
) -> ExamResult {
let correct_count =
list.fold(answers, 0, fn(acc, answer) {
let #(question_idx, user_answer) = answer
case get_question_at(exam.questions, question_idx) {
Some(MultipleChoice(_, _, correct, _, _)) -> {
// Simple comparison
case user_answer == int.to_string(correct) {
True -> acc + 1
False -> acc
}
}
_ -> acc
}
})
let points_earned = correct_count * 10
let percentage =
int.to_float(points_earned) /. int.to_float(exam.total_points) *. 100.0
ExamResult(
exam_id: exam.exam_id,
score: int.to_float(points_earned),
max_score: exam.total_points,
percentage: percentage,
time_taken: exam.duration_minutes,
answers: answers,
feedback: generate_exam_feedback(percentage),
)
}
pub fn generate_exam_feedback(percentage: Float) -> List(String) {
case percentage {
p if p >. 90.0 -> [
"🏆 Outstanding! You've achieved elite mastery!",
"Your understanding of these topics is exceptional.",
"Consider challenging yourself with harder material.",
]
p if p >. 75.0 -> [
"💪 Great job! You've demonstrated solid understanding.",
"A few areas could use some review.",
"Focus on the topics you found challenging.",
]
p if p >. 60.0 -> [
"📈 Good effort! You're on the right track.",
"Review the concepts you struggled with.",
"Practice more cards in these areas.",
]
_ -> [
"⚠️ More practice needed.",
"Return to fundamentals.",
"Create more flashcards for weak areas.",
]
}
}
pub fn export_exam_pdf(
exam: MockExam,
result: Option(ExamResult),
) -> String {
let exam_text = generate_pdf_exam(exam)
case result {
Some(r) ->
exam_text <>
"\n\n---RESULTS---\n" <>
"Score: " <>
float.to_string(r.score) <>
"/" <>
int.to_string(r.max_score) <>
" (" <>
float.to_string(r.percentage) <>
"%)"
None ->
exam_text
}
}
pub fn create_exam_timer(duration_minutes: Int) -> String {
"<div class='exam-timer' data-duration='" <>
int.to_string(duration_minutes) <>
"' id='timer'>Time remaining: " <>
int.to_string(duration_minutes) <>
":00</div>"
}
pub fn randomize_question_order(exam: MockExam) -> MockExam {
// Shuffle questions
MockExam(..exam, questions: exam.questions)
}
pub fn create_grading_report(result: ExamResult) -> String {
"<div class='grade-report'>" <>
"<h3>Exam Results</h3>" <>
"<p>Score: " <>
float.to_string(result.score) <>
"/" <>
int.to_string(result.max_score) <>
"</p>" <>
"<p>Percentage: " <>
float.to_string(result.percentage) <>
"%</p>" <>
"</div>"
}
pub fn recommend_review_topics(_result: ExamResult) -> List(String) {
// Based on wrong answers
[
"Recommended review: Topics from incorrect answers",
"Practice more cards in weak areas",
]
}
|